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()
Example #2
0
 def move_value(self, game_state: GameState, depth: int) -> (Move, int):
     candidates = game_state.legal_moves()
     if not candidates:
         return (Move.pass_turn(), 0)
     for move in candidates:
         next_state = game_state.apply_move(move)
         if next_state.is_over():
             return (move, 1)
     if depth > 1:
         best_move, best_value = random.choice(candidates), -1
         for move in candidates:
             next_state = game_state.apply_move(move)
             _, value = self.move_value(next_state, depth - 1)
             value *= -1
             if value > best_value:
                 best_move = move
                 best_value = value
         return (best_move, best_value)
     return random.choice(candidates), 0
    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 #4
0
 def __init__(self,
              game_state: GameState,
              parent: 'MCTSNode' = None,
              move: Move = None):
     self.game_state = game_state
     self.parent = parent
     self.move = move
     self.win_counts = {
         Player.black: 0,
         Player.white: 0,
     }
     self.num_rollouts = 0
     self.children = []
     self.unvisited_moves = game_state.legal_moves()
Example #5
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 #6
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))
Example #8
0
 def select_move(self, game_state: GameState) -> Move:
     """Choose a random valid move."""
     candidates = game_state.legal_moves()
     if not candidates:
         return Move.pass_turn()
     return random.choice(candidates)