gpt4 book ai didi

python - 我的代码不会检查 X、Y 和 Z 是否在列表中,但如果仅覆盖其中之一,则会进行检查。我究竟做错了什么?

转载 作者:行者123 更新时间:2023-11-30 21:50:54 26 4
gpt4 key购买 nike

我正在制作一个 Tic-Tac-Toe 游戏,我正在尝试制作一个函数来检查玩家是否获胜。

这是我的功能:

def win_check(playerpicks, player):
#HORIZONTAL CHECK
if 1 and 2 and 3 in playerpicks or 4 and 5 and 6 in playerpicks or 7 and 8 and 9 in playerpicks:
print("Player " + str(player) + " Wins!")

#Vertical Check
elif 1 and 4 and 7 in playerpicks or 2 and 5 and 8 in playerpicks or 3 and 6 and 9 in playerpicks:
print("Player " + str(player) +" Wins!")

#Diagonal Check
elif 1 and 5 and 9 in playerpicks or 3 and 5 and 7 in playerpicks:
print("Player " + str(player) +" Wins!")

该板将可以通过数字键盘引用。

这是我的游戏流程,其中使用了获胜检查:

def flow():

global turn

if turn == 1:
position1 = input("P{} choose your position (1-9)".format(turn))

if 9 >= int(position1) >= 1:
if int(position1) in p1_list or int(position1) in p2_list:
print("Spot Taken")
else:
p1_list.append(int(position1))
win_check(p1_list, 1)
turn = 2
print(p1_list)
else:
print("INVALID INPUT")

elif turn == 2:
position2 = input("P{} choose your position (1-9)".format(turn))

if 9 >= int(position2) >= 1:
if int(position2) in p2_list or int(position2) in p1_list:
print("Spot Taken")
else:
p2_list.append(int(position2))
win_check(p2_list, 2)
turn = 1
print(p2_list)
else:
print("INVALID INPUT")

我知道我的代码可能过于复杂,这就是它无法正常工作的原因。但距离我写一行代码已经过去几个月了,我正在努力刷新我的大脑。

这是我得到的输出:

Player 1 or player 2? (Enter 1 or 2)1
You are Player 1
P1 choose your position (1-9)2
[2]
P2 choose your position (1-9)3
Player 2 Wins!
[3]
P1 choose your position (1-9)

有史以来最简单的游戏吧?我很困惑,因为 and 没有做我认为它应该做的事情。有什么帮助吗?

最佳答案

您需要将条件更改为如下所示:

if (1 in playerpicks and 2 in playerpicks and 3 in playerpicks) or \
(4 in playerpicks and 5 in playerpicks and 6 in playerpicks) or \
(7 in playerpicks and 8 in playerpicks and 9 in playerpicks):
print("Player " + str(player) + " Wins!")

关于python - 我的代码不会检查 X、Y 和 Z 是否在列表中,但如果仅覆盖其中之一,则会进行检查。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60255724/

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