我做了一个功能,在我的井字游戏中的每一个动作之后都会改变玩家,但似乎不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我做了一个功能,在我的井字游戏中的每一个动作之后都会改变玩家,但似乎不起作用相关的知识,希望对你有一定的参考价值。
因此,我制作了一个井字游戏,并制作了一个名为marker_change()
的函数,以在每次移动后更改玩家,但是当我开始游戏时,标记不变,每次移动后标记都保持不变。这是我的代码,对不起,如果我的代码很烂,但是我确实尝试了很多事情来解决这个问题:
board = [" "]*10
marker = None
def display_board(board):
print("""
▀█▀ █ █▀▀ ▀█▀ ▄▀█ █▀▀ ▀█▀ █▀█ █▀▀
░█░ █ █▄▄ ░█░ █▀█ █▄▄ ░█░ █▄█ ██▄
█▀▀ ▄▀█ █▀▄▀█ █▀▀
█▄█ █▀█ █░▀░█ ██▄""")
print("\n"*1)
print(board[7]+"|"+board[8]+"|"+board[9])
print("-----")
print(board[4]+"|"+board[5]+"|"+board[6])
print("-----")
print(board[1]+"|"+board[2]+"|"+board[3])
def place_marker(board):
global marker
marker = " "
while marker != "x" and marker != "o":
marker = input("Player 1, choose x or o: ")
player1 = marker
if player1 == "x":
player2 = "o"
print("""
|------------------------------------|
| Player 1 is now X and player 2 is O|
|------------------------------------|
""")
else:
player2 = "x"
print("""
|------------------------------------|
| Player 1 is now O and player 2 is X|
|------------------------------------|
""")
#Input of the player
count = 0
for a in range(10):
count += 1
display_board(board)
position = int(input("Choose a number between 1-9: "))
board[position] = marker
if board[1] == board[2] == board[3] != ' ':
win_check = str(board[1] + " HAS WON !!!")
print(win_check)
break
elif board[1] == board[5] == board[9] != ' ':
variabila1 = str(board[1] + " HAS WON !!!")
print(variabila1)
break
elif board[7] == board[8] == board[9] != ' ':
variabila2 = str(board[7] + " HAS WON !!!")
print(variabila2)
break
elif board[7] == board[5] == board[3] != ' ':
variabila3 = str(board[7] + " HAS WON !!!")
print(variabila3)
break
elif board[1] == board[4] == board[7] != ' ':
variabila4 = str(board[1] + " HAS WON !!!")
print(variabila4)
break
elif board[3] == board[6] == board[9] != ' ':
variabila5 = str(board[3] + " HAS WON !!!")
print(variabila5)
break
elif count == 9:
print("THIS IS A TIE")
return(a)
return (player1,player2)
place_marker(board)
def marker_change():
global marker
if marker == "x":
marker = "o"
print("Now it's o's turn")
else:
print("Now it's x's turn")
marker_change()
#restart function
def clear_board(board):
restart = input("Would you like to play again?:\nyes/no: ")
if restart == "yes":
board = [" "]*10
place_marker(board)
clear_board(board)
else:
restart == "no"
print("\n"*100)
print("""
▀▀█▀▀ █──█ █▀▀█ █▀▀▄ █─█ █▀▀ ░█▀▀▀ █▀▀█ █▀▀█ ░█▀▀█ █── █▀▀█ █──█ ─▀─ █▀▀▄ █▀▀▀
─░█── █▀▀█ █▄▄█ █──█ █▀▄ ▀▀█ ░█▀▀▀ █──█ █▄▄▀ ░█▄▄█ █── █▄▄█ █▄▄█ ▀█▀ █──█ █─▀█
─░█── ▀──▀ ▀──▀ ▀──▀ ▀─▀ ▀▀▀ ░█─── ▀▀▀▀ ▀─▀▀ ░█─── ▀▀▀ ▀──▀ ▄▄▄█ ▀▀▀ ▀──▀ ▀▀▀▀""")
if __name__ == "__main__":
clear_board(board)
答案
您在游戏结束时呼叫marker_change()
,而不是在每回合之后。您应该在if之后调用它,在这里检查是否有人赢了。
另一答案
这是基于您的原始代码的简单重构。
显示函数参数而不是全局变量的用法。
def display_board(board):
# changed numbering to 1 to 9 starting with first row
print("""
▀█▀ █ █▀▀ ▀█▀ ▄▀█ █▀▀ ▀█▀ █▀█ █▀▀
░█░ █ █▄▄ ░█░ █▀█ █▄▄ ░█░ █▄█ ██▄
█▀▀ ▄▀█ █▀▄▀█ █▀▀
█▄█ █▀█ █░▀░█ ██▄""")
print("\n"*1)
print(board[1]+"|"+board[2]+"|"+board[3])
print("-----")
print(board[4]+"|"+board[5]+"|"+board[6])
print("-----")
print(board[7]+"|"+board[8]+"|"+board[9])
def detect_win(board):
# All you needed was one-variable (win_check) not variable1, ...
if board[1] == board[2] == board[3] != ' ':
display_board(board)
return str(board[1] + " HAS WON !!!")
elif board[1] == board[5] == board[9] != ' ':
display_board(board)
return str(board[1] + " HAS WON !!!")
elif board[7] == board[8] == board[9] != ' ':
display_board(board)
return str(board[7] + " HAS WON !!!")
elif board[7] == board[5] == board[3] != ' ':
return str(board[7] + " HAS WON !!!")
elif board[1] == board[4] == board[7] != ' ':
return str(board[1] + " HAS WON !!!")
elif board[3] == board[6] == board[9] != ' ':
return str(board[3] + " HAS WON !!!")
else:
return None
def setup():
" Initialization to start each game "
board = [" "]*10
marker = " "
while marker != "x" and marker != "o":
marker = input("Player 1, choose x or o: ")
player1 = marker
if player1 == "x":
player2 = "o"
print("""
|------------------------------------|
| Player 1 is now X and player 2 is O|
|------------------------------------|
""")
else:
player2 = "x"
print("""
|------------------------------------|
| Player 1 is now O and player 2 is X|
|------------------------------------|
""")
# Since 'x' goes first assign player1 ^ 2 based upon who's 'x'
if player1 == 'o':
player1, player2 = player2, player1 # swap letters somce player1 goes first
# cell numbers for game
board_numbering = """
1 | 2 | 3
----------
4 | 5 | 6
-----------
7 | 8 | 9"""
print(f'Cell numbering for game\n{board_numbering}')
return board, player1, player2
def place_marker(board, player1, player2):
" Alternates between 'x' and 'o' until win or tie "
for count in range(1, 10):
display_board(board)
# player1 on odd count, playier2 on even
player = player1 if count % 2 else player2
print(f'Player {player} turn')
while True:
try:
position = int(input("Choose a number between 1-9: "))
if 1 <= position <= 9:
if board[position] != ' ':
print('Choose an empty cell')
else:
break
except:
pass
board[position] = player
win_check = detect_win(board)
if win_check:
display_board(board)
print(win_check)
break
else:
print("THIS IS A TIE")
display_board(board)
def play_tic_tac_toe():
" Main function to play the game "
while True:
board, player1, player2 = setup()
place_marker(board, player1, player2)
restart = input("Would you like to play again?:\nyes/no: ")
if restart.lower()[0] != 'y':
print("\n"*100)
print("""
▀▀█▀▀ █──█ █▀▀█ █▀▀▄ █─█ █▀▀ ░█▀▀▀ █▀▀█ █▀▀█ ░█▀▀█ █── █▀▀█ █──█ ─▀─ █▀▀▄ █▀▀▀
─░█── █▀▀█ █▄▄█ █──█ █▀▄ ▀▀█ ░█▀▀▀ █──█ █▄▄▀ ░█▄▄█ █── █▄▄█ █▄▄█ ▀█▀ █──█ █─▀█
─░█── ▀──▀ ▀──▀ ▀──▀ ▀─▀ ▀▀▀ ░█─── ▀▀▀▀ ▀─▀▀ ░█─── ▀▀▀ ▀──▀ ▄▄▄█ ▀▀▀ ▀──▀ ▀▀▀▀""")
if __name__ == "__main__":
play_tic_tac_toe()
以上是关于我做了一个功能,在我的井字游戏中的每一个动作之后都会改变玩家,但似乎不起作用的主要内容,如果未能解决你的问题,请参考以下文章