我正在尝试学习python并且已经编写了一个(非常)简单的tic-tac-toe程序。但是,我已经陷入困境,因为除了让用户输入数字之外,它似乎不会执行。之后,没有任何反应。我无法弄清楚我哪里出错了。当然有很多错误,但非常感谢!
谢谢!
import random
board = range(0,9)
def print_board():
print board[0], "|", board[1], "|", board[2]
print board[3], "|", board[4], "|", board[5]
print board[6], "|", board[7], "|", board[8]
def checkAll():
if board[0] == board[1] == board[2]:
True
if board[3] == board[4] == board[5]:
True
if board[6] == board[7] == board[8]:
True
if board[0] == board[3] == board[6]:
True
if board[1] == board[4] == board[7]:
True
if board[2] == board[5] == board[8]:
True
if board[0] == board[4] == board[8]:
True
if board[6] == board[4] == board[2]:
True
print_board()
while True:
input = int(raw_input("Choose a number to place your X: "))
if input <= 8:
if board[input] != "x" or board[input] != "o":
board[input] = "x" # places x if board[input] is neither x or o
# Check for winner
if checkAll() == True:
"The game is over!"
break;
finding = True
while finding:
random.seed() # gives a random generator
opponent = random.randrange(0,8) # generates a random integer between 1 and 8
if board[opponent] != "x" or board[opponent] != "o":
board[opponent] = "o"
# Check for winner
if checkAll() == True:
"The game is over!"
break;
else:
print "This spot is taken."
print_board()
else: "Please choose a number between O and 8."
答案 0 :(得分:0)
有很多东西需要改进你的代码(你必须重写你的CheckAll函数,至少它可以检查任何大小的板),但有两件事可以帮助你调试 - 首先,你必须实际从CheckAll函数返回一些内容:
...
if board[0] == board[1] == board[2]:
return True
if board[3] == board[4] == board[5]:
return True
...
第二,你可能真的想打印输出:
if checkAll() == True:
print "The game is over!"
break;