我有一本字典:
squares = {
'r1c1':{'location':[0,0,150,150],'status':'o'},
'r1c2':{'location':[150,0,300,150],'status':None},
'r1c3':{'location':[300,0,450,150],'status':None},
'r2c1':{'location':[0,150,150,300],'status':'x'},
'r2c2':{'location':[150,150,300,300],'status':'x'},
'r2c3':{'location':[300,150,450,300],'status':'x'},
'r3c1':{'location':[0,300,150,450],'status':None},
'r3c2':{'location':[150,300,300,450],'status':None},
'r3c3':{'location':[300,300,450,450],'status':'o'}
}
忽略位置。R代表行;C代表列。我想知道是否有一个紧凑的函数,我可以用来检查是否所有的行,列,或对角线有相同的值。。。想想提克托。。。到目前为止,我有一个很大的假设声明:
def TicTacToe(self):
#rows
if self.squares['r1c1']['status'] == 'x' and self.squares['r1c2']['status'] == 'x' and self.squares['r1c3']['status'] == 'x':
self.gameOver('x')
elif self.squares['r2c1']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r2c3']['status'] == 'x':
self.gameOver('x')
elif self.squares['r3c1']['status'] == 'x' and self.squares['r3c2']['status'] == 'x' and self.squares['r3c3']['status'] == 'x':
self.gameOver('x')
elif self.squares['r1c1']['status'] == 'o' and self.squares['r1c2']['status'] == 'o' and self.squares['r1c3']['status'] == 'o':
self.gameOver('o')
elif self.squares['r2c1']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r2c3']['status'] == 'o':
self.gameOver('o')
elif self.squares['r3c1']['status'] == 'o' and self.squares['r3c2']['status'] == 'o' and self.squares['r3c3']['status'] == 'o':
self.gameOver('o')
#columns
elif self.squares['r1c1']['status'] == 'x' and self.squares['r2c1']['status'] == 'x' and self.squares['r3c1']['status'] == 'x':
self.gameOver('x')
elif self.squares['r1c2']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r3c2']['status'] == 'x':
self.gameOver('x')
elif self.squares['r1c3']['status'] == 'x' and self.squares['r2c3']['status'] == 'x' and self.squares['r3c3']['status'] == 'x':
self.gameOver('x')
elif self.squares['r1c1']['status'] == 'o' and self.squares['r2c1']['status'] == 'o' and self.squares['r3c1']['status'] == 'o':
self.gameOver('o')
elif self.squares['r1c2']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r3c2']['status'] == 'o':
self.gameOver('o')
elif self.squares['r1c3']['status'] == 'o' and self.squares['r2c3']['status'] == 'o' and self.squares['r3c3']['status'] == 'o':
self.gameOver('o')
#diagonal
elif self.squares['r1c1']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r3c3']['status'] == 'x':
self.gameOver('x')
elif self.squares['r1c3']['status'] == 'x' and self.squares['r2c2']['status'] == 'x' and self.squares['r3c1']['status'] == 'x':
self.gameOver('x')
elif self.squares['r1c1']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r3c3']['status'] == 'o':
self.gameOver('o')
elif self.squares['r1c3']['status'] == 'o' and self.squares['r2c2']['status'] == 'o' and self.squares['r3c1']['status'] == 'o':
self.gameOver('o')
但这很难看,我想做得更好。有什么想法吗?。。。你知道吗
首先,把
squares
转换成更好的,比如现在
board
是一个列表列表:您现在可以使用此问题的数千个代码示例中的任意一个:
像这样使用:
这里有一个NxN维正方形板的通用解决方案。你知道吗
输出
这里有一个检查tic-tac-toe游戏结果的更短的方法,创建一个generator,
lines()
产生所有8行(3行、3列和2条对角线),然后检查其中是否有一个元素只由一个元素组成,而那个元素不是None
一旦你有了
请执行以下操作:
这将适用于任何尺寸的tic-tac趾板,而不仅仅是3×3。你知道吗
相关问题 更多 >
编程相关推荐