所以我开始了一个TicTacToe的项目,而且我还是一个初学者,所以我不知道该怎么做。这是半小时编码的结果。
我似乎在调试为什么Player2不转弯时遇到问题。
因此它要求添加更多详细信息,然后我再写几行,但仍然要求我添加更多行。
最后...
#Simple TicTacToe(Not so much)
#November 26, 2018
#1 is X, 2 is O
data = ["1","2","3","4","5","6","7","8","9"]
play = True
replay = False
chs = ""
ppl = 0
def ques():
global chs
global replay
a = int(input(f"{chs}:Postion(Classic Computer Number Pad Order, in number form,1-9): "))
if chs == "Player 1":
if data[a-1] != "X" and data[a-1] != "O":
data[a-1] = "X"
replay = False
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
elif chs == "Player 2":
if data[a-1] != "O" and data[a-1] != "X":
data[a-1] = "O"
replay = False
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
if chs == "Player 1" and replay == True:
chs = "Player 2"
elif chs == "Player 2" and replay == True:
chs = "Player 1"
def board():
print(str(data[6])+"|"+str(data[7])+"|"+str(data[8]))
print(str(data[3])+"|"+str(data[4])+"|"+str(data[5]))
print(str(data[0])+"|"+str(data[1])+"|"+str(data[2]))
def checkX():
if data[0] == "X" and data[1] == "X" and data[2] == "X":
print("Player 1 WON")
return True
elif data[3] == "X" and data[4] == "X" and data[5] == "X":
print("Player 1 WON")
return True
elif data[6] == "X" and data[7] == "X" and data[8] == "X":
print("Player 1 WON")
return True
elif data[0] == "X" and data[3] == "X" and data[6] == "X":
print("Player 1 WON")
return True
elif data[1] == "X" and data[4] == "X" and data[7] == "X":
print("Player 1 WON")
return True
elif data[2] == "X" and data[5] == "X" and data[8] == "X":
print("Player 1 WON")
return True
elif data[0] == "X" and data[4] == "X" and data[8] == "X":
print("Player 1 WON")
return True
elif data[2] == "X" and data[4] == "X" and data[6] == "X":
print("Player 1 WON")
return True
def checkO():
if data[0] == "O" and data[1] == "O" and data[2] == "O":
print("Player 2 WON")
return True
elif data[3] == "O" and data[4] == "O" and data[5] == "O":
print("Player 2 WON")
return True
elif data[6] == "O" and data[7] == "O" and data[8] == "O":
print("Player 2 WON")
return True
elif data[0] == "O" and data[3] == "O" and data[6] == "O":
print("Player 2 WON")
return True
elif data[1] == "O" and data[4] == "O" and data[7] == "O":
print("Player 2 WON")
return True
elif data[2] == "O" and data[5] == "O" and data[8] == "O":
print("Player 2 WON")
return True
elif data[0] == "O" and data[4] == "O" and data[8] == "O":
print("Player 2 WON")
return True
elif data[2] == "O" and data[4] == "O" and data[6] == "O":
print("Player 2 WON")
return True
def main():
global chs
player = input("Which shape go first?('x'for player1 or 'o'for player2): ")
if player == "x" or player == "X":
print("Player 1 will go first.")
chs = "Player 1"
elif player == "o" or player == "O":
print("Player 2 will go first.")
chs = "Player 2"
while play:
ques()
board()
if checkX():
break
if checkO():
break
print("\n"*10)
main()
答案 0 :(得分:0)
您永远不会告诉您的程序更换播放器。您可以通过在每转一圈之后改变chs
的值来解决该问题,例如:
if chs == "Player 1":
if data[a-1] != "X" and data[a-1] != "O":
data[a-1] = "X"
replay = False
chs = "Player 2"
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
elif chs == "Player 2":
if data[a-1] != "O" and data[a-1] != "X":
data[a-1] = "O"
replay = False
chs = "Player 1"
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
答案 1 :(得分:0)
您似乎要在此处更改第2节中的播放器。
但是在此代码的第1部分中,您将重播设置为false。然后它永远都不会变成第2节中的下一个玩家。
也许您打算在第2部分中检查“ play”变量?
# section 1
if chs == "Player 1":
if data[a-1] != "X" and data[a-1] != "O":
data[a-1] = "X"
replay = False # after making their turn replay is false.
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
elif chs == "Player 2":
if data[a-1] != "O" and data[a-1] != "X":
data[a-1] = "O"
replay = False
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
# section 2
if chs == "Player 1" and replay == True: # replay is checked here, but is false
chs = "Player 2" # never gets run
elif chs == "Player 2" and replay == True: # replay is checked again here, but is false once more
chs = "Player 1" # never gets run