def rotate_point(p, index, board_size): assert 0 <= index < 8 if index & 1: # Reflect rows p = baduk.Point(row=board_size - p.row + 1, col=p.col) if index & 2: # Reflect cols p = baduk.Point(row=p.row, col=board_size - p.col + 1) if index & 4: # Swap rows & cols p = baduk.Point(row=p.col, col=p.row) return p
def print_board(self, board): for row in range(board.num_rows, 0, -1): line = [] for col in range(1, board.num_cols + 1): stone = board.get(baduk.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone]) print('%2d %s' % (row, ''.join(line))) print(' ' + COLS[:board.num_cols])
def decode_ogs_move(omove): x, y, t = omove if x < 0: move = baduk.Move.pass_turn() else: col = x + 1 row = 19 - y move = baduk.Move.play(baduk.Point(row, col)) return move
def test_rotations(self): orig = baduk.Point(3, 6) rotations = [ baduk.Point(3, 6), baduk.Point(6, 3), baduk.Point(14, 3), baduk.Point(17, 6), baduk.Point(3, 14), baduk.Point(6, 17), baduk.Point(17, 14), baduk.Point(14, 17), ] rotated = [symmetry.rotate_point(orig, i, 19) for i in range(8)] self.assertCountEqual(rotated, rotations)
def rotate_board(board, index): size = board.num_rows assert board.num_cols == size new_board = baduk.Board(size, size) for r in range(1, size + 1): for c in range(1, size + 1): p = baduk.Point(row=r, col=c) stone = board.get(p) if stone is not None: new_point = rotate_point(p, index, size) new_board.place_stone(stone, new_point) return new_board
def test_rotate_game_record(self): # Handicap-ish setup board = baduk.Board(5, 5) board.place_stone(baduk.Player.black, baduk.Point(2, 2)) board.place_stone(baduk.Player.black, baduk.Point(2, 4)) game = baduk.GameState.from_board(board, baduk.Player.white) game = game.apply_move(baduk.Move.play(baduk.Point(4, 3))) # 1 == Reflect rows rot_game = symmetry.rotate_game_record(game, 1) self.assertEqual(baduk.Move.play(baduk.Point(2, 3)), rot_game.last_move) rot_board = rot_game.board self.assertEqual(baduk.Player.black, rot_board.get(baduk.Point(4, 2))) self.assertEqual(baduk.Player.black, rot_board.get(baduk.Point(4, 4))) self.assertEqual(baduk.Player.white, rot_board.get(baduk.Point(2, 3))) rot_game = rot_game.previous_state self.assertIsNone(rot_game.last_move) rot_board = rot_game.board self.assertEqual(baduk.Player.black, rot_board.get(baduk.Point(4, 2))) self.assertEqual(baduk.Player.black, rot_board.get(baduk.Point(4, 4))) self.assertIsNone(rot_board.get(baduk.Point(2, 3)))
def test_rotate_board(self): board = baduk.Board(5, 5) board.place_stone(baduk.Player.black, baduk.Point(3, 3)) board.place_stone(baduk.Player.white, baduk.Point(3, 2)) board.place_stone(baduk.Player.black, baduk.Point(4, 2)) rotated = symmetry.rotate_board(board, 1) self.assertEqual(baduk.Player.black, rotated.get(baduk.Point(3, 3))) self.assertEqual(baduk.Player.white, rotated.get(baduk.Point(3, 2))) self.assertEqual(baduk.Player.black, rotated.get(baduk.Point(2, 2)))
def parse_gtp_coords(gtp_coords): col = COLS.index(gtp_coords.upper()[0]) + 1 row = int(gtp_coords[1:]) return baduk.Point(row, col)