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))
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)
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
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
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))
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")
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()
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()
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, {})
def see(colour, move, board): tc.assertIsInstance(board, boards.Board) seen.append("%s %s" % (colour, format_vertex(move)))
def format_move(move): if move in ['pass', 'skip']: return move return format_vertex(move)
def print_move(colour, move, board, **kwargs): print(colour.upper(), format_vertex(move))
def callback(colour, move, board, **kwargs): fx.backend.log.append("[callback %s %s]" % (colour, format_vertex(move))) if move is None: 1 / 0
def print_board(colour, move, board, **kwargs): print(colour.upper(), format_vertex(move)) print(ascii_boards.render_board(board)) print()