4 ms·
Can You Beat This Game?
HN,
git://github.com/inteq/Tic-Tac-Toe.git
I would like to issue a challenge to see if you can find a way to beat my tic-tac-toe program. I would definitely appreciate any feedback that will help me improve the brains of the cpu opponent. It is console driven and not pretty on the outside. Thanks!
- mphillips34 16y agohttp://en.wikipedia.org/wiki/Tic-tac-toe#Strategy http://en.wikipedia.org/wiki/Tic-tac-toe#Strategy
- po 16y agoIt was a bit buggy but I beat the cpu. If you play tic-tac-toe the player who goes first should be able to win or at worst case force a draw. If you go second you should be able to force a draw. With two players who both know how to play an optimal game, you will always have a draw. Tic-tac-toe is basically a solved game which should make it easy to code.
- integraleq 16y agoWhich part did you find buggy? I would like to tighten it up. Thanks!
- po 16y agoAll of the squares in the field were labeled with "-1" $ python tictactoe.py Enter H or T to decide if you will go first:h Enter 'X' or 'O' to select the shape that you want to use:x #### Initial Game Board State [-1, -1, -1] [-1, -1, -1] [-1, -1, -1] Enter the slot(location) where you want to place your shape:(available slots are :'0', '1', '2', '3', '4', '5', '6', '7', '8'):1 you have chosen to place an 'X' at 1. #### Game Board State After Move By:player [-1, 'X', -1] [-1, -1, -1] [-1, -1, -1]
- integraleq 16y agoThat is not a bug. It is there because these locations are just not initialized. How did you manage to beat the cpu? Can you remember the last state of the game board which led to your victory? Thanks.
- po 16y agoIt was unexpected by me so I consider it a bug. You're encoding the game state as a giant switch statement. This is not the best approach. The size of your code will increase exponentially with the complexity of the game. The only reason it works at all is because tic-tac-toe is very simple. You should read up on representing this as a game tree: http://en.wikipedia.org/wiki/Game_tree http://en.wikipedia.org/wiki/Game_tree http://en.wikipedia.org/wiki/Extensive-form_game http://en.wikipedia.org/wiki/Extensive-form_game Here was my first run, pretty much randomly choosing boxes: Enter H or T to decide if you will go first:h Enter 'X' or 'O' to select the shape that you want to use:x #### Initial Game Board State [-1, -1, -1] [-1, -1, -1] [-1, -1, -1] Enter the slot(location) where you want to place your shape:(available slots are :'0', '1', '2', '3', '4', '5', '6', '7', '8'):1 you have chosen to place an 'X' at 1. #### Game Board State After Move By:player [-1, 'X', -1] [-1, -1, -1] [-1, -1, -1] cpu has chosen to place an 'O' at 2. #### Game Board State After Move By:cpu [-1, 'X', 'O'] [-1, -1, -1] [-1, -1, -1] Enter the slot(location) where you want to place your shape:(available slots are :'0', '3', '4', '5', '6', '7', '8'):2 Enter the slot(location) where you want to place your shape:(available slots are :'0', '3', '4', '5', '6', '7', '8'):3 you have chosen to place an 'X' at 3. #### Game Board State After Move By:player [-1, 'X', 'O'] ['X', -1, -1] [-1, -1, -1] cpu has chosen to place an 'O' at 6. #### Game Board State After Move By:cpu [-1, 'X', 'O'] ['X', -1, -1] ['O', -1, -1] Enter the slot(location) where you want to place your shape:(available slots are :'0', '4', '5', '7', '8'):4 you have chosen to place an 'X' at 4. #### Game Board State After Move By:player [-1, 'X', 'O'] ['X', 'X', -1] ['O', -1, -1] cpu has chosen to place an 'O' at 5. #### Game Board State After Move By:cpu [-1, 'X', 'O'] ['X', 'X', 'O'] ['O', -1, -1] Enter the slot(location) where you want to place your shape:(available slots are :'0', '7', '8'):8 you have chosen to place an 'X' at 8. #### Game Board State After Move By:player [-1, 'X', 'O'] ['X', 'X', 'O'] ['O', -1, 'X'] cpu has chosen to place an 'O' at 7. #### Game Board State After Move By:cpu [-1, 'X', 'O'] ['X', 'X', 'O'] ['O', 'O', 'X'] Enter the slot(location) where you want to place your shape:(available slots are :'0'):0 you have chosen to place an 'X' at 0. #### Game Board State After Move By:player ['X', 'X', 'O'] ['X', 'X', 'O'] ['O', 'O', 'X'] #### Ending Game Board State ['X', 'X', 'O'] ['X', 'X', 'O'] ['O', 'O', 'X'] player has won the game... Hope that is helpful.
- systemtrigger 16y agocracks knuckles After a few minutes trying different strategies: If I win the coin toss I'm winning the game, every time. If I lose the coin toss it always ends in a draw. Transcript (of 7 wins): http://dl.dropbox.com/u/142733/tictactoe.txt http://dl.dropbox.com/u/142733/tictactoe.txt Assuming I win the coin toss, I always choose a corner for my first move. The CPU should then take the center but it never does. This ensures I always win. That said, great job so far. It's very close to perfect IMO.
- integraleq 16y agoThanks a ton! I will tighten it up! Hopefully, you will pull the next release and try to beat it again. I do hope it was not a complete pushover.....
- integraleq 16y agoOk!!! Round 2! Any challengers?
- systemtrigger 16y agoI couldn't resist... Well done. This version appears to be unbeatable.
- po 16y agoAlso you neglected to mention the most interesting part of this: http://github.com/RichardBronosky/Tic-Tac-Toe http://github.com/RichardBronosky/Tic-Tac-Toe The forked network for this project is interesting. Is this for a class or something?
- integraleq 16y agoSorry that I did not mention that part: This was actually for a job interview. So, it had to be completed in a short amount of time.
- zbanks 16y agoTic-tac-toe is proven to be unlosable. (You can win if you go first & you are playing an imperfect opponent) Check out http://en.wikipedia.org/wiki/Tic-tac-toe#Strategy http://en.wikipedia.org/wiki/Tic-tac-toe#Strategy for the description of the algorithm.