如何在Python中的tic tac toe中切换玩家

时间:2018-04-25 01:14:34

标签: python tic-tac-toe

我的其余代码完成了我现在想做的事情。我不能为我的生活弄清楚为什么我不能在玩家之间切换。我的代码对我有意义。这是一个由两部分组成的问题:我如何使用我采用的相同方法解决这个问题,以及你将如何做到这一点?

<p>Hello</p>
<hr>
<p>Hello from the other side</p>

2 个答案:

答案 0 :(得分:0)

这里实际上存在多个问题,但让我们来看看它的根源:

winning_combo功能会在每次通话开始时重置turn = 0。当然,您使用turn = (turn + 1) % len(players)将其更新为1,但您永远不会再次使用该值;您只需从函数返回,然后再次调用它,然后再次设置turn = 0

这里最简单的解决方法是为turn使用全局变量,因此它可以在调用之间保持持久性:

def winning_combo(player1, player2):
    global turn
    players = [player1, player2]
    player = players[turn]
    turn = (turn + 1) % len(players)
    while tiles_available:
        # the rest of the code is the same

def game():
    global turn
    turn = 0
    playing = True
    # the rest of the code is the same

您可能听说全局变量很糟糕。你能在这里避开吗?当然。虽然你不能在winning_combo中使用局部变量,因为该函数不断退出并重新开始,你可以使用game中的局部变量,并将其传入作为参数winning_combo

def winning_combo(player1, player2, turn):
    players = [player1, player2]
    player = players[turn]
    while tiles_available:
        move = int(input('Where would you like to move? '))
        # the rest of the code is the same

def game():
    turn = 0
    playing = True
    player1, player2 = player_input()
    while playing:
        draw_board()
        winning_combo(player1, player2, turn)
        turn = (turn + 1) % 2
        win_combo()

还有很多其他方法可以改进你的设计并简化你的代码,并修复错误(例如,当游戏结合时会发生什么?),但这应该会让你超越当前的bug,并希望能给你深入了解局部变量的工作原理。

答案 1 :(得分:0)

board_tiles = [1, 2, 3, 4, 5, 6, 7, 8, 9]
tiles_available = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# create a function that draw the board
def draw_board():
    #call clear_screen()
    print(f"""
    {board_tiles[6]} | {board_tiles[7]} | {board_tiles[8]}
   ---+---+---
    {board_tiles[3]} | {board_tiles[4]} | {board_tiles[5]}
   ---+---+---
    {board_tiles[0]} | {board_tiles[1]} | {board_tiles[2]}
""")

# create a function that defines a win
def winning_combo(player1, player2):
    players = [player1, player2]
    global turn
    player = players[turn]
    turn = (turn + 1) % len(players)
    moove = ''
    while tiles_available:
        if turn != 1:
            moove = player2
        else:
            moove = player1
        # player 1 = 1 player 2 = 0
        move = int(input(f'Player {moove}, Where would you like to move? '))
        real_move = move - 1
        if move in tiles_available:
            tiles_available.remove(move)
            board_tiles.insert(board_tiles[real_move], player)
            board_tiles.remove(move)
        else:
            print('That move has already been taken.')

        return move, turn, player

# create a function to determine when a player wins
def win_combo():
    play_1 = 'X'
    play_2 = 'O'
    if board_tiles[0] == board_tiles[1] == board_tiles[2] == play_1 or \
        board_tiles[3] == board_tiles[4] == board_tiles[5] == play_1 or \
        board_tiles[6] == board_tiles[7] == board_tiles[8] == play_1 or \
        board_tiles[0] == board_tiles[3] == board_tiles[6] == play_1 or \
        board_tiles[1] == board_tiles[4] == board_tiles[7] == play_1 or \
        board_tiles[2] == board_tiles[5] == board_tiles[8] == play_1 or \
        board_tiles[0] == board_tiles[4] == board_tiles[8] == play_1 or \
        board_tiles[2] == board_tiles[4] == board_tiles[6] == play_1 :
        print(draw_board())
        print(f"{play_1} YOU ARE THE WINNER!")
    elif board_tiles[0] == board_tiles[1] == board_tiles[2] == play_2 or \
          board_tiles[3] == board_tiles[4] == board_tiles[5] == play_2 or \
          board_tiles[6] == board_tiles[7] == board_tiles[8] == play_2 or \
          board_tiles[0] == board_tiles[3] == board_tiles[6] == play_2 or \
          board_tiles[1] == board_tiles[4] == board_tiles[7] == play_2 or \
          board_tiles[2] == board_tiles[5] == board_tiles[8] == play_2 or \
          board_tiles[0] == board_tiles[4] == board_tiles[8] == play_2 or \
          board_tiles[2] == board_tiles[4] == board_tiles[6] == play_2:
        print(draw_board())
        print(f"{play_2} YOU ARE THE WINNER!")
    else:
        True

# create a game function
def game():
    global turn
    turn = 0
    playing = 0
    player1, player2 = 'X', 'O'
    while playing < 10:
        draw_board()
        winning_combo(player1, player2)
        win_combo()
        if playing == 9:
            print("It's a TIE!!!")
            playing = 10
        playing += 1

game()