News for hackers. Stuff that matters.

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!

( 14 comments | 2 points | on HN )
Re:Can You Beat This Game? by systemtrigger on Sunday September 12, 2010 @10:54PM (#1685322)
cracks 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

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.

Re:Can You Beat This Game? by integraleq on Sunday September 12, 2010 @10:58PM (#1685331)
Thanks 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.....
Re:Can You Beat This Game? by integraleq on Sunday September 12, 2010 @11:03PM (#1685344)
Ok!!! Round 2! Any challengers?
Re:Can You Beat This Game? by systemtrigger on Sunday September 12, 2010 @11:20PM (#1685377)
I couldn't resist... Well done. This version appears to be unbeatable.
Re:Can You Beat This Game? by po on Sunday September 12, 2010 @09:59PM (#1685202)
It 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.

Re:Can You Beat This Game? by integraleq on Sunday September 12, 2010 @10:02PM (#1685207)
Which part did you find buggy? I would like to tighten it up. Thanks!
Re:Can You Beat This Game? by po on Sunday September 12, 2010 @10:18PM (#1685244)
All 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]
Re:Can You Beat This Game? by integraleq on Sunday September 12, 2010 @10:26PM (#1685262)
That 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.
Re:Can You Beat This Game? by po on Sunday September 12, 2010 @11:09PM (#1685351)
It 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/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.
Re:Can You Beat This Game? by integraleq on Sunday September 12, 2010 @11:27PM (#1685399)
You are correct about the representation, but I went for the most low-scale approach since the search space is so small. The next version will learn how to build its own rules, but that is for another day. Thanks for all of your help though. I really appreciate it.
Re:Can You Beat This Game? by mphillips34 on Sunday September 12, 2010 @09:49PM (#1685178)
Re:Can You Beat This Game? by zbanks on Monday September 13, 2010 @03:48PM (#1687958)
Tic-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 for the description of the algorithm.

Re:Can You Beat This Game? by po on Sunday September 12, 2010 @11:29PM (#1685402)
Also you neglected to mention the most interesting part of this:

http://github.com/RichardBronosky/Tic-Tac-Toe

The forked network for this project is interesting. Is this for a class or something?

Re:Can You Beat This Game? by integraleq on Tuesday September 14, 2010 @10:07AM (#1690579)
Sorry 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.