gpt4 book ai didi

python - 我需要帮助制作 Tic Tac Toe 游戏。 Python

转载 作者:太空宇宙 更新时间:2023-11-03 14:16:10 24 4
gpt4 key购买 nike

我可以得到一些帮助来做出计算机的选择并完成这个程序,我仍然对我必须做什么感到困惑......也许最后有一个 while 循环与函数?我对如何做出计算机的选择感到非常困惑。到目前为止,玩家的选择、棋盘和 wincheck 可能都有效,我只需要其他所有东西都能工作,然后才能对其进行测试。由于某种原因,当我尝试在板上使用 1 时,它给了我一个值错误。

import random
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Computer = [1, 2, 3, 4, 5, 6, 7, 8, 9]
player = []
pc = []
wincheck = []
def WinCheck():
for i in range(0,9):
o = player.count[i]
if o == 1:
wincheck.append[i]
else:
continue

if wincheck == [1,2,3]:
print("You Win! The board will reset in 5 seconds!")
reset()
elif wincheck == [4,5,6]:
print("You Win! The board will reset in 5 seconds!")
reset()
elif wincheck == [7,8,9]:
print("You Win! The board will reset in 5 seconds!")
reset()
elif wincheck == [1,4,7]:
print("You Win! The board will reset in 5 seconds!")
reset()
elif wincheck == [2,5,8]:
print("You Win! The board will reset in 5 seconds!")
reset()
elif wincheck == [3,6,9]:
print("You Win! The board will reset in 5 seconds!")
reset()
elif wincheck == [1,5,9]:
print("You Win! The board will reset in 5 seconds!")
reset()
elif wincheck == [3,5,7]:
print("You Win! The board will reset in 5 seconds!")
reset()


def draw():

print ("\n" , "|",board[0],"|",board[1],"|",board[2], "|")
print ("\n", "--------")
print ("\n", "|",board[3],"|",board[4],"|",board[5], "|")
print ("\n", "--------")
print ("\n", "|", board[6],"|", board[7],"|", board[8], "|" , " \n")


#Resets the board
def reset():
while True:
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
time.sleep(5)
break

#Choosing a number from the board
def ChooseNum():
while 1 == 1:
while True:
print('Where do you want to place you X')
choice = input('')
try:
choice = int(choice)
except ValueError:
print("You didn't enter a number... Try again")
break

x = board.index(choice)
Computer.remove(x)
board.remove(choice)
board.insert(x,'X')
print(Computer)
draw()




def CompChoice():
while 1 == 1:
while True:
compchoice = random.choice(board)
o = board.index(compchoice)
Computer.remove(o)
board.remove(compchoice)
board.insert(o,'O')
print(Computer)
draw()

draw()
ChooseNum()

最佳答案

现在你有办法让计算机移动了,从我的评论中,注意这段代码:

Computer.remove(x)
board.remove(choice)

对于其中一个,您使用找到的索引x,对于另一个,您使用该值。

remove 删除“列表中值为 x 的第一个项目。如果没有这样的项目,则会发生错误。”如果它找不到您告诉它要查找的值,则会出现 ValueError

这给出了你的错误。您需要从列表中删除该选项:

Computer.remove(choice)
board.remove(choice)

您在计算机选择功能中也遇到类似的问题。为了让它和你一起玩井字棋,你需要做一些改变。为您指明方向:从函数中删除 while 循环,并使用类似这样的东西来播放:

draw()
while any(i not in ['X', 'O'] for i in board):
ChooseNum()
CompChoice()

如果您只有一个,您可能会发现事情会更容易 - 您试图在两个列表中维护相同的信息。您还需要检查移动是否合法 - 您不能去带有 'X''O' 的位置。最后,您可能想检查是否有人获胜。

关于python - 我需要帮助制作 Tic Tac Toe 游戏。 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48228619/

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