gpt4 book ai didi

Python 有问题的 TicTacToe 棋盘

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:28 28 4
gpt4 key购买 nike

所以我开始了一个 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()

最佳答案

看来您正在尝试更改第 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

关于Python 有问题的 TicTacToe 棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53491448/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com