def DrawField(field):
for row in range(5):
if row%2 == 0:
practicalrow = int(row/2)
for column in range(5):
if column%2 == 0:
practicalcolumn = int(column/2)
if column !=4:
print(field[practicalcolumn][practicalrow],end="")
else:
print(field[practicalcolumn][practicalrow])
else:
print("|",end="")
else:
print("-----")
Player = 1
CurrentField = [[" ", " ", " "],[" ", " ", " "],[" ", " ", " "]]
print(CurrentField)
DrawField(CurrentField)
while(True):
print("Players Turn:", Player)
MoveRow = int(input("PLease enter the row\n"))
MoveCol = int(input("PLeasae enter the column\n"))
if Player == 1:
if CurrentField[MoveCol][MoveRow] == " ":
CurrentField[MoveCol][MoveRow] = "X"
#make move for player 1
Player = 2
else:
continue
else:
if CurrentField[MoveCol][MoveRow] == " ":
CurrentField[MoveCol][MoveRow] = "O"
#make move for player 2
Player = 1
else:
continue
DrawField(CurrentField)
所以,这里是gtic tictactoe游戏的代码,我有一个错误,玩家可以重写彼此的动作。我最终解决了这个问题,但是另一个错误是,一旦玩家获胜,循环就不会中断。同样,如果有一种方法可以要求玩家获胜后要求重播一些琐碎的信息:currentField列表的每个子列表中的空格分别引用游戏板的每一列。
我无法告诉您如何验证一方胜出,如果您做到了,我会假设您会这样做,因为那不是您要问的话题。
关于中断while
循环的问题,可以使用break
,例如这样:
while True:
if has_won == true:
break
####other code
如果if语句是while循环中的第一个检查,它在开始时就很重要,因为如果在结束时将进行回合,而检查游戏是否结束前回合则只会发生。
break
可以用于while
和for
循环,因为它刚好在循环点外中断