示例#1
0
def play_game(board: Board, player1: Player, player2: Player):
    player1.new_game(CROSS)
    player2.new_game(NAUGHT)
    board.reset()

    finished = False
    while not finished:
        result, finished = player1.move(board)
        if finished:
            if result == GameResult.DRAW:
                final_result = GameResult.DRAW
            else:
                final_result = GameResult.CROSS_WIN
        else:
            result, finished = player2.move(board)
            if finished:
                if result == GameResult.DRAW:
                    final_result = GameResult.DRAW
                else:
                    final_result = GameResult.NAUGHT_WIN

    # noinspection PyUnboundLocalVariable
    player1.final_result(final_result)
    # noinspection PyUnboundLocalVariable
    player2.final_result(final_result)
    return final_result
示例#2
0
def play_game(board: Board,
              player1: Player,
              player2: Player,
              silent: bool = True):
    player1.new_game(CROSS)
    player2.new_game(NAUGHT)
    board.reset()

    if not silent:
        print()
        board.print_board()
        time.sleep(1)

    finished = False
    while not finished:
        # player1 move
        result, finished = player1.move(board)
        if not silent:
            print()
            print("{} move:".format(player1.name))
            board.print_board()
            time.sleep(1)
        if finished:
            if result == GameResult.DRAW:
                final_result = GameResult.DRAW
            else:
                final_result = GameResult.CROSS_WIN
        else:
            # player 2 move
            result, finished = player2.move(board)
            if not silent:
                print()
                print("{} move:".format(player2.name))
                board.print_board()
                time.sleep(1)
            if finished:
                if result == GameResult.DRAW:
                    final_result = GameResult.DRAW
                else:
                    final_result = GameResult.NAUGHT_WIN

    player1.final_result(final_result)
    player2.final_result(final_result)

    if not silent:
        print()
        if final_result == GameResult.CROSS_WIN:
            print("{} wins!".format(player1.name))
        elif final_result == GameResult.NAUGHT_WIN:
            print("{} wins!".format(player2.name))
        else:
            print("Draw!")
    return final_result