Tic Tac Toe - 寻找空的网格图块 [英] Tic Tac Toe - Finding empty grid tiles

查看:31
本文介绍了Tic Tac Toe - 寻找空的网格图块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前关于井字游戏的问题的延续.我正在制作一个函数,它将收集井字棋盘的所有空网格图块,并将它们返回到列表中.这个函数是递归的,因为它会不断寻找与移动相邻的空网格图块.像这样:

This is a continuation of my previous question regarding the tic tac toe game. I am making a function that will collect all the empty grid tiles of a tic tac toe board, and return them in a list. This function would be recursive, in that it would keep looking for empty grid tiles that are adjacent to a move made. Something like this:

<------->
< X O - >
< - - X >
< O X - >
<------->

所以假设用户(或者在这种情况下,我为计算机编写的代码与计算机对战)想通过选择一个图块来知道哪些网格图块是空的.对于上面的例子,瓷砖将被编号

So lets say the user (or in this case, the code I wrote for the computer playing against the computer) wants to know what grid tiles are empty by picking a tile. For the above example, the tiles would be numbered

0 1 2
3 4 5 
6 7 8

所以假设计算机选择了 tile 1 - 它会搜索相邻的 tile(因此在本例中为顶部、左侧、右侧和底部)并查看它是否可以在那里移动.如果它发现一个相邻的牌是空的,它也会找到该牌的相邻牌,直到它穷尽所有的可能性.所以我希望能够使用当前板和 playerMove 调用我的函数,并找到相邻的空图块,并将它们附加到列表中.有什么提示吗?

and so lets say the computer chose tile 1 - it would search the adjacent tiles (so in this case, top, left, right, and bottom) and see if it can make moves there. If it finds that an adjacent tile is empty, it finds the adjacent tiles of that tile as well, until it exhausts all possibilities. So I want to be able to call my function with the current board and a playerMove, and find the adjacent tiles that are empty, and append them to a list. Any tips?

def whatIsEmpty(moveList,move):
     emptyTiles = []
     #something that allows you to find the adjacent tiles of the move
     #something that allows you to find the adjacent tiles of the tiles found above, until all are found

我知道为此我需要几个 for 循环,但我不知道如何开始.我所知道的是我想在一个假想的 3x3 网格的中间移动,并找到它的相邻图块以查​​看它们是否为空,等等.所以在 2D 列表中,我会使用这样的东西:

I know I need a couple for loops for this, but I'm not sure how to start. What I know is I want to have move in the middle of an imaginary 3x3 grid, and find its adjacent tiles to see if they are empty, and so on. So in a 2D list, I would use something like this:

moveList[x-1][y]
moveList[x][y+1]
moveList[x+1][y]
moveList[x][y+1]

其中每一个对应于顶部、右侧、左侧和底部,这将被递归使用.非常感谢任何提示.

Where each of those corresponds to top, right, left, and bottom, and this would be used recursively. Any tips are greatly appreciated.

推荐答案

在这种情况下,递归策略可能不是最佳选择.考虑您自己提供的示例:

A recursive strategy might not be best in this scenario. Consider the example you yourself provided:

<------->
< X O - >
< - - X >
< O X - >
<------->

假设下一步是在中间的瓷砖上进行的.如果递归函数只检查与其相邻的四个瓦片,那么它将错过与其余瓦片(右下角)截断"的瓦片.如果您要编写一个函数来检查所有 8 个相邻的图块(包括对角相邻的图块),您不妨迭代地编写它.

Suppose the next move was made in the middle tile. If the recursive function checks only the four tiles adjacent to it, then it would miss the tile that was "cut off" from the rest (the bottom right one). And if you were to write a function to check all 8 adjacent tiles (including diagonally adjacent) you might as well write it iteratively.

for i in range(3):
   for j in range(3):
      pass # Replace with code to add empty tile to list

这篇关于Tic Tac Toe - 寻找空的网格图块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆