为啥我不能运行程序直到游戏结束
Posted
技术标签:
【中文标题】为啥我不能运行程序直到游戏结束【英文标题】:Why I cant run the program until the game is over为什么我不能运行程序直到游戏结束 【发布时间】:2021-04-04 05:07:51 【问题描述】:任何人都可以告诉我我的代码有什么问题。
我尝试运行它并获得第一轮,然后程序停止 我尝试在 python 导师中运行它并看到他们玩后玩 去理解。 我不知道是什么问题 请睁开我的眼睛,引导我去寻找 如何解决这个问题
可能是循环出了问题,或者我的函数太多了 那些对其他函数的调用
我是初学者,我很想阅读并了解我需要改进的地方。
def winner_x(board):
i = 0 # i = index
x = ord('X')
x1 = 0
x2 = 0
x3 = 0
while i <= 2: # loop to scan the lists
if board[i].count('X') == 3:
return True
elif ord((board[i][0])) == x:
x1 += 1
if x1 == 3:
return True
elif ord((board[i][1])) == x:
x2 += 1
if x2 == 3:
return True
elif ord((board[i][2])) == x:
x3 += 1
if x3 == 3:
return True
if ord((board[0][0])) == x and ord(board[1][1]) == x and ord(board[2][2]) == x:
return True
if ord((board[0][2])) == x and ord(board[1][1]) == x and ord(board[2][0]) == x:
return True
i += 1
def winner_o(board):
i = 0 # i = index
o = ord('O')
o1 = 0
o2 = 0
o3 = 0
while i <= 2: # loop to scan the lists
if board[i].count('X') == 3:
return True
elif ord((board[i][0])) == o:
o1 += 1
if o1 == 3:
return True
elif ord((board[i][1])) == o:
o2 += 1
if o2 == 3:
return True
elif ord((board[i][2])) == o:
o3 += 1
if o3 == 3:
return True
if ord((board[0][0])) == o and ord(board[1][1]) == o and ord(board[2][2]) == o:
return True
if ord((board[0][2])) == o and ord(board[1][1]) == o and ord(board[2][0]) == o:
return True
i += 1
def draw(board):
if winner_o(board) != True and winner_x(board) != True or board.count('-') == 9:
return True
def checkboard(board):
if winner_x(board) == True:
return "The Winner Is X"
elif winner_o(board) == True:
return "The Winner Is O"
elif draw(board) == True:
return "Draw"
def check_player(board, player1): #function to place X/O and check if its possible
check = True
line = '-'
while check == True:
c1 = int(input("Player " + player1 + " Choose line (0-2,0-2):"))
c2 = int(input("Player " + player1 + " Choose column (0-2,0-2):"))
if 0 <= c1 <= 2 and 0 <= c2 <= 2:
if board[c1][c2] == line:
board[c1][c2] = player1
check = False
else:
check = True
else:
check = True
return board
def tic_tac_toe(board, player):#
counter = 0
while counter <= 9:
while draw(board) == True:
if counter % 2 == 0: # when its true the player O get start
player = 'O'
check_player(board, player)# check if the cell is empty or avilable
print(board[0])
print(board[1])
print(board[2])
counter += 1
elif counter % 3 == 0: # when its True player X can play is turn
player = 'X'
check_player(board, player)
print(board[0])
print(board[1])
print(board[2])
counter += 1
checkboard(board) #check the board if we have win or draw
board = [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]
player = input("Please Choose X/O: ")
tic_tac_toe(board, player)
【问题讨论】:
在你的第一行“def”之后你需要缩进吗? 请更正缩进;就目前而言,此代码不是有效的 Python。 ***.com/help/formatting 【参考方案1】:在 tic_tac_toe 函数中 你有
elif counter % 3 == 0: # when its True player X can play is turn
但你需要
elif counter % 2 == 1: # when its True player X can play is turn
【讨论】:
或者,事实上,else:
也可以。以上是关于为啥我不能运行程序直到游戏结束的主要内容,如果未能解决你的问题,请参考以下文章
为啥我把项目内的package名字改变后,程序就不能正常运行了呢