为什么我的check_if_tie()函数在TicTacToe中不起作用?

时间:2020-05-25 18:07:23

标签: python tic-tac-toe

所以我的TicTacToe有了这个Python代码。一切工作正常,除非没有获胜者,程序必须返回“ Tie”,而是继续要求X和O,即使董事会已满。我假设问题出在check_if_tie()函数中,但我无法弄清楚。

<style lang='sass' scoped>
.works
 &__content
  p
  img
   width: 100px
</style>

2 个答案:

答案 0 :(得分:3)

此代码中存在很多问题,可以通过避免使用全局变量来避免大多数问题。

使用全局变量时,将更改一个全局状态,这使得很难理解发生了什么。您拥有的函数只会返回更改游戏状态或结束状态的返回值。

一个简单的更改是让您的check_方法返回一个实际的布尔值,并在循环中使用该值来检查您是否平局或获胜。

如果您不是平局或胜利,则表示游戏尚未结束。因此,您无需存储全局值,当然也不必在其他任何地方修改并列或获胜的状态。

使您的功能尽可能简单。我经常这么说,但请考虑一下您实际如何玩游戏。

在游戏中,您有2个玩家和1个棋盘,您可以在其中任意位置设置值。

每个回合都会添加一个棋子,直到游戏以平局或获胜告终。

每次添加一块棋子都可以检查游戏的状态。如果还没有完成,那么您可以与当前播放器并处,然后输入一个新片段并重复。

这都不要求全局状态,您始终可以将游戏板传递给您的方法...

对于您来说,这样做就很简单:

def is_tie():
    return ' ' not in board

def is_win():
    ... is_win logic
    return result


while not is_tie() or not is_win():
    ... game logic

要走得更远,除了拥有全局元素,您还必须像这样通过游戏状态来传递游戏状态:

def is_tie(game):
    return ' ' not in game.board

再进一步,在板上输入新零件将返回新状态。因此,除了修改当前状态外,您还可以看到一个主循环,如下所示:

game = Game()

while True:
    position = game.prompt_choice()
    if not game.position_available(position)
        # loop again to select a new position
        continue

    game = game.execute_turn(position)

    if game.is_done()
        break
    else:
        # if game not done switch player and move to next turn
        game = game.switch_player()

# current game player is winner
game.print_winner()

很棒的事情是,如果您想“重玩”游戏,只需在再次循环之前保存游戏状态即可。返回的每个“游戏”都是先前游戏对象的修改版本,因此您永远不会修改实际对象。

答案 1 :(得分:0)

我能想到的最简单的建议是有一个变量,该变量保存剩余的可用平方量,并在每转之后将其减小。一旦达到0,如果没有胜利,那么就必须平局。

# -------Global variables--------

# If game is still going
game_still_going = True


# Who won? Or tie
winner = None
# Whose turn it is
current_player = 'X'

# The board displaying function
board = [' '] * 10

#board spaces
board_spaces = 9

def display_board():
    print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
    print('---+-'   '--+---  ')
    print(' ' + board[3] + ' | ' + board[4] + ' | ' + board[5])
    print('---+-'   '--+---  ')
    print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])

# Checks if game is over


def check_if_game_over():
    check_if_win()
    check_if_tie()


# Checks if there is a winner


def check_if_win():
    global winner

    if check_row():
        winner = check_row()
    elif check_columns():
        winner = check_columns()
    elif check_diagonals():
        winner = check_columns()
    else:
        winner = None
    return


def check_row():
    global game_still_going
    row1 = board[6] == board[7] == board[8] != " "
    row2 = board[3] == board[4] == board[5] != " "
    row3 = board[0] == board[1] == board[2] != " "
    if row1 or row2 or row3:
        game_still_going = False
    if row1:
        return board[6]
    elif row2:
        return board[3]
    elif row3:
        return board[0]

    return


def check_columns():
    global game_still_going
    column1 = board[6] == board[3] == board[0] != " "
    column2 = board[7] == board[4] == board[1] != " "
    column3 = board[8] == board[5] == board[2] != " "
    if column1 or column2 or column3:
        game_still_going = False
    if column1:
        return board[6]
    elif column2:
        return board[7]
    elif column3:
        return board[8]
    return


def check_diagonals():
    global game_still_going
    diagonal1 = board[6] == board[4] == board[2] != " "
    diagonal2 = board[0] == board[4] == board[8] != " "
    if diagonal1 or diagonal2:
        game_still_going = False
    elif diagonal1:
        return board[6]
    elif diagonal2:
        return board[0]
    return


def check_if_tie():
    global game_still_going
    global winner
    global board_spaces
    if winner == None and board_spaces == 0:
        game_still_going = False
    return


def flip_player():
    global current_player
    if current_player == 'X':
        current_player = 'O'
    elif current_player == 'O':
        current_player = 'X'
    return


# Whose turn it is to play


def handle_turn(player):
    print(player + "'s turn")
    position = int(input('Please write your position from 1 - 9: ')) - 1
    if position not in [0,1,2,3,4,5,6,7,8,9]:
        return input('Invalid position. Please write 1-9: ')
    board[position] = player
    display_board()
    board_spaces -= 1


# Main gameplay function
def play_game():
    global winner
    # Displays initial board
    display_board()
    # Loop running the game
    while game_still_going:

        handle_turn(current_player)
        flip_player()
        check_if_game_over()

    if winner == 'X' or winner == 'O':
        print(winner + ' won.')
    elif winner:
        print('Tie')


play_game()