7 ms·
Which part did you find buggy? I would like to tighten it up. Thanks!
by integraleq 16y ago
Which 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.
- integraleq 16y agoYou 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.