gpt4 book ai didi

python - 如何找到领带游戏python

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:38 25 4
gpt4 key购买 nike

您好,我正在制作这款井字游戏,我看了一些教程,但没有任何游戏可以以平局结束关于我的芬兰语变量和评论

import random

board = [0,1,2,
3,4,5,
6,7,8]

def show():
print board[0], '|',board[1],'|',board[2]
print '----------'
print board[3], '|',board[4],'|',board[5]
print '----------'
print board[6], '|',board[7],'|',board[8]

def checkLine(char, spot1, spot2, spot3):
if (board[spot1] == char) and (board[spot2] == char) and (board [spot3] == char) :
return True
else:
return False

def checkAll(char):
ret = False
if checkLine(char, 0, 1, 2):
ret = True
if checkLine(char, 0,3, 6):
ret = True
if checkLine(char, 1, 4, 7):
ret = True
if checkLine(char, 2, 5, 8):
ret = True
if checkLine(char, 6, 7, 8):
ret = True
if checkLine(char, 3, 4, 5):
ret = True
if checkLine(char, 2, 4, 6):
ret = True
if checkLine(char, 0, 4, 8):
ret = True
return ret

moves = range(9)
numindex = 1
ratkennut = False
while moves:
show()

input = raw_input("Put x: ")
try:
val = int(input)
input = int(input)
except ValueError:
print("Input number!")
input = raw_input("Put x: ")
input = int(input)
if input in moves:
moves.remove(input)

if board [input] != 'x' and board[input] != 'o':
board[input] = 'x'

if checkAll('x') == True:
print "~~ X Won ~~"
ratkennut = True
break;

while moves:
random.seed() #Gives opponents move
opponent = random.choice(moves)
moves.remove(opponent)

if board[opponent] != 'o' and board[opponent] != 'x':
board[opponent] = 'o'

if checkAll('o') == True:
print "~~ O Won ~~"
ratkennut = True
break;
else:
print 'This spot is taken'
else:
print "Tie!"

问题:当游戏以 tie game 结束时,这段代码有什么问题它卡住了,我需要 ctrl + c 如何让它找到 tie game 并打印“tie game”我对其进行了编辑,现在效果非常好!

最佳答案

您在 while 循环中为对手选择的 randint 移动选择可能会无限期地运行,尤其是当剩余的有效移动数变少时。取而代之的是,制作一个有效 Action 列表,list.remove() 中的每个 Action :

moves = range(9) 

这简化了用户的移动:

if input in moves:
moves.remove(input)

对手的举动:

opponent = random.choice(moves)
moves.remove(opponent)

并确定游戏结束:

while moves:
...
else:
print "It's a tie."

关于python - 如何找到领带游戏python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22113548/

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