首先,我对编码还是新手,所以请不要因为我没有做正确的事情而责备我。代码中的所有内容都是我迄今为止从课堂上学到的。
所以我构建了一个井字游戏,正如标题所示,如果棋盘上填满了整行,则玩家二号的回合将被跳过。
代码主要部分:
print("This is Tic-Tac-Toe, play this with a friend")
TicTacToe = [["-", "-", "-"],
["-", "-", "-"],
["-", "-", "-"]]
for row in TicTacToe:
print(row[0],row[1],row[2])
while True:
Player = input("Which row do you want to go; T = top, M = middle, or B = bottom?\n")
if Player not in ["top", "Top", "T", "t", "middle", "Middle", "M", "m", "bottom", "Bottom", "B", "b"]:
print("Wrong input, I'm afraid you are going to have to actually write what you were told to.")
continue
# These are the moves of the player, starting with Player One.
if Player == "top" or Player == "Top" or Player == "T" or Player == "t":
while True:
Movement = input("Where do you want to go, L = left, M = middle or R = right?\n")
# Checks if the movement after the player chose their option is in check
if Movement not in ["left", "Left", "L", "l", "Middle", "middle", "M", "m", "right", "Right", "R", "r"]:
print("That is not one of the options you are allowed to write, please write the options listed.")
continue
if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":
if TicTacToe[0][0] == "-":
TicTacToe[0][0] = "X"
break
else:
print("A move has already been placed there!")
# Now it is the beginning of Player Two's turn
while True:
Player2 = input("Now it's your turn Player Two; T = top, M = middle, or B = bottom?\n")
# This is what happens if Player Two does not input the options originally listed
if Player2 not in ["top", "Top", "T", "t", "middle", "Middle", "M", "m", "bottom", "Bottom", "B", "b"]:
print("Wrong input, I'm afraid you are going to have to actually write what you were told to.")
continue
# Moves of Player Two
if Player2 == "top" or Player2 == "Top" or Player2 == "T" or Player2 == "t":
while True:
Movement = input("Where do you want to go, L = left, M = middle or R = right?\n")
# Checks if the movement after the player chose their option is in check
if Movement not in ["left", "Left", "L", "l", "Middle", "middle", "M", "m", "right", "Right", "R", "r"]:
print("That is not one of the options you are allowed to write, please write the options listed.")
continue
if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":
if TicTacToe[0][0] == "-":
TicTacToe[0][0] = "O"
break
else:
print("A move has already been placed there!")
break
因此,在玩家二号的代码块以及其他类似的代码块中,我尝试将 else 标签下的中断更改为 continue 语句,认为它会循环到原来的问题“现在轮到玩家二号了; T = 顶部,M = 中间,还是 B = 底部?”。
代码块:
if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":
if TicTacToe[0][0] == "-":
TicTacToe[0][0] = "O"
break
else:
print("A move has already been placed there!")
break
更改的代码块:
if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":
if TicTacToe[0][0] == "-":
TicTacToe[0][0] = "O"
break
else:
print("A move has already been placed there!")
continue
相反,它会循环回到名为“Movement”的变量,这使得玩家两个软锁定,因为该行已完全填满。
如果有人知道如何解决此问题,请同时解释为什么我的方法不起作用以及新方法如何工作。
您需要调整代码中的逻辑。在打印“移动已被放置在那里!”后,不要使用
continue
。对于玩家二,您可以简单地打破循环的当前迭代并允许玩家二进行另一次移动。
考虑以下内容(请记住,我不使用 python,我的代码中也可能存在问题):
if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":
if TicTacToe[0][0] == "-":
TicTacToe[0][0] = "O"
break
else:
print("A move has already been placed there!")
break # Break out of the current iteration to allow Player Two to make another move
使用
continue
无法按预期工作的原因是 continue
会跳过内循环中的剩余代码并继续该循环的下一次迭代。由于内循环中没有更多的迭代,因此不会达到预期的效果。
通过在打印消息后使用
break
,您可以退出内循环,从而允许再次提示玩家二进行移动。这样,控制流返回到外循环,玩家二可以再进行一次移动。