有一款TicTacToe游戏已经快要完成了。我只有while循环有问题。我不知道为什么我得不到"X赢“或"O赢”,只能得到“平局”。感谢您的帮助!
我不知道我还应该写什么,但有一句话:“看起来你的帖子主要是代码;请添加更多细节。”消息,所以我写了这篇文章。
x_o = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
x_wins = {'row1_X': "X" == x_o[0] and "X" == x_o[1] and "X" == x_o[2],
'row2_X': "X" == x_o[3] and "X" == x_o[4] and "X" == x_o[5],
'row3_X': "X" == x_o[6] and "X" == x_o[7] and "X" == x_o[8],
'col1_X': "X" == x_o[0] and "X" == x_o[3] and "X" == x_o[6],
'col2_X': "X" == x_o[1] and "X" == x_o[4] and "X" == x_o[7],
'col3_X': "X" == x_o[2] and "X" == x_o[5] and "X" == x_o[8],
'slant1_X': "X" == x_o[0] and "X" == x_o[4] and "X" == x_o[8],
'slant2_X': "X" == x_o[2] and "X" == x_o[4] and "X" == x_o[6]
}
o_wins = {'row1_O': "O" == x_o[0] and "O" == x_o[1] and "O" == x_o[2],
'row2_O': "O" == x_o[3] and "O" == x_o[4] and "O" == x_o[5],
'row3_O': "O" == x_o[6] and "O" == x_o[7] and "O" == x_o[8],
'col1_O': "O" == x_o[0] and "O" == x_o[3] and "O" == x_o[6],
'col2_O': "O" == x_o[1] and "O" == x_o[4] and "O" == x_o[7],
'col3_O': "O" == x_o[2] and "O" == x_o[5] and "O" == x_o[8],
'slant1_O': "O" == x_o[0] and "O" == x_o[4] and "O" == x_o[8],
'slant2_O': "O" == x_o[2] and "O" == x_o[4] and "O" == x_o[6]
}
print("---------")
print("|", " ", " ", " ", "|")
print("|", " ", " ", " ", "|")
print("|", " ", " ", " ", "|")
print("---------")
coordinates = [['1', '3'], ['2', '3'], ['3', '3'], ['1', '2'], ['2', '2'], ['3', '2'], ['1', '1'], ['2', '1'],
['3', '1']]
count = 0
empty_places = coordinates
while True not in x_wins.values() and True not in o_wins.values() and x_o.count("X") + x_o.count("O") != 9:
move = input("Enter the coordinates: ").split(" ")
if move in empty_places:
ind = coordinates.index(move)
empty_places[ind] = [" ", " "]
if count % 2 == 0:
x_o[ind] = "X"
count += 1
else:
x_o[ind] = "O"
count += 1
print("----------")
print("|", x_o[0], x_o[1], x_o[2], "|")
print("|", x_o[3], x_o[4], x_o[5], "|")
print("|", x_o[6], x_o[7], x_o[8], "|")
print("----------")
elif move not in empty_places and move in coordinates:
print("This cell is occupied! Choose another one!")
elif not move[0].isdigit() and not move[1].isdigit():
print("You should enter numbers!")
elif move not in empty_places and int(move[0]) > 3 or int(move[1]) > 3:
print("Coordinates should be from 1 to 3!")
if True in x_wins.values():
print("X wins")
elif True in o_wins.values():
print("O wins")
elif True not in x_wins.values() and True not in o_wins.values():
print("Draw")发布于 2020-06-06 19:53:55
在程序开始时,您只定义了一次x_wins和o_wins,所以在整个程序运行过程中,它们都会保持初始值,也就是说,完全用False填充。要解决这个问题,应该将x_wins和o_wins放入返回字典的函数中--这样,无论何时调用x_wins()和o_wins(),它都会重新计算字典,并正确地确定其中一个是否成功。
def x_wins():
return {'row1_X': "X" == x_o[0] and "X" == x_o[1] and "X" == x_o[2],
'row2_X': "X" == x_o[3] and "X" == x_o[4] and "X" == x_o[5],
'row3_X': "X" == x_o[6] and "X" == x_o[7] and "X" == x_o[8],
'col1_X': "X" == x_o[0] and "X" == x_o[3] and "X" == x_o[6],
'col2_X': "X" == x_o[1] and "X" == x_o[4] and "X" == x_o[7],
'col3_X': "X" == x_o[2] and "X" == x_o[5] and "X" == x_o[8],
'slant1_X': "X" == x_o[0] and "X" == x_o[4] and "X" == x_o[8],
'slant2_X': "X" == x_o[2] and "X" == x_o[4] and "X" == x_o[6]
}https://stackoverflow.com/questions/62231250
复制相似问题