Generate a list of all unique Tic Tac Toe boards

前端 未结 7 2101

I would like to generate a text file containing all 19,683 Tic-Tac-Toe board layouts in the structure of 0 = Blank, 1 = X, and 2 = O. Unfortunately math is not my strong su

7条回答
  •  我在风中等你
    2020-12-30 09:29

    Generating all possible game boards (depth first search works best) and excluding duplicates under rotation and mirroring results in 765 boards. 626 are mid game, 91 games X has won, 44 games O has won and 3 games are a draw.

    If you are only intresetd in the optimal moves you can simply use https://xkcd.com/832/ as reference. Makes a nice poster.

    But all the fun in tic-tac-toe is in implementing it. So I leaves that to the reader. Just a few tips:

    1. Every tile on the board has 3 states so you can encode the board as number in base 3. For simpler math I use base 4 (2 bit per tile so I only need to shift). I then have a hash function that generates that number for a board under all possible rotations and mirroring (8 cases) and returns the minimum value. By that I can lookup if I played that board already.

    2. Starting with an empty board place a mark on the board in every possible position, check if the board was already played, mark it played, check if the game is over and count the board, otherwise recurse alternating players.

    3. The first X can only be set in 3 places (considering rotation and mirroring) and all later moves have at most 8 choices. Instead of encoding the absolute tile to be played you can only count empty tiles and encode that in 3 bit.

    4. Using the above hash function gives us 626 boards where we have to make a move (you just have to reverse the rotation/mirroring to get the real move out of the data). There is probably a not much larger relative prime number so that each board fits in a hash table without collisions. Lets say that number is 696 (I know, not relative prime). At 3 bits per board that would only need 261 byte of data to store the best move for every possible game.

    5. Since you are playing perfect the number of reachable boards goes way down again. Build a data set for playing X and one for playing O and you can cut that way down again.

    6. Want to make it even smaller? Just program in a few basic rules like: The first O should be in the middle if free. With 2 "my color" in a row complete the row. With 2 "other color" in a row block the row and so on. Wikipedia has a list of 8 rules but I think I had less when I did it that way.

    7. A perfect tic-tac-toe opponent is boring. You can never win. Why not make the game learn from failure? Keep track of all 626 boards and their possible moves. When a move results in a loss remove that move from the board. If the board has no more moves left remove from all boards leading to this one the move that causes it (recursively if that removes the last move there). Your game will never loose the same way twice. Similary for moves resulting in a win you remove the opponents move from the list of possible ones and if there are none left you mark the previous move as a sure win. That way if you can force a win you will always force it from now on. Playing X can you get it to loose 91 ways? Playing O can you get it to loose all 44 ways?

提交回复
热议问题