Author Topic: Impossible to beat Tic-Tac-Toe Bot  (Read 7502 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Impossible to beat Tic-Tac-Toe Bot
« on: October 16, 2018, 04:00:53 am »
Hey,
I got a bit bored and decided to type up a Tic-Tac-Toe bot that could not be beaten.
Give it a shot, try your luck.

Thanks,
Zeppelin


P.s If you beat it let me know, either you've modified my code or there is a bug :)
* TICTACTOE BOT.bas (Filesize: 8.77 KB, Downloads: 375)
* TICTACTOE BOT.exe (Filesize: 1.42 MB, Downloads: 473)
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: Impossible to beat Tic-Tac-Toe Bot
« Reply #1 on: October 16, 2018, 04:13:47 am »
Ok. I just realised that I forgot to make the bot actually try to win instead of just defend everything.
So Im gonna go fix that now.
You still can't win by the way.

Zeppelin
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Impossible to beat Tic-Tac-Toe Bot
« Reply #2 on: October 17, 2018, 05:03:43 pm »
Hi Zeppelin

I'm sorry but at the first time I got this (see picture attached) playing with your TicTacToeBot

So IMHO AI must be enpowered!
TICTACTOEBOT_DEFEATED.jpg
* TICTACTOEBOT_DEFEATED.jpg (Filesize: 206.04 KB, Dimensions: 1366x768, Views: 606)
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Impossible to beat Tic-Tac-Toe Bot
« Reply #3 on: October 17, 2018, 05:15:18 pm »
As feedback for debug I can post the sequence of choices

        first game                          second game
I     2  5  6  4      wins        I       2  5  7  6   4   wins
Bot  3  8  7                       Bot    9  8  3  1

Good Debug
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Impossible to beat Tic-Tac-Toe Bot
« Reply #4 on: October 17, 2018, 07:33:04 pm »
Hi TempodiBasic,

Let me know if you can beat this mouse version (don't have to use X's or O's, AI calls itself AI):
Code: QB64: [Select]
  1. _TITLE "Tic Tac Toe with AI"
  2. 'QB64 version 2017 1106/82 (the day before they switched to version 1.2)
  3. 'bplus 2018-01-23
  4. CONST xmax = 800
  5. CONST ymax = 600
  6. CONST redh& = &HFFFF0000
  7. CONST greenh& = &HFF00FF00
  8. CONST blueh& = &HFF0000FF
  9.  
  10. SCREEN _NEWIMAGE(xmax, ymax, 32)
  11. _SCREENMOVE 360, 60
  12.  
  13. DEFINT A-Z
  14. COMMON SHARED player$, AI$
  15. DIM SHARED board$(2, 2) 'store X and O here 3x3
  16.  
  17.     COLOR greenh&: CLS
  18.     winner$ = "": count = 0: done = 0: ERASE board$
  19.  
  20.     'get player's choice
  21.     INPUT "Player, enter symbol to use for your squares: (nothing quits) "; player$
  22.     IF player$ = "" THEN END
  23.     IF player$ = "AI" THEN AI$ = "#1 AI" ELSE AI$ = "AI"
  24.     IF LEN(player$) > 15 THEN player$ = LEFT$(player$, 15)
  25.  
  26.     'who plays first
  27.     INPUT "And now please enter y for yes, if you want to go first: "; first$
  28.     IF first$ = "y" THEN turn$ = player$ ELSE turn$ = AI$
  29.  
  30.     'prep, make grid according to size of longest participant string
  31.     IF LEN(player$) > LEN(AI$) THEN s = LEN(player$) ELSE s = LEN(AI$)
  32.     sq = 16 * (s + 2)
  33.     xoff = (xmax - 3 * sq) / 2
  34.     yoff = (ymax - 3 * sq) / 2
  35.  
  36.     'make # grid once
  37.     CLS
  38.     FOR i = 1 TO 2
  39.         LINE (sq * i + xoff, yoff)-STEP(0, 3 * sq)
  40.         LINE (xoff, sq * i + yoff)-STEP(3 * sq, 0)
  41.     NEXT
  42.  
  43.     'take turns filling out the board until a winner$ is found or board is out of spaces
  44.     DO
  45.         IF turn$ = AI$ THEN
  46.             rc = AIchoice
  47.             bx = rc MOD 3: by = INT(rc / 3)
  48.             board$(bx, by) = AI$
  49.             _DELAY 1 'let player think AI is thinking
  50.             COLOR blueh&
  51.             _PRINTSTRING (bx * sq + xoff + sq / 2 - _PRINTWIDTH(AI$) / 2, by * sq + yoff + sq / 2 - 8), AI$
  52.             count = count + 1
  53.             IF checkwin THEN winner$ = AI$
  54.             turn$ = player$
  55.         ELSE
  56.             'player's turn from mouse click
  57.  
  58.             'this might NOT be too intuitive but proper mouse catching demands we wait for mouse button release
  59.             m = _MOUSEINPUT: mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  60.             IF mb THEN 'get last place mouse button was down
  61.                 mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  62.                 WHILE mb 'left button down, wait for mouse button release before doing anything as a "click"
  63.                     'this updates mx, my while waiting for button release
  64.                     m = _MOUSEINPUT: mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  65.                 WEND
  66.  
  67.                 'board x and board y?
  68.                 ' board is offset by (xoff, yoff) = top left corner, so subtract these from mouse mx, my
  69.                 ' then divide by the size of the square (sq) to get the position in the board array
  70.                 ' check that position is inbounds of board array
  71.                 bx = INT((mx - xoff) / sq): by = INT((my - yoff) / sq)
  72.  
  73.                 'now we have mouse pixels converted to board position of array
  74.                 IF bx >= 0 AND bx <= 2 AND by >= 0 AND by <= 2 THEN 'caught mouse in a box!
  75.                     IF board$(bx, by) <> "" THEN
  76.                         BEEP 'NO good! already clicked!
  77.                     ELSE
  78.                         'OK it all checks out, update the board array and the screen display
  79.                         COLOR redh&
  80.                         _PRINTSTRING (bx * sq + xoff + sq / 2 - _PRINTWIDTH(player$) / 2, by * sq + yoff + sq / 2 - 8), player$
  81.                         board$(bx, by) = player$
  82.                         count = count + 1
  83.                         IF checkwin THEN winner$ = player$
  84.                         turn$ = AI$
  85.                     END IF
  86.                 ELSE
  87.                     BEEP 'you are clicking out of bounds of board!
  88.                 END IF
  89.             END IF
  90.         END IF 'turn
  91.         IF winner$ <> "" THEN done = 1
  92.         IF done = 0 AND count >= 9 THEN done = -1
  93.     LOOP UNTIL done
  94.     COLOR greenh&
  95.     IF done = -1 THEN s$ = "Out of spaces." ELSE s$ = winner$ + " is the winner!"
  96.     _PRINTSTRING ((xmax - _PRINTWIDTH(s$)) / 2, 2 * _FONTHEIGHT), s$
  97.     s$ = "Wait 5 secs for next game..."
  98.     _PRINTSTRING ((xmax - _PRINTWIDTH(s$)) / 2, 4 * _FONTHEIGHT), s$
  99.     _DELAY 5
  100.  
  101. FUNCTION checkwin
  102.     FOR i = 0 TO 2
  103.         IF (board$(0, i) = board$(1, i) AND board$(1, i) = board$(2, i)) AND (board$(2, i) <> "") THEN checkwin = 1: EXIT SUB
  104.     NEXT
  105.     FOR i = 0 TO 2
  106.         IF (board$(i, 0) = board$(i, 1) AND board$(i, 1) = board$(i, 2)) AND board$(i, 2) <> "" THEN checkwin = 1: EXIT SUB
  107.     NEXT
  108.     IF (board$(0, 0) = board$(1, 1) AND board$(1, 1) = board$(2, 2)) AND board$(2, 2) <> "" THEN checkwin = 1: EXIT SUB
  109.     IF (board$(0, 2) = board$(1, 1) AND board$(1, 1) = board$(2, 0)) AND board$(2, 0) <> "" THEN checkwin = 1
  110.  
  111. FUNCTION AIchoice
  112.     'test all moves to win
  113.     FOR r = 0 TO 2
  114.         FOR c = 0 TO 2
  115.             IF board$(c, r) = "" THEN
  116.                 board$(c, r) = AI$
  117.                 IF checkwin THEN
  118.                     board$(c, r) = ""
  119.                     AIchoice = 3 * r + c
  120.                     EXIT FUNCTION
  121.                 ELSE
  122.                     board$(c, r) = ""
  123.                 END IF
  124.             END IF
  125.         NEXT
  126.     NEXT
  127.  
  128.     'still here? then no winning moves for AI, how about for player$
  129.     FOR r = 0 TO 2
  130.         FOR c = 0 TO 2
  131.             IF board$(c, r) = "" THEN
  132.                 board$(c, r) = player$
  133.                 IF checkwin THEN
  134.                     board$(c, r) = ""
  135.                     AIchoice = 3 * r + c 'spoiler move!
  136.                     EXIT FUNCTION
  137.                 ELSE
  138.                     board$(c, r) = ""
  139.                 END IF
  140.             END IF
  141.         NEXT
  142.     NEXT
  143.  
  144.     'still here? no winning moves, no spoilers then is middle sq available
  145.     IF board$(1, 1) = "" THEN AIchoice = 4: EXIT FUNCTION
  146.  
  147.     'still here still? how about a corner office?
  148.     IF board$(0, 0) = "" THEN AIchoice = 0: EXIT FUNCTION
  149.     IF board$(2, 0) = "" THEN AIchoice = 2: EXIT FUNCTION
  150.     IF board$(0, 2) = "" THEN AIchoice = 6: EXIT FUNCTION
  151.     IF board$(2, 2) = "" THEN AIchoice = 8: EXIT FUNCTION
  152.  
  153.     'still here??? one of these has to go!
  154.     IF board$(1, 0) = "" THEN AIchoice = 1: EXIT FUNCTION
  155.     IF board$(0, 1) = "" THEN AIchoice = 3: EXIT FUNCTION
  156.     IF board$(2, 1) = "" THEN AIchoice = 5: EXIT FUNCTION
  157.     IF board$(1, 2) = "" THEN AIchoice = 7: EXIT FUNCTION
  158.  
  159.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Impossible to beat Tic-Tac-Toe Bot
« Reply #5 on: October 18, 2018, 02:54:52 pm »
Hi Bplus
Thanks to promote me to betatester....
I find your AI as stronge as to draw both starting for first both starting for second.
Moreover if user looses attention
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Impossible to beat Tic-Tac-Toe Bot
« Reply #6 on: October 18, 2018, 03:14:03 pm »
Hi Bplus
Thanks to promote me to betatester....
I find your AI as stronge as to draw both starting for first both starting for second.
Moreover if user looses attention

Thanks how much do I owe you?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Impossible to beat Tic-Tac-Toe Bot
« Reply #7 on: October 18, 2018, 07:24:49 pm »
@Bplus

:-)
as many ideas or suggestions you can pass to a QB64_friend that likes joke and programming
Programming isn't difficult, only it's  consuming time and coffee