【发布时间】:2021-12-19 23:19:27
【问题描述】:
我目前正在开发一个程序,该程序允许您选择游戏板的大小,然后允许玩家选择 S 或 O 以实现 SOS 的目标。我已经用 gui 中的按钮列表完成了这项工作。现在我坚持的问题是检查是否已发出 SOS,同时还要保持在我的列表索引内。代码中的打印语句是为了帮助我确定正在调用什么方法,这是我遇到的另一个问题,具体取决于单击了哪个按钮,只有在调用语句时才能确定。
def checksos(i, j):
for i in range (len(board[j])):
for j in range(len(board[j])):
if board[i][j]["text"]=='S':
if not i >= len(board[j]):
print("h")
if board[j][i-1]["text"] == 'O' and board[j][i-2]["text"]== 'S':
this will execute if there is a horizontal sos
print("found horizontal sos")
#check if it will go out of boundaries horizontally and vertically
if (not i >= len(board[j])-2) and (not j >= len(board)-2):
print("d")
if board[j+1][i+1]["text"] == 'O' and board[j+2][i+2]["text"] == 'S':
#this will execute if there is a diagonal sos
print("found diagonal sos")
#check if it will go out of boundaries vertically
if not j >= len(board)-2:
print("v")
if board[j+1][i]["text"] == 'O' and board[j+2][i]["text"] == 'S':
#this will execute if there is a vertical sos
print("found vertical sos")
if not j >= len(board)-1:
print("v")
if board[j+1][i]["text"] == 'O' and board[j+2][i]["text"] == 'S':
#this will execute if there is a vertical sos
print("found vertical sos")
if not j >= len(board):
print("v")
if board[j+1][i]["text"] == 'O' and board[j+2][i]["text"] == 'S':
#this will execute if there is a vertical sos
print("found vertical sos")
elif board[i][j]["text"]=='O':
if not i >= len(board[j])-2:
print("not out of bounds")
if board[j][i-1]["text"] == 'S' and board[j][i+1]["text"]== 'S':
#this will execute if there is a horizontal sos
print("found horizontal sos")
#check if it will go out of boundaries horizontally and vertically
if (not i >= len(board[j])-2) and (not j >= len(board)-2):
if board[j+1][i+1]["text"] == 'S' and board[j-1][i-1]["text"] == 'S':
#this will execute if there is a diagonal sos
print("found diagonal sos")
#check if it will go out of boundaries vertically
if not j >= len(board)-2:
if board[j+1][i]["text"] == 'S' and board[j-1][i]["text"] == 'S':
#this will execute if there is a vertical sos
print("found vertical sos")
【问题讨论】:
标签: python list user-interface tkinter indexing