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