python - 如何让我的 2 bot 玩家井字游戏程序确定是否为平局?
问题描述
我正在尝试制作一个由两个机器人玩的井字游戏,井字游戏完美运行并向我展示了谁赢了,但是当他们平局时它会中断,我尝试了不同的解决方案来尝试修复它,但我不知道它是如何完成的。基本上,机器人从 1-9 的列表中选择一个随机数,该数字对应于棋盘上的一个位置,然后变成“O”或“X”。如果他们连续获得三个,他们就赢了。对于每一步,随机生成的数字将从列表中扣除,这将使另一个机器人选择一个尚未使用的数字,反之亦然。这里的问题是当游戏平局(列表变为空)时,我不知道如何停止它并提示用户显示平局或 X/O 获胜的消息。
这是程序:
import random,time
xoro = ["X","O"]
line1 = [1,2,3]
line2 = [4,5,6]
line3 = [7,8,9]
pause = False
starter = ""
botlist = [1,2,3,4,5,6,7,8,9]
def displayboard():
print(" ")
print(" ")
print(line1)
print(line2)
print(line3)
def xbot():
botnumber = random.choice(botlist)
if botnumber in line1:
for n,i in enumerate(line1):
if botnumber == i:
line1[n] = 'X'
botlist.remove(i)
return("X")
elif botnumber in line2:
for n,i in enumerate(line2):
if botnumber == i:
line2[n] = 'X'
botlist.remove(i)
return "X"
elif botnumber in line3:
for n,i in enumerate(line3):
if botnumber == i:
line3[n] = 'X'
botlist.remove(i)
return "X"
def obot():
botnumber = random.choice(botlist)
if botnumber in line1:
for n,i in enumerate(line1):
if botnumber == i:
line1[n] = 'O'
botlist.remove(i)
return("O")
elif botnumber in line2:
for n,i in enumerate(line2):
if botnumber == i:
line2[n] = 'O'
botlist.remove(i)
return "O"
elif botnumber in line3:
for n,i in enumerate(line3):
if botnumber == i:
line3[n] = 'O'
botlist.remove(i)
return "O"
def checkwin():
if line1[0] == "X" and line1[1] == "X" and line1[2] == "X":
return "X"
pause = True
elif line2[0] == "X" and line2[1] == "X" and line2[2] == "X":
return "X"
pause = True
elif line3[0] == "X" and line3[1] == "X" and line3[2] == "X":
return "X"
pause = True
elif line1[0] == "X" and line2[0] == "X" and line3[0] == "X":
return "X"
pause = True
elif line1[1] == "X" and line2[1] == "X" and line3[1] == "X":
return "X"
pause = True
elif line1[2] == "X" and line2[2] == "X" and line3[2] == "X":
return "X"
pause = True
elif line1[0] == "X" and line2[1] == "X" and line3[2] == "X":
return "X"
pause = True
elif line1[2] == "X" and line2[1] == "X" and line3[0] == "X":
return "X"
pause = True
elif line1[0] == "O" and line1[1] == "O" and line1[2] == "O":
return "O"
pause = True
elif line2[0] == "O" and line2[1] == "O" and line2[2] == "O":
return "O"
pause = True
elif line3[0] == "O" and line3[1] == "O" and line3[2] == "O":
return "O"
pause = True
elif line1[0] == "O" and line2[0] == "O" and line3[0] == "O":
return "O"
pause = True
elif line1[1] == "O" and line2[1] == "O" and line3[1] == "O":
return "O"
pause = True
elif line1[2] == "O" and line2[2] == "O" and line3[2] == "O":
return "O"
pause = True
elif line1[0] == "O" and line2[1] == "O" and line3[2] == "O":
return "O"
pause = True
elif line1[2] == "O" and line2[1] == "O" and line3[0] == "O":
return "O"
pause = True
def checkdraw():
if not botlist:
return True
else:
return False
returned = ""
def start():
return(random.choice(xoro))
winner = checkwin()
draw = checkdraw()
while (winner == None) or (draw == False):
displayboard()
if returned == "X":
returned = obot()
else:
returned = xbot()
winner = checkwin()
draw = checkdraw()
displayboard()
if winner == "X":
print(f"Winner is {winner}")
elif winner == "O":
print(f"Winner is {winner}")
else:
print(f"Game draw? {draw}")
主要问题就在最后。
非常感谢您的阅读。
解决方案
问题是你的while
循环条件。
它不应该是一个,or
而是一个and
。当这两个条件都成立时(即没有赢家,也不是平局),您想继续循环,并在其中任何一个条件不成立时退出(即有赢家或平局):
while winner is None and not draw:
推荐阅读
- python - 如何使用'select_related'从查询集中的多个模型中获取数据?
- c# - GetPreSignedUrlRequest 可以只使用部分密钥吗?
- docker - Docker 上的 Hadoop 集群。Zookeeper 启动服务命令不能从外部容器工作,但可以从内部工作
- mysql - 按值更新 MySql 表
- java - 如何修复 getPostalCode-null?
- python - 如何从没有 None 字段的类创建字典?
- python - 从 __init__.py 导入时出现 Python 导入错误
- python - 在 Django 2.2 中为 GRAPHENE 设置“SCHEMA”导入模块时出现问题
- python - 如何让 Flask 进行很长时间的操作?
- php - 将 JSON 对象转换为 PHP 数组时出现问题