Python Tic Tac Toe游戏中的游戏性不起作用
我已经呆了大约一个月了,末日终于到了。我正在为游戏性的代码而挣扎,非常感谢任何人对克服这个障碍的建议 问题:Python Tic Tac Toe游戏中的游戏性不起作用,python,tic-tac-toe,Python,Tic Tac Toe,我已经呆了大约一个月了,末日终于到了。我正在为游戏性的代码而挣扎,非常感谢任何人对克服这个障碍的建议 问题: print('Welcome to Tic Tac Toe!') # turn = '' # needs to be an empty string because we're storing the result of go_first(), a string, inside while True: # loop forever game_board = [' ']*10
print('Welcome to Tic Tac Toe!')
# turn = '' # needs to be an empty string because we're storing the result of go_first(), a string, inside
while True: # loop forever
game_board = [' ']*10 # clear board
player1_marker , player2_marker = assign_marker() # assign players to marker
turn = go_first() # randomly choose who goes first
print(f"{turn}, you're up first!")
play_game = input("Ready? Enter Yes or No.")
if play_game.lower()[0] == 'y':
game_on = True
print("LET'S PLAY!")
else:
game_on = False
while game_on: # initiates the start of the game
if turn == 'Player 1': # if player 1 is first
display_board(game_board) # display board
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player1_marker, position) # place marker at the position
if check_winner(game_board, player1_marker): # if there are no more moves
display_board(game_board) # display board
print(f"{turn} wins!")
game_on = False
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
display_board(game_board) # display board
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 2' #move on to player 2; THIS IS HOW TO ALTERNATE TURNS
else: # Player 2's turn
display_board(game_board)
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player2_marker, position) # place marker at the position
if check_winner(game_board, player2_marker):
display_board(game_board) # display board
print(f"{turn} wins!")
game_on = False
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
display_board(game_board) # display board
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 1' #move on to player 1; THIS IS HOW TO ALTERNATE TURNS
if not replay():
break
每次玩家移动时,我的游戏板都会重复播放。结果,游戏在任何一块棋盘被填满之前就结束了
代码
以下是我的游戏板的代码:
game_board = ['#'] + ([' ']*9)
display_board(game_board)
以下是我的游戏代码:
print('Welcome to Tic Tac Toe!')
# turn = '' # needs to be an empty string because we're storing the result of go_first(), a string, inside
while True: # loop forever
game_board = [' ']*10 # clear board
player1_marker , player2_marker = assign_marker() # assign players to marker
turn = go_first() # randomly choose who goes first
print(f"{turn}, you're up first!")
play_game = input("Ready? Enter Yes or No.")
if play_game.lower()[0] == 'y':
game_on = True
else:
game_on = False
while game_on: # initiates the start of the game
if turn == 'Player 1': # if player 1 is first
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player1_marker, position) # place marker at the position
display_board(game_board) # display board
if check_winner(game_board, player1_marker): # if there are no more moves
game_on = False
print(f"{turn} wins!")
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 2' #move on to player 2; THIS IS HOW TO ALTERNATE TURNS
else: # if player 2 is first
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player2_marker, position) # place marker at the position
display_board(game_board)
if check_winner(game_board, player2_marker):
game_on = False
print(f"{turn} wins!")
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
game_on = False
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 1' #move on to player 2; THIS IS HOW TO ALTERNATE TURNS
if not replay():
break
结果
Welcome to Tic Tac Toe!
Player 1 has chosen X
Player 2 is O
Player 1, you're up first!
| |
| |
_____
| |
| |
_____
| |
| |X
| |
| |
_____
| |
|O|
_____
| |
| |X
| |
X| |
_____
| |
|O|
_____
| |
| |X
| |
X|O|
_____
| |
|O|
_____
| |
| |X
| |
X|O|X
_____
| |
|O|
_____
| |
| |X
| |
X|O|X
_____
| |
|O|
_____
| |
|O|X
Player 2 wins!
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-36-9859c5aad566> in <module>
54 turn = 'Player 1' #move on to player 2; THIS IS HOW TO ALTERNATE TURNS
55
---> 56 if not replay():
57 break
<ipython-input-30-73127fbca36c> in replay()
9 choice = input("Do you want to play again? ") # program will keep asking for valid input
10
---> 11 if choice.lower()[0] == 'n':
12 print("GAME OVER.")
13 return False
IndexError: string index out of range
有人猜测我的代码为什么不起作用吗?
我希望这是足够的代码通知任何人可能有任何反应。我很乐意分享更多的代码
另外,我也明白,如果有些人觉得StackOverflow不是提出此类问题的地方。我只是在努力学习和解决问题!提前谢谢 我解决了这个问题,最终完成了游戏!!这是对我有用的东西。我希望这些解决方案能帮助其他人强>
print('Welcome to Tic Tac Toe!')
# turn = '' # needs to be an empty string because we're storing the result of go_first(), a string, inside
while True: # loop forever
game_board = [' ']*10 # clear board
player1_marker , player2_marker = assign_marker() # assign players to marker
turn = go_first() # randomly choose who goes first
print(f"{turn}, you're up first!")
play_game = input("Ready? Enter Yes or No.")
if play_game.lower()[0] == 'y':
game_on = True
print("LET'S PLAY!")
else:
game_on = False
while game_on: # initiates the start of the game
if turn == 'Player 1': # if player 1 is first
display_board(game_board) # display board
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player1_marker, position) # place marker at the position
if check_winner(game_board, player1_marker): # if there are no more moves
display_board(game_board) # display board
print(f"{turn} wins!")
game_on = False
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
display_board(game_board) # display board
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 2' #move on to player 2; THIS IS HOW TO ALTERNATE TURNS
else: # Player 2's turn
display_board(game_board)
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player2_marker, position) # place marker at the position
if check_winner(game_board, player2_marker):
display_board(game_board) # display board
print(f"{turn} wins!")
game_on = False
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
display_board(game_board) # display board
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 1' #move on to player 1; THIS IS HOW TO ALTERNATE TURNS
if not replay():
break
print('Welcome to Tic Tac Toe!')
# turn = '' # needs to be an empty string because we're storing the result of go_first(), a string, inside
while True: # loop forever
game_board = [' ']*10 # clear board
player1_marker , player2_marker = assign_marker() # assign players to marker
turn = go_first() # randomly choose who goes first
print(f"{turn}, you're up first!")
play_game = input("Ready? Enter Yes or No.")
if play_game.lower()[0] == 'y':
game_on = True
print("LET'S PLAY!")
else:
game_on = False
while game_on: # initiates the start of the game
if turn == 'Player 1': # if player 1 is first
display_board(game_board) # display board
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player1_marker, position) # place marker at the position
if check_winner(game_board, player1_marker): # if there are no more moves
display_board(game_board) # display board
print(f"{turn} wins!")
game_on = False
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
display_board(game_board) # display board
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 2' #move on to player 2; THIS IS HOW TO ALTERNATE TURNS
else: # Player 2's turn
display_board(game_board)
position = player_choice(game_board) # ask for position from player
place_marker(game_board, player2_marker, position) # place marker at the position
if check_winner(game_board, player2_marker):
display_board(game_board) # display board
print(f"{turn} wins!")
game_on = False
else: # if there's no winner
if is_board_full(game_board): # check to see if the board is full
# if board is full but there's no winner, it's a draw
display_board(game_board) # display board
print("It's a draw!")
break
else: # if the board is NOT full ...
turn = 'Player 1' #move on to player 1; THIS IS HOW TO ALTERNATE TURNS
if not replay():
break
检查一下
choice>0
?像这样if len(choice)<1:choice='';直接在输入后继续
?然后它会再次询问选项是否为空。谢谢您的回答,@wuerfelfreak!虽然这个解决方案对我不起作用,但我还是设法自己解决了这个问题。