繁体   English   中英

如何让我的 2 bot 玩家井字游戏程序确定是否为平局?

[英]How can I make my 2 bot player Tic Tac Toe program determine whether it is a draw?

我正在尝试制作一个由两个机器人玩的井字游戏,井字游戏完美运行并向我展示了谁赢了,但是当他们平局时它会中断,我尝试了不同的解决方案来尝试修复它,但我不知道它是如何完成的。 基本上,机器人从 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:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM