在我的井字游戏中,经过4个回合,它宣布X(第一个玩家)为获胜者,即使不是。我不知道我错过了什么,当只检查列但现在却没有行时它正在工作。当我用字母调用该函数时,我感到有问题,但我不确定。
moves = [["1", "2", "3"],
["1", "2", "3"],
["1", "2", "3"]]
def win(letter):
if(moves[0][0] == letter and
moves[1][0] == letter and
moves[2][0] == letter):
print("~~~ " + letter + " WON!!! CONGRATS!!!! ~~~")
quit()
elif(moves[0][1] == letter and
moves[1][1] == letter and
moves[2][1] == letter):
print("~~~ " + letter + " WON!!! CONGRATS!!!! ~~~")
quit()
elif(moves[0][2] == letter and
moves[1][2] == letter and
moves[2][2] == letter ):
print("~~~ " + letter + " WON!!! CONGRATS!!!! ~~~")
quit()
elif(moves[0][0] == letter and
moves[0][1] == letter and
moves[0][2]):
print("~~~ " + letter + " WON!!! CONGRATS!!!! ~~~")
quit()
elif(moves[1][0] == letter and
moves[1][1] == letter and
moves[1][2]):
print("~~~ " + letter + " WON!!! CONGRATS!!!! ~~~")
quit()
elif(moves[2][0] == letter and
moves[2][1] == letter and
moves[2][2]):
print("~~~ " + letter + " WON!!! CONGRATS!!!! ~~~")
quit()
def playerInput():
player1 = input("Where do you want to place your X, player 1? (row number, space, number)")
moves[int(player1[0]) - 1][int(player1[2]) - 1] = "X"
player2 = input("Where do you want to place your O, player 2? (row number, space, number)")
moves[int(player2[0]) - 1][int(player2[2]) - 1] = "O"
boardDraw()
def boardDraw():
print("1| "+moves[0][0]+" | "+moves[0][1]+" | "+moves[0][2]+" |")
print(" |---+---+---|")
print("2| "+moves[1][0]+" | "+moves[1][1]+" | "+moves[1][2]+" |")
print(" |---+---+---|")
print("3| "+moves[2][0]+" | "+moves[2][1]+" | "+moves[2][2]+" |")
win("X")
win("O")
playerInput()
print("OK SO....\nPlayer 1 is X\nPlayer 2 is O\nGOOOOO!!")
boardDraw()
答案 0 :(得分:2)
您显然错过了2个获胜案例:
moves[0][0] == moves[1][1] == moves[2][2]
和:
moves[0][2] == moves[1][1] == moves[2][0]
我宁愿将您的获奖检测功能重写为:
def win(letter) :
for i in range(3) : # rows
if set(moves[i]) == set([letter]) :
print( 'WIN' )
quit()
for x in zip(moves[0], moves[1], moves[2]) : # columns
if set(x) == set([letter]) :
print( 'WIN' )
quit()
# you have completely missed the part below...
if set(moves[i][i] for i in range(3)) == set([letter]) : # one diagonal
print( 'WIN' )
quit()
if set(moves[i][3-i] for i in range(3)) == set([letter]) : # another diagonal
print( 'WIN' )
quit()
或更紧凑:
def win(letter) :
possible_wins = [ set(moves[i]) for i in rage(3) ] + # rows
[ set(x) for x in zip(moves[0], moves[1], moves[2]) ] + # columns
[ set(moves[i][i] for i in range(3)) ] + # diagonal
[ set(moves[i][3-i] for i in range(3)) ] # another diagonal
if any( p == set([letter]) for p in possible_wins ) :
print( 'WIN' )
quit()
答案 1 :(得分:2)
我假设您的问题的一部分与以下事实有关:在您的某些if
线索中,您检查前两个平方或等于letter
,而不检查第三个平方。除了None
或0
之类的内容外,Python中的大多数对象都将被评估为True
。因此,如果您的数组或字符中有一个非零数字,它将被评估为True
。这导致程序认为只有两件事组成一行时,玩家才赢了。
此外,您还有六个特定的获胜条件,而且肯定比您在井字游戏中获胜的条件还要多。我建议采用一种更合乎逻辑(且更具可读性)的方式将获胜方案整合在一起。例如,您可以在一个循环中检查所有水平和垂直获胜条件:
for i in range(3):
if ((moves[i][0] == moves[i][1] == moves[i][2] == letter) or
(moves[0][i] == moves[1][i] == moves[2][i] == letter):
# do win-condition stuff here
最后,我建议您检查无效举动,因为您当前的代码只会让用户覆盖现有举动。