python - 我的代码应该有一个(非常糟糕的)AI 将他的井字棋 O 放在棋盘上的随机位置

标签 python while-loop

但是在我赢得比赛后,(我每次都让计算机打印数字)计算机不断地一遍又一遍地挑选数字,试图获得一个位置,即使已经没有了。我不知道该怎么办,非常感谢任何帮助。

if board[computer] != 'X' and board[computer] != 'O':
        print computer
        board[computer] = 'O'
    else:
        while board[computer] == 'O' or board[computer] == 'X':
            computer = random.randint(0, 8)
            print computer
            if board[computer] != 'X' and board[computer] != 'O':
                board[computer] = 'O'
                break

最佳答案

你需要看看你的逻辑......

你的 while 说...board[computer] == 'O' 或 board[computer] == 'X'

以及你的break if语句:

if board[computer] != 'X' and board[computer] != 'O':

都不检查板是否已满。

您需要循环浏览“板”并检查它是否已满

例如...

emptyposition = True
for position in board :
    if not position == Null : # Your board position is empty 
        emptyposition = False


if not emptyposition :
    break

关于python - 我的代码应该有一个(非常糟糕的)AI 将他的井字棋 O 放在棋盘上的随机位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618000/

相关文章:

c++ - 如何解决这个奇怪的 while 循环错误?

python - 按计划在 Heroku 上运行 Python 脚本(作为单独的应用程序)

numpy - 从 txt 文件计算平均值、标准差的有效方法

python - 如何在 python 中找到列表的 N 个最大值的位置?

java - 循环只运行两次

while循环的语法可以是一个在开始时没有赋值的变量吗?

python - 如何在抓取网页时单击下一步按钮

javascript - Flask 找不到带有 webpack 的静态文件

javascript - 如何用递归函数替换while?

c - main{} 括号上的语法错误, "expected while"?