Example #1
0
def play_fast(board_size: int, bots: dict) -> Player:
    """This can play 4.7 games/sec on 19x19 on MacBook Air"""
    game = GameState.new_game(board_size)
    while not game.is_over():
        bot_move = bots[game.next_player].select_move(game)
        game = game.apply_move(bot_move)
    return game.winner()
    def test_place_stone(self):
        board_size = 9
        game = GameState.new_game(board_size)
        game = game.apply_move(Move.play(Point(3, 3)))
        game = game.apply_move(Move.play(Point(4, 4)))
        encoder = OnePlaneEncoder(board_size)
        encoded_board = encoder.encode(game)

        self.assertEqual((1, board_size, board_size), encoded_board.shape)
        self.assertEqual(0.0, encoded_board[0, 1, 1])
        self.assertEqual(1.0, encoded_board[0, 2, 2])
        self.assertEqual(-1.0, encoded_board[0, 3, 3])
Example #3
0
def simulate_game(black_player, white_player, board_size):
    moves = []
    game = GameState.new_game(board_size)
    agents = {
        Player.black: black_player,
        Player.white: white_player,
    }
    while not game.is_over():
        next_move = agents[game.next_player].select_move(game)
        moves.append(next_move)
        game = game.apply_move(next_move)

    print_board(game.board)
    game_result = scoring.compute_game_result(game)
    print(game_result)

    return GameRecord(
        moves=moves,
        winner=game_result.winner,
        margin=game_result.winning_margin,
    )
Example #4
0
def play_slow(board_size: int, bots, serialize=False) -> Player:
    game = GameState.new_game(board_size)
    step = 1
    while not game.is_over():
        time.sleep(0.2)  

        bot_move = bots[game.next_player].select_move(game)
        print(chr(27) + "[2J") 
        print_move(game.next_player, bot_move)
        game = game.apply_move(bot_move)
        print_board(game.board)
        if serialize:
            file_name = 'pickle/game-state-{:02d}.pickle'.format(step)
            game.serialize(file_name)
        step += 1

    winner = game.winner()
    if winner is None:
        print('Draw')
    else:
        print('{} won the game'.format(winner))
    return winner
def measure_move_time(agent):
    game_state = GameState.new_game(9)
    start_time = time.time()
    agent.select_move(game_state)
    total_time = time.time() - start_time
    print('Finished in {:.2f}sec.'.format(total_time))