【问题标题】:Gameplay not working in Python Tic-Tac-Toe game游戏在 Python Tic-Tac-Toe 游戏中不起作用
【发布时间】:2021-07-18 11:56:19
【问题描述】:

我已经working on a Python tic-tac-toe game 大约一个月了,终于看到了结局。我正在为游戏的代码而苦苦挣扎,非常感谢任何人为克服这个障碍而提出的建议。

问题:

每次玩家移动时,我的 game_board 都会重新打印一次。因此,游戏在任何一个棋盘被填满之前就结束了。

代码

这是我的游戏板的代码:

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

而且,这与 IndexError 相关:

def replay():

choice = ' '
# declare variable, which is an empty string

while choice.lower()[0] != 'y': #uppercasing the 'Y' ensures that input can only be uppercase
# while loop ensures any other input will be rejected

    choice = input("Do you want to play again? ") # program will keep asking for valid input

    if choice.lower()[0] == 'n':
        print("GAME OVER.")
        return False

else:
    print("LET'S PLAY!")
    return True
    # if player wants a replay, return a boolean True

对我的代码为什么不起作用有任何猜测吗?

我希望这是足够的代码来告知人们可能有的任何响应。我很乐意分享更多代码。

另外,我理解是否有些人会觉得 StackOverflow 不适合提出此类问题。我只是想学习和解决问题!提前致谢!

【问题讨论】:

  • 检查是否choice &gt; 0怎么样?就像if len(choice) &lt; 1: choice = ' '; continue 直接在input 之后?然后它会再次询问选择是否为空。
  • 感谢您的回答,@wuerfelfreak!虽然这个解决方案对我不起作用,但我设法自己解决了这个问题。

标签: python tic-tac-toe


【解决方案1】:

我解决了这个问题,终于完成了游戏!!这对我有用。我希望这些解决方案对其他人有所帮助!

  1. 我在我的 def display_board(board) 函数中添加了 print('\n'*50)(上面未显示),这样当我的电路板重新打印时——这显然是它应该做的——我的重印板并非全部重叠。
  2. 上面的代码中也没有显示,我意识到在检查获胜者的函数 def check_winner 中,原始参数是“board”和“marker”,而我返回的是 板[位置] == 标记。这个函数应该接受参数 "board" 和 "MARK" 并返回 board[position] == MARK

在我进行这些更改后,我注意到我的电路板打印出来的时间有延迟,这是我在开发这个项目的早期遇到的一个错误。为了解决这个问题,我进行了以下更改:

  1. game_on while 循环(如上所示)中,我重新组织了在 turn == 'Player 1' 或 'Player 2' 时应该发生的动作顺序。我没有将我的操作排序为 (1) position = player_choice(game_board)、(2) place_marker(game_board, player1_marker, position) 和 (3) display_board(game_board),而是将它们重新排序为:(1) display_board(game_board), (2) position = player_choice(game_board),和 (3) place_marker(game_board, player1_marker, position)。
  2. if check_winner 下,我确保 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
        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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多