处理 tictactoe 的异常情况
handling outlier cases for tictactoe
我需要异常情况的帮助。我在下面提到了案例失败的地方。
possibleTurns = ("A1","A2","A3","B1","B2","B3","C1","C2","C3")
movesUsed = []
def main():
players = ["X","O"]
while len(movesUsed) <9:
#Player 1 Move
turnp1 = input(f"Choose a move Player 1: ")
print(f"Your move was: {turnp1}")
validMove = analyzeMove(turnp1)
move(validMove,players[0])
score()
movesUsed.append(validMove)
#Player 2 Move
turnp2 = input(f"Choose a move Player 2: ")
print(f"Your move was: {turnp2}")
validMove = analyzeMove(turnp2)
move(validMove,players[1])
score()
movesUsed.append(validMove)```
def analyzeMove(themove):
if themove.upper() in possibleTurns:
return themove
# This works but it switches the turn to player 1 even if its player 2 who makes a mistake.
if themove.upper() not in possibleTurns:
print("Invalid Move")
main()
# A used move will replace the old move so an X A1 will become O if O is chosen in error.
if themove.upper() in movesUsed:
print("That move was used!")
main()
问题是,当您递归调用 main()
时,它总是在轮到玩家 1 时开始。
不是在输入无效时递归,而是使用 while
循环不断请求输入,直到输入有效。 analyzeMove
可以 return False
表示移动无效,循环再次询问。
另一个错误是 analyzeMove()
在检查移动是否已被使用之前检查移动是否在 possibleTurns
中。
此外,不要复制玩家 1 和 2 的所有代码,而是使用一个变量来指示轮到谁。
possibleTurns = ("A1","A2","A3","B1","B2","B3","C1","C2","C3")
movesUsed = []
def main():
players = ["X","O"]
cur_player = 0
while len(movesUsed) < 9:
while True:
turnp1 = input(f"Choose a move Player {cur_player+1}: ")
print(f"Your move was: {turnp1}")
validMove = analyzeMove(turnp1)
if validMove:
break
move(validMove,players[cur_player])
score()
movesUsed.append(validMove)
cur_player = 1 if cur_player == 0 else 0
def analyzeMove(themove):
if themove.upper() in movesUsed:
print("That move was used!")
return False
if themove.upper() in possibleTurns:
return themove
else:
print("Invalid Move")
return False
我需要异常情况的帮助。我在下面提到了案例失败的地方。
possibleTurns = ("A1","A2","A3","B1","B2","B3","C1","C2","C3")
movesUsed = []
def main():
players = ["X","O"]
while len(movesUsed) <9:
#Player 1 Move
turnp1 = input(f"Choose a move Player 1: ")
print(f"Your move was: {turnp1}")
validMove = analyzeMove(turnp1)
move(validMove,players[0])
score()
movesUsed.append(validMove)
#Player 2 Move
turnp2 = input(f"Choose a move Player 2: ")
print(f"Your move was: {turnp2}")
validMove = analyzeMove(turnp2)
move(validMove,players[1])
score()
movesUsed.append(validMove)```
def analyzeMove(themove):
if themove.upper() in possibleTurns:
return themove
# This works but it switches the turn to player 1 even if its player 2 who makes a mistake.
if themove.upper() not in possibleTurns:
print("Invalid Move")
main()
# A used move will replace the old move so an X A1 will become O if O is chosen in error.
if themove.upper() in movesUsed:
print("That move was used!")
main()
问题是,当您递归调用 main()
时,它总是在轮到玩家 1 时开始。
不是在输入无效时递归,而是使用 while
循环不断请求输入,直到输入有效。 analyzeMove
可以 return False
表示移动无效,循环再次询问。
另一个错误是 analyzeMove()
在检查移动是否已被使用之前检查移动是否在 possibleTurns
中。
此外,不要复制玩家 1 和 2 的所有代码,而是使用一个变量来指示轮到谁。
possibleTurns = ("A1","A2","A3","B1","B2","B3","C1","C2","C3")
movesUsed = []
def main():
players = ["X","O"]
cur_player = 0
while len(movesUsed) < 9:
while True:
turnp1 = input(f"Choose a move Player {cur_player+1}: ")
print(f"Your move was: {turnp1}")
validMove = analyzeMove(turnp1)
if validMove:
break
move(validMove,players[cur_player])
score()
movesUsed.append(validMove)
cur_player = 1 if cur_player == 0 else 0
def analyzeMove(themove):
if themove.upper() in movesUsed:
print("That move was used!")
return False
if themove.upper() in possibleTurns:
return themove
else:
print("Invalid Move")
return False