【发布时间】:2018-11-16 18:13:19
【问题描述】:
我似乎无法弄清楚我应该如何让我的垂直移动发挥作用 我的板是一个嵌套列表,由 6 个列表组成,每个列表有 6 个元素。
我已经能够计算出我的水平和对角获胜条件。但我不能把头绕在垂直的上面
这是我的水平条件,例如
#the list itself
board = [
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
]
def hor_condition(board, player):
"""the hor_condition checks whether the list contains necessary amount of checkers in order to call it a win"""
for row in board:
for cell in row:
if row.index(cell) < 2:
check_list = [row[row.index(cell)],
row[row.index(cell) + 1],
row[row.index(cell) + 2],
row[row.index(cell) + 3]
]
if check_list[0] == player and\
check_list[1] == player and \
check_list[2] == player and \
check_list[3] == player:
return True
这里检查一个玩家在同一行中是否有 4 个相同的元素,以便称其为胜利。 我期望垂直条件做的是检查同一列中是否有 4 个相同的检查器
看起来可能会很痛苦,但请记住,我刚刚开始使用 python,并且开始了解基础知识。
我并不真正要求黑白代码,而是朝着正确的方向推进
如有必要,我很乐意提供任何进一步的信息!提前谢谢!
编辑: 这有点晚了,但我想帮助那些和我有同样问题的人。我最终解决了以下问题:
def vert_condition(board, player):
"""The vert_condition iterates over the board and checks whether 4 vertical slots contain the same player input by
with the help of a nested loop"""
for row in range(3):
for col in range(6):
if (board[row][col] == \
board[row + 1][col] == \
board[row + 2][col] == \
board[row + 3][col] == player):
return True
【问题讨论】:
-
请解释你真正的问题是什么。
-
我想不出垂直条件的开头
-
欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。
-
@Prune 我希望我能遵守,但我不知道还能分享什么。正如我试图解释的那样,我想了解如何处理一个检查条件(在这种情况下,同一玩家的 4 个元素在一个列中一个接一个)是否已满足的函数。
-
您只是将一些相关代码丢给我们,而没有任何数据结构的演示,也没有我们测试建议或解决方案所需的工具。我不知道
cell是什么,您必须使用index找到另一个实例。我不知道为什么2是边界。我不明白重复的电话。正如发布指南告诉您的那样,请方便我们为您提供帮助。
标签: python list if-statement