Live data from Hacker News

Can You Beat This Game?

news.ycombinator.com

1–10 of 15 posts

Can You Beat This Game?

#1
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!

Re: Can You Beat This Game?

#3
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?

#4
post #3

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.

Which part did you find buggy? I would like to tighten it up. Thanks!

Re: Can You Beat This Game?

#5
post #3

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.

Which part did you find buggy? I would like to tighten it up. Thanks!

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?

#6
post #5

Earlier quoted context omitted.

Which part did you find buggy? I would like to tighten it up. Thanks!

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. #…

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?

#7
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?

#8

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…

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?

#9

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…

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.....

Ok!!! Round 2! Any challengers?

Re: Can You Beat This Game?

#10
post #5

Earlier quoted context omitted.

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. #…

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.

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.
Post reply on HN