Example #1
0
File: dataset.py Project: olk/ki-go
 def _get_handicap(self, sgf):
     board = Board(19, 19)
     first_move_done = False
     game_state = GameState.new_game(19)
     if sgf.get_handicap() is not None:
         point = None
         for setup in sgf.get_root().get_setup_stones():
             for move in setup:
                 row, col = move
                 point = Point(row + 1, col + 1)
                 board.place_stone(Player.black, point)
         first_move_done = True
         if point is not None:
             game_state = GameState(board, Player.white, None, Move.play(point))
     return game_state, first_move_done
Example #2
0
def main():
    game = GameState.new_game(19)
    checkpoint_p = Path('./checkpoints/').resolve().joinpath('betago.params')
    ctx = mx.gpu()
    agent = BetaGoAgent.create(checkpoint_p, ctx)

    #for i in range(3):
    #    human_move = 'A%d' % (i+1)
    #    print(human_move)
    #    point = point_from_coords(human_move.strip())
    #    print(point)
    #    move = Move.play(point)
    #    game = game.apply_move(move)
    #    move = agent.select_move(game)
    #    game = game.apply_move(move)
    #    print_board(game.board)

    while not game.is_over():
        # before each move, clear screen
        print(chr(27) + "[2J")  # <2>
        print_board(game.board)
        if Player.black == game.next_player:
            human_move = input('-- ')
            point = point_from_coords(human_move.strip())
            move = Move.play(point)
        else:
            move = agent.select_move(game)
        game = game.apply_move(move)
    print_board(game.board)
    winner = game.winner()
    if winner is None:
        print("It's a draw.")
    else:
        print('Winner: ' + str(winner))
Example #3
0
File: server.py Project: olk/ki-go
 def select_move(bot_name):
     content = request.json
     board_size = content['board_size']
     game_state = GameState.new_game(board_size)
     # Replay the game up to this point.
     for move in content['moves']:
         if move == 'pass':
             next_move = Move.pass_turn()
         elif move == 'resign':
             next_move = Move.resign()
         else:
             next_move = Move.play(point_from_coords(move))
         game_state = game_state.apply_move(next_move)
     bot_agent = bot_map[bot_name]
     bot_move = bot_agent.select_move(game_state)
     if bot_move.is_pass:
         bot_move_str = 'pass'
     elif bot_move.is_resign:
         bot_move_str = 'resign'
     else:
         bot_move_str = coords_from_point(bot_move.point)
     return jsonify({
         'bot_move': bot_move_str,
         'diagnostics': bot_agent.diagnostics()
     })
Example #4
0
 def __init__(self, agent, termination=None, handicap=0, opponent='gnugo', output_sgf="out.sgf",
              our_color='w'):
     # initialize a bot from an agent and a termination strategy
     self._agent = TerminationAgent(agent, termination)
     self._handicap = handicap
     # _play until the game is stopped by one of the _players
     self._stopped = False
     self._game_state = GameState.new_game(19)
     # at the end we write the the game to the provided file in SGF forma
     self._sgf = SGFWriter(output_sgf)
     self._our_color = Player.black if our_color == 'b' else Player.white
     self._their_color = self._our_color.other
     # opponent will either be GNU Go or Pachi
     cmd = self.opponent_cmd(opponent)
     pipe = subprocess.PIPE
     # read and write GTP commands from the command line
     self._proc = Popen(cmd, stdin=PIPE, stdout=PIPE)
     self._stdin = TextIOWrapper(self._proc.stdin, encoding='utf-8', line_buffering=True)
     self._stdout = TextIOWrapper(self._proc.stdout, encoding='utf-8')
Example #5
0
def main():
    board_size = 3
    game = GameState.new_game(board_size)
    bots = {
            Player.black: DepthBrunedAgent(3, capture_diff),
            Player.white: DepthBrunedAgent(3, capture_diff),
    }
    while not game.is_over():
        # sleep 300ms so that bot moves are not printed too fast
        time.sleep(0.3)
        # before each move, clear screen
        print(chr(27) + "[2J")  # <2>
        bot_move = bots[game.next_player].select_move(game)
        print_board(game.board)
        print_move(game.next_player, bot_move)
        game = game.apply_move(bot_move)
    print_board(game.board)
    winner = game.winner()
    if winner is None:
        print("It's a draw.")
    else:
        print('Winner: ' + str(winner))
Example #6
0
def main():
    board_size = 5
    game = GameState.new_game(board_size)
    bot = MCTSAgent(num_rounds=500, temperature=1.4)
    while not game.is_over():
        # before each move, clear screen
        print(chr(27) + "[2J")  # <2>
        print_board(game.board)
        if Player.black == game.next_player:
            human_move = input('-- ')
            point = point_from_coords(human_move.strip())
            move = Move.play(point)
        else:
            move = bot.select_move(game)
        game = game.apply_move(move)

    print_board(game.board)
    winner = game.winner()
    if winner is None:
        print("It's a draw.")
    else:
        print('Winner: ' + str(winner))