初学者井字游戏,结果问题(程序不在乎第一个条件)


board = [["___", "___", "___"],
["___", "___", "___"],
["___", "___", "___"]]
winning_possibilities = [[[0, 0], [1, 0], [2, 0]],
[[0, 1], [1, 1], [2, 1]],
[[0, 2], [1, 2], [2, 2]],
[[0, 0], [0, 1], [0, 2]],
[[1, 0], [1, 1], [1, 2]],
[[2, 0], [2, 1], [2, 2]],
[[0, 0], [1, 1], [2, 2]],
[[0, 2], [1, 1], [2, 0]]]
status_X = []
status_O = []
turnX = "X".center(3)
turnY = "O".center(3)
turn = 2
control = 1
counter = 0
tempCounter = 0
import random
print("n"*10)
for i in board:
print(" "*28, *i, sep=" "*3, end="n"*3)
while control == 1:
try:
for i in winning_possibilities:
win_O = [z for z in i if z in status_O]
win_X = [z for z in i if z in status_X]
if counter > 4 and (len(win_O) == len(i) or (counter == 9 and len(win_O) == len(i))):
print("O won, the game is over!" + "n"*2)
control = 0
status_X = []
status_O = []
board = [["___", "___", "___"],
["___", "___", "___"],
["___", "___", "___"]]
turn = 2
counter = 0
restart = str(input("To restart the game please press R.n")).lower()
if restart == "r":
print("n"*10)
for i in board:
print(" "*28, *i, sep=" "*3, end="n"*3)
control = 1
if len(win_X) == len(i) or (counter == 9 and len(win_X) == len(i)):
print("X won, the game is over!" + "n"*2)
control = 0
status_X = []
status_O = []
board = [["___", "___", "___"],
["___", "___", "___"],
["___", "___", "___"]]
turn = 1
counter = 0
restart = str(input("To restart the game please enter R.n")).lower()
if restart == "r":
print("n"*10)
for i in board:
print(" "*28, *i, sep=" "*3, end="n"*3)
control = 1
if turn % 2 == 0:
whoseTurn = turnX
else:
whoseTurn = turnY
if control == 1:
if whoseTurn == turnX:
if counter > 0:
print("n"*2 + "Your opponent played their turn, now it's your turn!" + "n")
else:
pass
coordinateX = int(input("Choose a line from top to bottom to put {}. (1, 2, 3): ".format(whoseTurn)))
if coordinateX > 0 and coordinateX < 4:
coordinateX -= 1
coordinateY = int(input("Choose a column from left to right to put {}. (1, 2, 3): ".format(whoseTurn)))
if coordinateY > 0 and coordinateY < 4:
coordinateY -= 1
if board[coordinateX][coordinateY] == "___":
board[coordinateX][coordinateY] = whoseTurn
status_X += [[coordinateX, coordinateY]]
counter += tempCounter
counter += 1
tempCounter = 0
turn += 1
print("n"*5)
for i in board:
print(" "*28, *i, sep=" "*3, end="n"*3)
else:
tempCounter += counter
counter = 0
print("n"*2 + "This spot is filled already! Please pick an empty spot!" +"n"*2)
else:
print("n"*5)
for i in board:
print(" "*28, *i, sep=" "*3, end="n"*3)
print("n"*2 + "Please just enter one of the numbers in the specified range." +"n"*2)
elif whoseTurn == turnY:
randomX = random.randint(0,2)
randomY = random.randint(0,2)
if board[randomX][randomY] == "___":
board[randomX][randomY] = whoseTurn
status_O += [[randomX, randomY]]
print("n"*2 + "Now is your opponent's turn, wait him/her to play!" + "n"*2)
counter += 1
turn +=1
print("n"*5)
for i in board:
print(" "*28, *i, sep=" "*3, end="n"*3)
else:
randomX = random.randint(0,2)
randomY = random.randint(0,2)
for i in winning_possibilities:
if (len(win_O) != len(i) or len(win_X) != len(i)) and counter == 9:
print("Draw!" + "n"*2)
control = 0
status_X = []
status_O = []
board = [["___", "___", "___"],
["___", "___", "___"],
["___", "___", "___"]]
counter = 0
restart = str(input("To restart the game please enter R.n")).lower()
if restart == "r":
print("n"*10)
for i in board:
print(" "*28, *i, sep=" "*3, end="n"*3)
control = 1
except ValueError:
tempCounter += counter
counter = 0
print("n"*2 + "Please just enter one of the numbers in the specified range." +"n"*2)
except IndexError:
continue
except NameError:
pass

我是Python的初学者,我正在练习。我发现了一个井字棋游戏代码,并试图理解开发者做了什么,在我理解之后,我开发了这款游戏,甚至改进了它。但是有一个bug我无法修复,也无法理解为什么会发生。

我有一个计数器变量,它每回合增加+1。当数字变为9时,游戏结果意味着平局。但也有一种情况,如果玩家在第9轮赢得游戏,游戏结果不应该是平局,所以我写了这个代码:

if (len(win_O) != len(i) or len(win_X) != len(i)) and counter == 9:

代替这个:

if counter == 9:

但是仍然没有固定,程序不关心条件的第一部分。我希望有人能解决这个问题。谢谢你。

问题是变量win_Owin_x超出了范围。因为您首先在try块中定义了它们,所以在该代码块之外无法访问它们。所以在:

if (len(win_O) != len(i) or len(win_X) != len(i)) and counter == 9:

win_Owin_x未定义。尝试在代码中更早地定义win_Owin_x,可能在第15行status_O = []之后:

status_X = []
status_O = []
win_0 = []
win_x = []

相关内容

  • 没有找到相关文章

最新更新