python - 为什么在尝试重新启动井字游戏时出现此错误?
问题描述
我是第一学期的学生,我正在尝试重新开始我的游戏。问题是我真的不知道即使游戏运行良好。我认为解决方案是再次调用函数 play() 但对于我的生活我卡住了!
尝试创建一个 while 循环以尝试在仍在玩的同时循环游戏
while playing == True:
play()
continue_play = input("Continue playing? (y/n)")
if continue_play.lower() == "n":
playing = False
else:
new_game = play()
show_board()
new_game()
这是while循环......新游戏返回'nonetype'不可调用......下面是我整个游戏的实际代码:
playing = True
#Global variables to be used
game_active = True
champion = None
active_player = " X "
#Displays board
def show_board():
print(" 1 2 3")
print("1 " + board[0] + " | " + board[1] + " | " + board[2])
print(" ----+-----+----")
print("2 " + board[3] + " | " + board[4] + " | " + board[5])
print(" ----+-----+----")
print("3 " + board[6] + " | " + board[7] + " | " + board[8])
#play game of TIC TAC TOE
def play():
global board
board = [" ", " ", " ",
" ", " ", " ",
" ", " ", " "]
#display the game board to users
show_board()
#while game is still active
while game_active:
#Whos turn is it?
turns(active_player)
#check if game has met finishing requirements
check_game_done()
#Change player turn
change_player()
#game has ended
if champion == " X " or champion == " O ":
print(champion + " Is the winner!")
elif champion == None:
print(" Draw ")
#function for handling player turns
def turns(player):
print(player + "'s Turn.")
player_position = input("Please choose a number for your move between 1-9: ")
ok = False
while not ok:
while player_position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
player_position = input(" Please choose a number for your move between 1-9: ")
player_position = int(player_position) - 1
if board[player_position] == " ":
ok = True
else:
print("Sorry this spot is taken.")
board[player_position] = player
show_board()
#function to check over game to see if game completed
def check_game_done():
check_win()
check_draw()
def check_win():
#using global variable from above code
global champion
#rows
winning_row = check_rows()
#columns
winning_column = check_columns()
#diagonals
winning_diagonal = check_diagonals()
if winning_row:
#winner found
champion = winning_row
elif winning_column:
#winner found
champion = winning_column
elif winning_diagonal:
#winner found
champion = winning_diagonal
else:
#no winner found
champion = None
def check_rows():
#call global variable to check if game still active
global game_active
#check rows for winning condition
row1 = board[0] == board[1] == board[2] != " "
row2 = board[3] == board[4] == board[5] != " "
row3 = board[6] == board[7] == board[8] != " "
#winning conditions met?
if row1 or row2 or row3:
game_active = False
#who won?
if row1:
return board[0]
elif row2:
return board[3]
elif row3:
return board[6]
else:
return None
def check_columns():
#call global variable to check if game still active
global game_active
#check columns for winning condition
column1 = board[0] == board[3] == board[6] != " "
column2 = board[1] == board[4] == board[7] != " "
column3 = board[2] == board[5] == board[8] != " "
#Win conditions met
if column1 or column2 or column3:
game_active = False
#who won
if column1:
return board[0]
elif column2:
return board[1]
elif column3:
return board[3]
else:
return None
def check_diagonals():
#call global variable to check if game still active
global game_active
#check diagonals for winning condition
diagonal_1 = board[0] == board[4] == board[8] != " "
diagonal_2 = board[2] == board[4] == board[6] != " "
#win conditon met
if diagonal_1 or diagonal_2:
game_active = False
#who won
if diagonal_1:
return board[0]
elif diagonal_2:
return board[2]
else:
return None
# Checking to see if conditions for a draw have been met
def check_draw():
global game_active
if " " not in board:
game_active = False
return True
else:
return False
# Using function to decide whos turn it is
def change_player():
global active_player
if active_player == " X ":
active_player = " O "
elif active_player == " O ":
active_player = " X "
return None
# Restarting game again
while playing == True:
play()
continue_play = input("Continue playing? (y/n)")
if continue_play.lower() == "n":
playing = False
else:
new_game = play()
show_board()
new_game()
play()
解决方案
将 while 循环更改为:
...
while playing == True:
play()
continue_play = input("Continue playing? (y/n)")
if continue_play.lower() == "n":
playing = False
else:
game_active = True
champion = None
play()
show_board()
...
现在它将起作用。
推荐阅读
- python - 熊猫:添加百分比列
- karate - 空手道-加特林:无法基于标签运行场景
- excel - 在 Excel Addin Nodejs Api 中获取单元格的最终非公式先例
- elasticsearch - 如何按运行时计算的价格(低/高)排序
- angular - 单元测试用例随机失败,Angular 应用程序从 Angular 7 转换为 11
- python - 当使用 conda-build 构建 conda 包并且我的代码使用纯 python 库时,我需要在 meta.yaml 文件中的 build/host/run 中放入什么?
- python - sympy 的 ConditionSet 对象不可迭代
- python - 每次我从一个环境跳转到另一个环境时,VS Code 都会启动 python 进程,每个人都会消耗大约 100% 的 CPU
- javascript - 如何在JS中构造多个包之间的导出和导入
- android - 在此代码中,上下文不需要工作:找到上下文:上下文?