Exemplo n.º 1
0
    def genmove(self, cmd):
        y = self.policy.evaluate(self.board, cmd[1].lower(), self.moves)

        i = np.argmax(y[0])
        self.board.play(row=i % 19, col=i // 19, colour=cmd[1].lower())
        self.moves.append((i % 19, i // 19))
        return "= " + common.format_vertex((i % 19, i // 19))
Exemplo n.º 2
0
    def check_moves(self, expected_moves):
        """Check that the game's moves are as expected.

        expected_moves -- list of pairs (colour, vertex)

        """
        game_moves = [(colour, format_vertex(move))
                      for (colour, move, comment) in self.game.get_moves()]
        self.tc.assertListEqual(game_moves, expected_moves)
Exemplo n.º 3
0
    def check_moves(self, expected_moves):
        """Check that the game's moves are as expected.

        expected_moves -- list of pairs (colour, vertex)

        """
        game_moves = [(colour, format_vertex(move))
                      for (colour, move, comment) in self.game.get_moves()]
        self.tc.assertListEqual(game_moves, expected_moves)
Exemplo n.º 4
0
 def notify_move(self, colour, move):
     if (self.reject_vertex is not None
             and move == move_from_vertex(self.reject_vertex, self._size)):
         if self.reject_as_error:
             self.log.append("notify_move -> %s [error]" % colour)
             return 'error', "programmed error"
         else:
             self.log.append("notify_move -> %s [rejecting]" % colour)
             return 'reject', "programmed reject"
     self.log.append("notify_move -> %s %s" % (colour, format_vertex(move)))
     return 'accept', None
Exemplo n.º 5
0
 def notify_move(self, colour, move):
     if (self.reject_vertex is not None and
         move == move_from_vertex(self.reject_vertex, self._size)):
         if self.reject_as_error:
             self.log.append("notify_move -> %s [error]" % colour)
             return 'error', "programmed error"
         else:
             self.log.append("notify_move -> %s [rejecting]" % colour)
             return 'reject', "programmed reject"
     self.log.append("notify_move -> %s %s" % (colour, format_vertex(move)))
     return 'accept', None
Exemplo n.º 6
0
 def get_move(self, colour):
     try:
         vertex = self._move_iters[colour].next()
         action, detail = self._action_for_vertex(vertex)
     except StopIteration:
         action, detail = 'move', None
     if action == 'move':
         log_description = "move/%s" % format_vertex(detail)
     else:
         log_description = "%s/%r" % (action, detail)
     self.log.append("get_move <- %s: %s" % (colour, log_description))
     self.last_move = log_description
     return action, detail
Exemplo n.º 7
0
def test_get_last_move(tc):
    fx = Gtp_state_fixture(tc)
    fx.player.set_next_move("A3", "preprogrammed move A3")
    fx.check_command('genmove', ['B'], "A3")
    history_moves = fx.player.last_game_state.move_history
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'b'), (False, None))
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'w'), (False, None))

    fx.player.set_next_move("B3", "preprogrammed move B3")
    fx.check_command('genmove', ['W'], "B3")
    history_moves = fx.player.last_game_state.move_history
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'b'), (False, None))
    move_is_available, move = gtp_states.get_last_move(history_moves, 'w')
    tc.assertIs(move_is_available, True)
    tc.assertEqual(format_vertex(move), "A3")

    fx.check_command('genmove', ['B'], "pass")
    history_moves = fx.player.last_game_state.move_history
    move_is_available, move = gtp_states.get_last_move(history_moves, 'b')
    tc.assertIs(move_is_available, True)
    tc.assertEqual(format_vertex(move), "B3")
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'w'), (False, None))
Exemplo n.º 8
0
 def get_move(self, colour):
     try:
         vertex = self._move_iters[colour].next()
         action, detail = self._action_for_vertex(vertex)
     except StopIteration:
         action, detail = 'move', None
     if action == 'move':
         log_description = "move/%s" % format_vertex(detail)
     else:
         log_description = "%s/%r" % (action, detail)
     self.log.append("get_move <- %s: %s" % (colour, log_description))
     self.last_move = log_description
     return action, detail
Exemplo n.º 9
0
def test_get_last_move(tc):
    fx = Gtp_state_fixture(tc)
    fx.player.set_next_move("A3", "preprogrammed move A3")
    fx.check_command('genmove', ['B'], "A3")
    history_moves = fx.player.last_game_state.move_history
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'b'), (False, None))
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'w'), (False, None))

    fx.player.set_next_move("B3", "preprogrammed move B3")
    fx.check_command('genmove', ['W'], "B3")
    history_moves = fx.player.last_game_state.move_history
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'b'), (False, None))
    move_is_available, move = gtp_states.get_last_move(history_moves, 'w')
    tc.assertIs(move_is_available, True)
    tc.assertEqual(format_vertex(move), "A3")

    fx.check_command('genmove', ['B'], "pass")
    history_moves = fx.player.last_game_state.move_history
    move_is_available, move = gtp_states.get_last_move(history_moves, 'b')
    tc.assertIs(move_is_available, True)
    tc.assertEqual(format_vertex(move), "B3")
    tc.assertEqual(gtp_states.get_last_move(history_moves, 'w'), (False, None))
Exemplo n.º 10
0
 def runTest(self):
     b = boards.Board(9)
     ko_point = None
     for move in self.moves:
         colour, vertex = move.split()
         colour = colour.lower()
         row, col = move_from_vertex(vertex, b.side)
         ko_point = b.play(row, col, colour)
     self.assertBoardEqual(b, self.diagram)
     if ko_point is None:
         ko_vertex = None
     else:
         ko_vertex = format_vertex(ko_point)
     self.assertEqual(ko_vertex, self.ko_vertex, "wrong ko point")
     self.assertEqual(b.area_score(), self.score, "wrong score")
Exemplo n.º 11
0
 def runTest(self):
     b = boards.Board(9)
     ko_point = None
     for move in self.moves:
         colour, vertex = move.split()
         colour = colour.lower()
         row, col = move_from_vertex(vertex, b.side)
         ko_point = b.play(row, col, colour)
     self.assertBoardEqual(b, self.diagram)
     if ko_point is None:
         ko_vertex = None
     else:
         ko_vertex = format_vertex(ko_point)
     self.assertEqual(ko_vertex, self.ko_vertex, "wrong ko point")
     self.assertEqual(b.area_score(), self.score, "wrong score")
Exemplo n.º 12
0
def test_set_free_handicap(tc):
    fx = Gtp_state_fixture(tc)
    fx.check_command('set_free_handicap', ["C3", "E5", "C7"], "")
    fx.check_command(
        'showboard', [],
        dedent("""
    9  .  .  .  .  .  .  .  .  .
    8  .  .  .  .  .  .  .  .  .
    7  .  .  #  .  .  .  .  .  .
    6  .  .  .  .  .  .  .  .  .
    5  .  .  .  .  #  .  .  .  .
    4  .  .  .  .  .  .  .  .  .
    3  .  .  #  .  .  .  .  .  .
    2  .  .  .  .  .  .  .  .  .
    1  .  .  .  .  .  .  .  .  .
       A  B  C  D  E  F  G  H  J"""))
    fx.check_command('genmove', ['B'], "pass")
    tc.assertEqual(fx.player.last_game_state.handicap, 3)
    fx.check_command('boardsize', ['9'], "")
    fx.check_command('play', ['B', 'B2'], "")
    fx.check_command('set_free_handicap', ["C3", "E5"],
                     "board not empty",
                     expect_failure=True)
    fx.check_command('clear_board', [], "")
    fx.check_command('set_free_handicap', ["C3"],
                     "invalid number of stones",
                     expect_failure=True)
    fx.check_command('set_free_handicap', [],
                     "invalid number of stones",
                     expect_failure=True)
    all_points = [format_vertex((i, j)) for i in range(9) for j in range(9)]
    fx.check_command('set_free_handicap',
                     all_points,
                     "invalid number of stones",
                     expect_failure=True)
    fx.check_command('set_free_handicap', ["C3", "asdasd"],
                     "invalid vertex: 'asdasd'",
                     expect_failure=True)
    fx.check_board_empty_9()
    fx.check_command('set_free_handicap', ["C3", "pass"],
                     "'pass' not permitted",
                     expect_failure=True)
    fx.check_board_empty_9()
    fx.check_command('set_free_handicap', ["C3", "E5", "C3"],
                     "engine error: C3 is occupied",
                     expect_failure=True)
    fx.check_board_empty_9()
Exemplo n.º 13
0
def test_set_free_handicap(tc):
    fx = Gtp_state_fixture(tc)
    fx.check_command('set_free_handicap', ["C3", "E5", "C7"], "")
    fx.check_command('showboard', [], dedent("""
    9  .  .  .  .  .  .  .  .  .
    8  .  .  .  .  .  .  .  .  .
    7  .  .  #  .  .  .  .  .  .
    6  .  .  .  .  .  .  .  .  .
    5  .  .  .  .  #  .  .  .  .
    4  .  .  .  .  .  .  .  .  .
    3  .  .  #  .  .  .  .  .  .
    2  .  .  .  .  .  .  .  .  .
    1  .  .  .  .  .  .  .  .  .
       A  B  C  D  E  F  G  H  J"""))
    fx.check_command('genmove', ['B'], "pass")
    tc.assertEqual(fx.player.last_game_state.handicap, 3)
    fx.check_command('boardsize', ['9'], "")
    fx.check_command('play', ['B', 'B2'], "")
    fx.check_command('set_free_handicap', ["C3", "E5"], "board not empty",
                     expect_failure=True)
    fx.check_command('clear_board', [], "")
    fx.check_command('set_free_handicap', ["C3"], "invalid number of stones",
                     expect_failure=True)
    fx.check_command('set_free_handicap', [], "invalid number of stones",
                     expect_failure=True)
    all_points = [format_vertex((i, j)) for i in range(9) for j in range(9)]
    fx.check_command('set_free_handicap', all_points,
                     "invalid number of stones", expect_failure=True)
    fx.check_command('set_free_handicap', ["C3", "asdasd"],
                     "invalid vertex: 'asdasd'", expect_failure=True)
    fx.check_board_empty_9()
    fx.check_command('set_free_handicap', ["C3", "pass"],
                     "'pass' not permitted", expect_failure=True)
    fx.check_board_empty_9()
    fx.check_command('set_free_handicap', ["C3", "E5", "C3"],
                     "engine error: C3 is occupied", expect_failure=True)
    fx.check_board_empty_9()
Exemplo n.º 14
0
 def callback(colour, move, board, **kwargs):
     self.backend.log.append("[callback %s %s]" %
                             (colour, format_vertex(move)))
     self.callback_boards.append(board.copy())
     self.tc.assertEqual(kwargs, {})
Exemplo n.º 15
0
 def see(colour, move, board):
     tc.assertIsInstance(board, boards.Board)
     seen.append("%s %s" % (colour, format_vertex(move)))
Exemplo n.º 16
0
def format_move(move):
    if move in ['pass', 'skip']:
        return move
    return format_vertex(move)
Exemplo n.º 17
0
 def callback(colour, move, board, **kwargs):
     self.backend.log.append("[callback %s %s]" %
                             (colour, format_vertex(move)))
     self.callback_boards.append(board.copy())
     self.tc.assertEqual(kwargs, {})
Exemplo n.º 18
0
def print_move(colour, move, board, **kwargs):
    print(colour.upper(), format_vertex(move))
Exemplo n.º 19
0
 def callback(colour, move, board, **kwargs):
     fx.backend.log.append("[callback %s %s]" %
                           (colour, format_vertex(move)))
     if move is None:
         1 / 0
Exemplo n.º 20
0
def print_board(colour, move, board, **kwargs):
    print(colour.upper(), format_vertex(move))
    print(ascii_boards.render_board(board))
    print()
Exemplo n.º 21
0
 def see(colour, move, board):
     tc.assertIsInstance(board, boards.Board)
     seen.append("%s %s" % (colour, format_vertex(move)))
Exemplo n.º 22
0
 def callback(colour, move, board, **kwargs):
     fx.backend.log.append("[callback %s %s]" %
                           (colour, format_vertex(move)))
     if move is None:
         1 / 0