Tic-Tac-Toe:如何填充决策树?
我正在制作一个井字游戏程序。我计划将 minimax 与它一起使用。我制作了一棵树,其中包含所有可能的游戏序列的空间,并且我正在寻找一种方法来填充它。我目前有这种类型:
typedef struct name
{
char grid [3] [3];
struct name * child [9];
} node;
我正在寻找一种填充网格的方法,就像它所示的此处。我将如何填充网格以确保所有可能的组合都存在?我的计划是让游戏识别玩家可以采取的每一步,然后决定采取哪些步骤才能获胜(我仍然需要弄清楚决策部分,但我会坚持下去,直到我可以填充树中的网格) 。
I'm making a Tic-Tac-Toe program. I plan to use minimax with it. I made a tree with space for all possible game sequences and I'm looking for a way to populate it. I currently have this type:
typedef struct name
{
char grid [3] [3];
struct name * child [9];
} node;
and I'm looking for a way to fill grid just like it's shown here. How would I go filling the grid to make sure that all possible combinations are there? My plan is to have the game recognize every move player can take and then decide what steps to take in order to win (I still need to figure out the decision part, but I'm holding that until I can fill the grids in tree).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对我来说听起来像是递归的主要候选者......
Sounds like a prime candidate for recursion to me...
对基数 3 中的位置进行编码。创建一个未使用的网格
0
;带有“X”的网格1
;以及带有“O”a2
的网格。因此,您可以拥有的完整网格的绝对最大数量是 3^9 = 19683(其中一些是无效的井字游戏表示)因此,假设您正在处理“020112021”。它的孩子们是:
我认为你可以想出一种从这里继续下去的方法。
Encode the positions in base 3. Make a unused grid
0
; a grid with "X" a1
; and a grid with "O" a2
. So the absolute maximum number of complete grids you can have is 3^9 = 19683 (some of those are invalid tic-tac-toe representations)So let's say you are dealing with '020112021'. Its children are:
I think you can figure a way to go on from here.
伪代码,因为递归很难放入列表中:
您应该能够通过几个
for
循环和if
语句来做到这一点。Pseudocode because recursion is hard to make into a list:
You should be able to do that with a couple
for
loops andif
statements.编辑
:以防万一上述情况链接中断,它引用了《科学美国人》1989 年 10 月对 Tinkertoy 计算机的描述,该文章还与《The Tinkertoy 计算机和其他机器》同一作者的其他 SA 娱乐文章一起编译和出版。 /em>.
建造这台机器的家伙(和女孩)足够聪明,可以避免任何 alpha-beta 搜索,并将棋盘压缩成易于计算的东西。由 Tinkertoys 制作。
Child's play.
EDIT: Just in case the above link breaks, it's a reference to a description of the Tinkertoy Computer from Scientific American October 1989, also compiled and published with other SA amusement articles by the same author as The Tinkertoy Computer and Other Machinations.
The guys (and gals) who built this machine were clever enough to avoid any alpha-beta search as well as compress the board into something that could easily be computed. By Tinkertoys.
这里你有一个可行的解决方案。它是用Python实现的,但我认为它可能对你有帮助。
游戏树由
build_tree
函数递归构建,并以列表的列表形式实现。build_tree
函数将棋盘(树的一个节点)和轮到玩的棋子作为输入参数,并构建将棋子应用到棋盘后产生的所有可能的新棋盘。然后,对于每个新棋盘,它再次调用build_tree
函数,但这次更改了轮到玩的棋子。当棋盘结束时(没有空方块),递归结束。这是 1x3 棋盘的结果树:
[(0, 0, 0), [[('x', 0, 0), [[('x', 'o', 0), [('x' , 'o', 'x')]], [('x', 0, 'o'), [('x', 'x', 'o')]]]], [(0, 'x') ', 0), [[('o', 'x', 0), [('o', 'x', 'x')]], [(0, 'x', 'o'), [ ('x', 'x', 'o')]]]], [(0, 0, 'x'), [[('o', 0, 'x'), [('o', ' x', 'x')]], [(0, 'o', 'x'), [('x', 'o', 'x')]]]]]]
空方块用 '0 表示'。
对于井字游戏,请将
blank_board = (0,0,0)
更改为blank_board = (0,0,0,0,0,0,0,0,0)
以便拥有 3x3 板。Here you have a working solution. It is implemented in Python but I think it may help you.
The game tree is build recursively by the
build_tree
function, and is implemented as a list of lists.The
build_tree
function takes a board (a node of the tree) and the piece that has the turn to play as input parameters, and builds all the possible new boards resulting of applying the piece to the board. Then, for each of these new boards, it calls thebuild_tree
function again, but this time changing the piece that has the turn to play. When the board is terminal (no empty squares) the recursion ends.This is the resulting tree for a 1x3 board:
[(0, 0, 0), [[('x', 0, 0), [[('x', 'o', 0), [('x', 'o', 'x')]], [('x', 0, 'o'), [('x', 'x', 'o')]]]], [(0, 'x', 0), [[('o', 'x', 0), [('o', 'x', 'x')]], [(0, 'x', 'o'), [('x', 'x', 'o')]]]], [(0, 0, 'x'), [[('o', 0, 'x'), [('o', 'x', 'x')]], [(0, 'o', 'x'), [('x', 'o', 'x')]]]]]]
Empty squares are denoted by '0'.
For the tic tac toe game, please change
blank_board = (0,0,0)
toblank_board = (0,0,0,0,0,0,0,0,0)
in order to have a 3x3 board.这是递归的经典案例:
This is a classic case for recursion: