本文介绍了Tic Tac Toe 对角线检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个井字游戏,我有一个如下所示的垂直和水平检查:
I'm building a game of Tic-Tac-Toe, and I have a vertical and horizontal check that look like this:
def check_win_left_vert (board):
win = True
x = 0
for y in range (2):
if board[y][x] != board[y+1][x]:
win = False
return win
它通过增加 y 轴来看板;我对 x 轴使用相同的方法.我将如何为对角轴执行此操作?我会增加两者吗?
It looks through the board by incrementing the y axis; I use the same method for the x axis. How would I do this for a diagonal axis? Would I increment both?
推荐答案
所有带有列表推导式的检查
game_board = [ [1, 0, 1],
[0, 1, 0],
[0, 1, 0] ]
# Horizontals
h = [str(i+1) + ' Row' for i, v in enumerate(game_board) if sum(v) == 3]
# Verticals
v = [str(i+1) + ' Col' for i in range(3) if sum([j[i] for j in game_board]) == 3]
# Diagonals
d = [['Left Diag', '','Right Diag'][i+1] for i in [-1, 1] if sum([game_board[0][1+i], game_board[1][1]], game_board[2][1-i]) == 3]
if any([h,v,d]):
print('You won on:', h, v, d)
else:
print('No win yet')
这篇关于Tic Tac Toe 对角线检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!