def test_move_with_capture(self):
     start_board = load_board(EMPTY_ROW * 5 + '''
         XXXX.....
         XOOX.....
         O.OX.....
         OOXX.....
     ''')
     start_position = Position(
         board=start_board,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=None,
         recent=tuple(),
         to_play=BLACK,
     )
     expected_board = load_board(EMPTY_ROW * 5 + '''
         XXXX.....
         X..X.....
         .X.X.....
         ..XX.....
     ''')
     expected_position = Position(
         board=expected_board,
         n=1,
         komi=6.5,
         caps=(7, 2),
         ko=None,
         recent=(PlayerMove(BLACK, pc('B2')), ),
         to_play=WHITE,
     )
     actual_position = start_position.play_move(pc('B2'))
     self.assertEqualPositions(actual_position, expected_position)
    def test_capture_multiple_groups(self):
        board = load_board('''
            .OX......
            OXX......
            XX.......
        ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(BLACK, pc('A9'))
        self.assertEqual(len(lib_tracker.groups), 2)
        self.assertEqual(captured, pc_set('B9 A8'))

        corner_stone = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
        self.assertEqual(corner_stone.stones, pc_set('A9'))
        self.assertEqual(corner_stone.liberties, pc_set('B9 A8'))

        surrounding_stones = lib_tracker.groups[lib_tracker.group_index[pc(
            'C9')]]
        self.assertEqual(surrounding_stones.stones, pc_set('C9 B8 C8 A7 B7'))
        self.assertEqual(surrounding_stones.liberties,
                         pc_set('B9 D9 A8 D8 C7 A6 B6'))

        liberty_cache = lib_tracker.liberty_cache
        for stone in corner_stone.stones:
            self.assertEqual(liberty_cache[stone], 2, str(stone))
        for stone in surrounding_stones.stones:
            self.assertEqual(liberty_cache[stone], 7, str(stone))
Beispiel #3
0
 def test_move_with_capture(self):
     start_board = load_board(EMPTY_ROW * 5 + '''
         XXXX.....
         XOOX.....
         O.OX.....
         OOXX.....
     ''')
     start_position = Position(
         board=start_board,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=None,
         recent=tuple(),
         to_play=BLACK,
     )
     expected_board = load_board(EMPTY_ROW * 5 + '''
         XXXX.....
         X..X.....
         .X.X.....
         ..XX.....
     ''')
     expected_position = Position(
         board=expected_board,
         n=1,
         komi=6.5,
         caps=(7, 2),
         ko=None,
         recent=(PlayerMove(BLACK, pc('B2')),),
         to_play=WHITE,
     )
     actual_position = start_position.play_move(pc('B2'))
     self.assertEqualPositions(actual_position, expected_position)
Beispiel #4
0
def get_next_move(node):
    if not node.next:
        return None
    props = node.next.properties
    if 'W' in props:
        return pc(props['W'][0])
    else:
        return pc(props['B'][0])
Beispiel #5
0
    def test_neighbors(self):
        corner = pc('A1')
        neighbors = [go.EMPTY_BOARD[c] for c in go.NEIGHBORS[corner]]
        self.assertEqual(len(neighbors), 2)

        side = pc('A2')
        side_neighbors = [go.EMPTY_BOARD[c] for c in go.NEIGHBORS[side]]
        self.assertEqual(len(side_neighbors), 3)
    def test_neighbors(self):
        corner = pc('A1')
        neighbors = [go.EMPTY_BOARD[c] for c in go.NEIGHBORS[corner]]
        self.assertEqual(len(neighbors), 2)

        side = pc('A2')
        side_neighbors = [go.EMPTY_BOARD[c] for c in go.NEIGHBORS[side]]
        self.assertEqual(len(side_neighbors), 3)
Beispiel #7
0
def get_next_move(node):
    if not node.next:
        return None
    props = node.next.properties
    if 'W' in props:
        return pc(props['W'][0])
    else:
        return pc(props['B'][0])
Beispiel #8
0
    def test_lib_tracker_init(self):
        board = load_board('X........' + EMPTY_ROW * 8)

        lib_tracker = LibertyTracker.from_board(board)
        self.assertEqual(len(lib_tracker.groups), 1)
        self.assertNotEqual(lib_tracker.group_index[pc('A9')], go.MISSING_GROUP_ID)
        self.assertEqual(lib_tracker.liberty_cache[pc('A9')], 2)
        sole_group = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
        self.assertEqual(sole_group.stones, pc_set('A9'))
        self.assertEqual(sole_group.liberties, pc_set('B9 A8'))
        self.assertEqual(sole_group.color, BLACK)
Beispiel #9
0
 def test_capture_stone(self):
     board = load_board('''
         .X.......
         XO.......
         .X.......
     ''' + EMPTY_ROW * 6)
     lib_tracker = LibertyTracker.from_board(board)
     captured = lib_tracker.add_stone(BLACK, pc('C8'))
     self.assertEqual(len(lib_tracker.groups), 4)
     self.assertEqual(lib_tracker.group_index[pc('B8')], go.MISSING_GROUP_ID)
     self.assertEqual(captured, pc_set('B8'))
 def test_capture_stone(self):
     board = load_board('''
         .X.......
         XO.......
         .X.......
     ''' + EMPTY_ROW * 6)
     lib_tracker = LibertyTracker.from_board(board)
     captured = lib_tracker.add_stone(BLACK, pc('C8'))
     self.assertEqual(len(lib_tracker.groups), 4)
     self.assertEqual(lib_tracker.group_index[pc('B8')],
                      go.MISSING_GROUP_ID)
     self.assertEqual(captured, pc_set('B8'))
    def test_lib_tracker_init(self):
        board = load_board('X........' + EMPTY_ROW * 8)

        lib_tracker = LibertyTracker.from_board(board)
        self.assertEqual(len(lib_tracker.groups), 1)
        self.assertNotEqual(lib_tracker.group_index[pc('A9')],
                            go.MISSING_GROUP_ID)
        self.assertEqual(lib_tracker.liberty_cache[pc('A9')], 2)
        sole_group = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
        self.assertEqual(sole_group.stones, pc_set('A9'))
        self.assertEqual(sole_group.liberties, pc_set('B9 A8'))
        self.assertEqual(sole_group.color, BLACK)
    def test_same_friendly_group_neighboring_twice(self):
        board = load_board('''
            XX.......
            X........
        ''' + EMPTY_ROW * 7)

        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(BLACK, pc('B8'))
        self.assertEqual(len(lib_tracker.groups), 1)
        sole_group_id = lib_tracker.group_index[pc('A9')]
        sole_group = lib_tracker.groups[sole_group_id]
        self.assertEqual(sole_group.stones, pc_set('A9 B9 A8 B8'))
        self.assertEqual(sole_group.liberties, pc_set('C9 C8 A7 B7'))
        self.assertEqual(captured, set())
Beispiel #13
0
    def test_same_friendly_group_neighboring_twice(self):
        board = load_board('''
            XX.......
            X........
        ''' + EMPTY_ROW * 7)

        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(BLACK, pc('B8'))
        self.assertEqual(len(lib_tracker.groups), 1)
        sole_group_id = lib_tracker.group_index[pc('A9')]
        sole_group = lib_tracker.groups[sole_group_id]
        self.assertEqual(sole_group.stones, pc_set('A9 B9 A8 B8'))
        self.assertEqual(sole_group.liberties, pc_set('C9 C8 A7 B7'))
        self.assertEqual(captured, set())
Beispiel #14
0
 def test_chinese_handicap_handling(self):
     intermediate_board = load_board('''
         .........
         .........
         ......X..
         .........
         .........
         .........
         .........
         .........
         .........
     ''')
     intermediate_position = go.Position(
         intermediate_board,
         n=1,
         komi=5.5,
         caps=(0, 0),
         recent=(go.PlayerMove(go.BLACK, pc('G7')), ),
         to_play=go.BLACK,
     )
     final_board = load_board('''
         ....OX...
         .O.OOX...
         O.O.X.X..
         .OXXX....
         OX...XX..
         .X.XXO...
         X.XOOXXX.
         XXXO.OOX.
         .XOOX.O..
     ''')
     final_position = go.Position(final_board,
                                  n=50,
                                  komi=5.5,
                                  caps=(7, 2),
                                  ko=None,
                                  recent=(
                                      go.PlayerMove(go.WHITE, pc('E9')),
                                      go.PlayerMove(go.BLACK, pc('F9')),
                                  ),
                                  to_play=go.WHITE)
     positions_w_context = list(replay_sgf(CHINESE_HANDICAP_SGF))
     self.assertEqualPositions(intermediate_position,
                               positions_w_context[1].position)
     self.assertEqual(positions_w_context[1].next_move, pc('C3'))
     self.assertEqualPositions(final_position,
                               positions_w_context[-1].position)
     self.assertFalse(positions_w_context[-1].is_usable())
     self.assertTrue(positions_w_context[-2].is_usable())
Beispiel #15
0
    def test_japanese_handicap_handling(self):
        intermediate_board = load_board('''
            .........
            .........
            ......X..
            .........
            ....O....
            .........
            ..X......
            .........
            .........
        ''')
        intermediate_position = go.Position(
            intermediate_board,
            n=1,
            komi=5.5,
            caps=(0, 0),
            recent=(go.PlayerMove(go.WHITE, pc('E5')), ),
            to_play=go.BLACK,
        )
        final_board = load_board('''
            .........
            .........
            ......X..
            .........
            ....O....
            .........
            ..XX.....
            .........
            .........
        ''')
        final_position = go.Position(
            final_board,
            n=2,
            komi=5.5,
            caps=(0, 0),
            recent=(
                go.PlayerMove(go.WHITE, pc('E5')),
                go.PlayerMove(go.BLACK, pc('D3')),
            ),
            to_play=go.WHITE,
        )

        positions_w_context = list(replay_sgf(JAPANESE_HANDICAP_SGF))
        self.assertEqualPositions(intermediate_position,
                                  positions_w_context[1].position)
        self.assertEqualPositions(final_position,
                                  positions_w_context[-1].position)
Beispiel #16
0
def handle_node(pos, node):
    'A node can either add B+W stones, play as B, or play as W.'
    props = node.properties
    black_stones_added = [pc(coords) for coords in props.get('AB', [])]
    white_stones_added = [pc(coords) for coords in props.get('AW', [])]
    if black_stones_added or white_stones_added:
        return add_stones(pos, black_stones_added, white_stones_added)
    # If B/W props are not present, then there is no move. But if it is present and equal to the empty string, then the move was a pass.
    elif 'B' in props:
        black_move = pc(props.get('B', [''])[0])
        return pos.play_move(black_move, color=go.BLACK)
    elif 'W' in props:
        white_move = pc(props.get('W', [''])[0])
        return pos.play_move(white_move, color=go.WHITE)
    else:
        return pos
Beispiel #17
0
def handle_node(pos, node):
    'A node can either add B+W stones, play as B, or play as W.'
    props = node.properties
    black_stones_added = [pc(coords) for coords in props.get('AB', [])]
    white_stones_added = [pc(coords) for coords in props.get('AW', [])]
    if black_stones_added or white_stones_added:
        return add_stones(pos, black_stones_added, white_stones_added)
    # If B/W props are not present, then there is no move. But if it is present and equal to the empty string, then the move was a pass.
    elif 'B' in props:
        black_move = pc(props.get('B', [''])[0])
        return pos.play_move(black_move, color=go.BLACK)
    elif 'W' in props:
        white_move = pc(props.get('W', [''])[0])
        return pos.play_move(white_move, color=go.WHITE)
    else:
        return pos
Beispiel #18
0
 def test_chinese_handicap_handling(self):
     intermediate_board = load_board('''
         .........
         .........
         ......X..
         .........
         .........
         .........
         .........
         .........
         .........
     ''')
     intermediate_position = go.Position(
         intermediate_board,
         n=1,
         komi=5.5,
         caps=(0, 0),
         recent=(go.PlayerMove(go.BLACK, pc('G7')),),
         to_play=go.BLACK,
     )
     final_board = load_board('''
         ....OX...
         .O.OOX...
         O.O.X.X..
         .OXXX....
         OX...XX..
         .X.XXO...
         X.XOOXXX.
         XXXO.OOX.
         .XOOX.O..
     ''')
     final_position = go.Position(
         final_board,
         n=50,
         komi=5.5,
         caps=(7, 2),
         ko=None,
         recent=(go.PlayerMove(go.WHITE, pc('E9')),
                 go.PlayerMove(go.BLACK, pc('F9')),),
         to_play=go.WHITE
     )
     positions_w_context = list(replay_sgf(CHINESE_HANDICAP_SGF))
     self.assertEqualPositions(intermediate_position, positions_w_context[1].position)
     self.assertEqual(positions_w_context[1].next_move, pc('C3'))
     self.assertEqualPositions(final_position, positions_w_context[-1].position)
     self.assertFalse(positions_w_context[-1].is_usable())
     self.assertTrue(positions_w_context[-2].is_usable())
Beispiel #19
0
    def test_merge_multiple_groups(self):
        board = load_board('''
            .X.......
            X.X......
            .X.......
        ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(board)
        lib_tracker.add_stone(BLACK, pc('B8'))
        self.assertEqual(len(lib_tracker.groups), 1)
        self.assertNotEqual(lib_tracker.group_index[pc('B8')], go.MISSING_GROUP_ID)
        sole_group = lib_tracker.groups[lib_tracker.group_index[pc('B8')]]
        self.assertEqual(sole_group.stones, pc_set('B9 A8 B8 C8 B7'))
        self.assertEqual(sole_group.liberties, pc_set('A9 C9 D8 A7 C7 B6'))
        self.assertEqual(sole_group.color, BLACK)

        liberty_cache = lib_tracker.liberty_cache
        for stone in sole_group.stones:
            self.assertEqual(liberty_cache[stone], 6, str(stone))
    def test_same_opponent_group_neighboring_twice(self):
        board = load_board('''
            XX.......
            X........
        ''' + EMPTY_ROW * 7)

        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(WHITE, pc('B8'))
        self.assertEqual(len(lib_tracker.groups), 2)
        black_group = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
        self.assertEqual(black_group.stones, pc_set('A9 B9 A8'))
        self.assertEqual(black_group.liberties, pc_set('C9 A7'))

        white_group = lib_tracker.groups[lib_tracker.group_index[pc('B8')]]
        self.assertEqual(white_group.stones, pc_set('B8'))
        self.assertEqual(white_group.liberties, pc_set('C8 B7'))

        self.assertEqual(captured, set())
Beispiel #21
0
    def test_same_opponent_group_neighboring_twice(self):
        board = load_board('''
            XX.......
            X........
        ''' + EMPTY_ROW * 7)

        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(WHITE, pc('B8'))
        self.assertEqual(len(lib_tracker.groups), 2)
        black_group = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
        self.assertEqual(black_group.stones, pc_set('A9 B9 A8'))
        self.assertEqual(black_group.liberties, pc_set('C9 A7'))

        white_group = lib_tracker.groups[lib_tracker.group_index[pc('B8')]]
        self.assertEqual(white_group.stones, pc_set('B8'))
        self.assertEqual(white_group.liberties, pc_set('C8 B7'))

        self.assertEqual(captured, set())
Beispiel #22
0
    def test_japanese_handicap_handling(self):
        intermediate_board = load_board('''
            .........
            .........
            ......X..
            .........
            ....O....
            .........
            ..X......
            .........
            .........
        ''')
        intermediate_position = go.Position(
            intermediate_board,
            n=1,
            komi=5.5,
            caps=(0, 0),
            recent=(go.PlayerMove(go.WHITE, pc('E5')),),
            to_play=go.BLACK,
        )
        final_board = load_board('''
            .........
            .........
            ......X..
            .........
            ....O....
            .........
            ..XX.....
            .........
            .........
        ''')
        final_position = go.Position(
            final_board,
            n=2,
            komi=5.5,
            caps=(0, 0),
            recent=(go.PlayerMove(go.WHITE, pc('E5')),
                    go.PlayerMove(go.BLACK, pc('D3')),),
            to_play=go.WHITE,
        )

        positions_w_context = list(replay_sgf(JAPANESE_HANDICAP_SGF))
        self.assertEqualPositions(intermediate_position, positions_w_context[1].position)
        self.assertEqualPositions(final_position, positions_w_context[-1].position)
    def test_merge_multiple_groups(self):
        board = load_board('''
            .X.......
            X.X......
            .X.......
        ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(board)
        lib_tracker.add_stone(BLACK, pc('B8'))
        self.assertEqual(len(lib_tracker.groups), 1)
        self.assertNotEqual(lib_tracker.group_index[pc('B8')],
                            go.MISSING_GROUP_ID)
        sole_group = lib_tracker.groups[lib_tracker.group_index[pc('B8')]]
        self.assertEqual(sole_group.stones, pc_set('B9 A8 B8 C8 B7'))
        self.assertEqual(sole_group.liberties, pc_set('A9 C9 D8 A7 C7 B6'))
        self.assertEqual(sole_group.color, BLACK)

        liberty_cache = lib_tracker.liberty_cache
        for stone in sole_group.stones:
            self.assertEqual(liberty_cache[stone], 6, str(stone))
Beispiel #24
0
    def test_move(self):
        start_position = Position(
            board=TEST_BOARD,
            n=0,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=tuple(),
            to_play=BLACK,
        )
        expected_board = load_board('''
            .XX....OO
            X........
        ''' + EMPTY_ROW * 7)
        expected_position = Position(
            board=expected_board,
            n=1,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=(PlayerMove(BLACK, pc('C9')),),
            to_play=WHITE,
        )
        actual_position = start_position.play_move(pc('C9'))
        self.assertEqualPositions(actual_position, expected_position)

        expected_board2 = load_board('''
            .XX....OO
            X.......O
        ''' + EMPTY_ROW * 7)
        expected_position2 = Position(
            board=expected_board2,
            n=2,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=(PlayerMove(BLACK, pc('C9')), PlayerMove(WHITE, pc('J8'))),
            to_play=BLACK,
        )
        actual_position2 = actual_position.play_move(pc('J8'))
        self.assertEqualPositions(actual_position2, expected_position2)
    def test_move(self):
        start_position = Position(
            board=TEST_BOARD,
            n=0,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=tuple(),
            to_play=BLACK,
        )
        expected_board = load_board('''
            .XX....OO
            X........
        ''' + EMPTY_ROW * 7)
        expected_position = Position(
            board=expected_board,
            n=1,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=(PlayerMove(BLACK, pc('C9')), ),
            to_play=WHITE,
        )
        actual_position = start_position.play_move(pc('C9'))
        self.assertEqualPositions(actual_position, expected_position)

        expected_board2 = load_board('''
            .XX....OO
            X.......O
        ''' + EMPTY_ROW * 7)
        expected_position2 = Position(
            board=expected_board2,
            n=2,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=(PlayerMove(BLACK, pc('C9')), PlayerMove(WHITE, pc('J8'))),
            to_play=BLACK,
        )
        actual_position2 = actual_position.play_move(pc('J8'))
        self.assertEqualPositions(actual_position2, expected_position2)
Beispiel #26
0
    def test_ko_move_mutable_board(self):
        start_board = load_board('''
            .OX......
            OX.......
        ''' + EMPTY_ROW * 7)
        start_position = Position(
            board=start_board,
            n=0,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=tuple(),
            to_play=BLACK,
        )
        expected_board = load_board('''
            X.X......
            OX.......
        ''' + EMPTY_ROW * 7)
        expected_position = Position(
            board=expected_board,
            n=1,
            komi=6.5,
            caps=(2, 2),
            ko=pc('B9'),
            recent=(PlayerMove(BLACK, pc('A9')),),
            to_play=WHITE,
        )
        actual_position = start_position.play_move(pc('A9'), mutate=True)

        self.assertEqualPositions(actual_position, expected_position)

        # Check that retaking ko is illegal until two intervening moves
        with self.assertRaises(go.IllegalMove):
            actual_position.play_move(pc('B9'), mutate=True)
        pass_twice = actual_position.pass_move(mutate=True).pass_move(mutate=True)
        ko_delayed_retake = pass_twice.play_move(pc('B9'), mutate=True)
        expected_position = Position(
            board=start_board,
            n=4,
            komi=6.5,
            caps=(2, 3),
            ko=pc('A9'),
            recent=(
                PlayerMove(BLACK, pc('A9')),
                PlayerMove(WHITE, None),
                PlayerMove(BLACK, None),
                PlayerMove(WHITE, pc('B9'))),
            to_play=BLACK,
        )
        self.assertEqualPositions(ko_delayed_retake, expected_position)
Beispiel #27
0
    def test_ko_move_mutable_board(self):
        start_board = load_board('''
            .OX......
            OX.......
        ''' + EMPTY_ROW * 7)
        start_position = Position(
            board=start_board,
            n=0,
            komi=6.5,
            caps=(1, 2),
            ko=None,
            recent=tuple(),
            to_play=BLACK,
        )
        expected_board = load_board('''
            X.X......
            OX.......
        ''' + EMPTY_ROW * 7)
        expected_position = Position(
            board=expected_board,
            n=1,
            komi=6.5,
            caps=(2, 2),
            ko=pc('B9'),
            recent=(PlayerMove(BLACK, pc('A9')), ),
            to_play=WHITE,
        )
        actual_position = start_position.play_move(pc('A9'), mutate=True)

        self.assertEqualPositions(actual_position, expected_position)

        # Check that retaking ko is illegal until two intervening moves
        with self.assertRaises(go.IllegalMove):
            actual_position.play_move(pc('B9'), mutate=True)
        pass_twice = actual_position.pass_move(mutate=True).pass_move(
            mutate=True)
        ko_delayed_retake = pass_twice.play_move(pc('B9'), mutate=True)
        expected_position = Position(
            board=start_board,
            n=4,
            komi=6.5,
            caps=(2, 3),
            ko=pc('A9'),
            recent=(PlayerMove(BLACK, pc('A9')), PlayerMove(WHITE, None),
                    PlayerMove(BLACK, None), PlayerMove(WHITE, pc('B9'))),
            to_play=BLACK,
        )
        self.assertEqualPositions(ko_delayed_retake, expected_position)
Beispiel #28
0
    def test_capture_multiple_groups(self):
        board = load_board('''
            .OX......
            OXX......
            XX.......
        ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(BLACK, pc('A9'))
        self.assertEqual(len(lib_tracker.groups), 2)
        self.assertEqual(captured, pc_set('B9 A8'))

        corner_stone = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
        self.assertEqual(corner_stone.stones, pc_set('A9'))
        self.assertEqual(corner_stone.liberties, pc_set('B9 A8'))

        surrounding_stones = lib_tracker.groups[lib_tracker.group_index[pc('C9')]]
        self.assertEqual(surrounding_stones.stones, pc_set('C9 B8 C8 A7 B7'))
        self.assertEqual(surrounding_stones.liberties, pc_set('B9 D9 A8 D8 C7 A6 B6'))

        liberty_cache = lib_tracker.liberty_cache
        for stone in corner_stone.stones:
            self.assertEqual(liberty_cache[stone], 2, str(stone))
        for stone in surrounding_stones.stones:
            self.assertEqual(liberty_cache[stone], 7, str(stone))
Beispiel #29
0
 def test_place_stone_opposite_color(self):
     board = load_board('X........' + EMPTY_ROW * 8)
     lib_tracker = LibertyTracker.from_board(board)
     lib_tracker.add_stone(WHITE, pc('B9'))
     self.assertEqual(len(lib_tracker.groups), 2)
     self.assertNotEqual(lib_tracker.group_index[pc('A9')], go.MISSING_GROUP_ID)
     self.assertNotEqual(lib_tracker.group_index[pc('B9')], go.MISSING_GROUP_ID)
     self.assertEqual(lib_tracker.liberty_cache[pc('A9')], 1)
     self.assertEqual(lib_tracker.liberty_cache[pc('B9')], 2)
     black_group = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
     white_group = lib_tracker.groups[lib_tracker.group_index[pc('B9')]]
     self.assertEqual(black_group.stones, pc_set('A9'))
     self.assertEqual(black_group.liberties, pc_set('A8'))
     self.assertEqual(black_group.color, BLACK)
     self.assertEqual(white_group.stones, pc_set('B9'))
     self.assertEqual(white_group.liberties, pc_set('C9 B8'))
     self.assertEqual(white_group.color, WHITE)
    def test_legal_moves(self):
        board = load_board('''
            .XXXXXXXO
            XX.OOOOO.
            OOOOOOOOO
            XXXXXXXX.
            OOOOOOOOO
            XXXXXXXXX
            XXXXXXXXX
            XXXXXXXXX
            XXXXXXXX.
        ''')
        position = Position(
            board=board,
            n=0,
            komi=6.5,
            caps=(0, 0),
            ko=pc('J8'),
            recent=tuple(),
            to_play=BLACK,
        )
        empty_spots = pc_set('A9 C8 J8 J6 J1')
        B_legal_moves = pc_set('A9 C8 J6')
        for move in empty_spots:
            if move not in B_legal_moves:
                with self.assertRaises(go.IllegalMove):
                    position.play_move(move)
            else:
                position.play_move(move)
        # pass should also be legal
        position.play_move(None)

        pass_position = position.pass_move()
        W_legal_moves = pc_set('C8 J8 J6 J1')
        for move in empty_spots:
            if move not in W_legal_moves:
                with self.assertRaises(go.IllegalMove):
                    pass_position.play_move(move)
            else:
                pass_position.play_move(move)
        # pass should also be legal
        pass_position.play_move(None)
Beispiel #31
0
    def test_legal_moves(self):
        board = load_board('''
            .XXXXXXXO
            XX.OOOOO.
            OOOOOOOOO
            XXXXXXXX.
            OOOOOOOOO
            XXXXXXXXX
            XXXXXXXXX
            XXXXXXXXX
            XXXXXXXX.
        ''')
        position = Position(
            board=board,
            n=0,
            komi=6.5,
            caps=(0, 0),
            ko=pc('J8'),
            recent=tuple(),
            to_play=BLACK,
        )
        empty_spots = pc_set('A9 C8 J8 J6 J1')
        B_legal_moves = pc_set('A9 C8 J6')
        for move in empty_spots:
            if move not in B_legal_moves:
                with self.assertRaises(go.IllegalMove):
                    position.play_move(move)
            else:
                position.play_move(move)
        # pass should also be legal
        position.play_move(None)

        pass_position = position.pass_move()
        W_legal_moves = pc_set('C8 J8 J6 J1')
        for move in empty_spots:
            if move not in W_legal_moves:
                with self.assertRaises(go.IllegalMove):
                    pass_position.play_move(move)
            else:
                pass_position.play_move(move)
        # pass should also be legal
        pass_position.play_move(None)
 def test_passing(self):
     start_position = Position(
         board=TEST_BOARD,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=pc('A1'),
         recent=tuple(),
         to_play=BLACK,
     )
     expected_position = Position(
         board=TEST_BOARD,
         n=1,
         komi=6.5,
         caps=(1, 2),
         ko=None,
         recent=(PlayerMove(BLACK, None), ),
         to_play=WHITE,
     )
     pass_position = start_position.pass_move()
     self.assertEqualPositions(pass_position, expected_position)
 def test_place_stone_opposite_color(self):
     board = load_board('X........' + EMPTY_ROW * 8)
     lib_tracker = LibertyTracker.from_board(board)
     lib_tracker.add_stone(WHITE, pc('B9'))
     self.assertEqual(len(lib_tracker.groups), 2)
     self.assertNotEqual(lib_tracker.group_index[pc('A9')],
                         go.MISSING_GROUP_ID)
     self.assertNotEqual(lib_tracker.group_index[pc('B9')],
                         go.MISSING_GROUP_ID)
     self.assertEqual(lib_tracker.liberty_cache[pc('A9')], 1)
     self.assertEqual(lib_tracker.liberty_cache[pc('B9')], 2)
     black_group = lib_tracker.groups[lib_tracker.group_index[pc('A9')]]
     white_group = lib_tracker.groups[lib_tracker.group_index[pc('B9')]]
     self.assertEqual(black_group.stones, pc_set('A9'))
     self.assertEqual(black_group.liberties, pc_set('A8'))
     self.assertEqual(black_group.color, BLACK)
     self.assertEqual(white_group.stones, pc_set('B9'))
     self.assertEqual(white_group.liberties, pc_set('C9 B8'))
     self.assertEqual(white_group.color, WHITE)
 def test_flipturn(self):
     start_position = Position(
         board=TEST_BOARD,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=pc('A1'),
         recent=tuple(),
         to_play=BLACK,
     )
     expected_position = Position(
         board=TEST_BOARD,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=None,
         recent=tuple(),
         to_play=WHITE,
     )
     flip_position = start_position.flip_playerturn()
     self.assertEqualPositions(flip_position, expected_position)
Beispiel #35
0
 def test_flipturn(self):
     start_position = Position(
         board=TEST_BOARD,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=pc('A1'),
         recent=tuple(),
         to_play=BLACK,
     )
     expected_position = Position(
         board=TEST_BOARD,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=None,
         recent=tuple(),
         to_play=WHITE,
     )
     flip_position = start_position.flip_playerturn()
     self.assertEqualPositions(flip_position, expected_position)
Beispiel #36
0
 def test_passing(self):
     start_position = Position(
         board=TEST_BOARD,
         n=0,
         komi=6.5,
         caps=(1, 2),
         ko=pc('A1'),
         recent=tuple(),
         to_play=BLACK,
     )
     expected_position = Position(
         board=TEST_BOARD,
         n=1,
         komi=6.5,
         caps=(1, 2),
         ko=None,
         recent=(PlayerMove(BLACK, None),),
         to_play=WHITE,
     )
     pass_position = start_position.pass_move()
     self.assertEqualPositions(pass_position, expected_position)
    def test_capture_many(self):
        board = load_board('''
            .XX......
            XOO......
            .XX......
        ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(BLACK, pc('D8'))
        self.assertEqual(len(lib_tracker.groups), 4)
        self.assertEqual(lib_tracker.group_index[pc('B8')],
                         go.MISSING_GROUP_ID)
        self.assertEqual(captured, pc_set('B8 C8'))

        left_group = lib_tracker.groups[lib_tracker.group_index[pc('A8')]]
        self.assertEqual(left_group.stones, pc_set('A8'))
        self.assertEqual(left_group.liberties, pc_set('A9 B8 A7'))

        right_group = lib_tracker.groups[lib_tracker.group_index[pc('D8')]]
        self.assertEqual(right_group.stones, pc_set('D8'))
        self.assertEqual(right_group.liberties, pc_set('D9 C8 E8 D7'))

        top_group = lib_tracker.groups[lib_tracker.group_index[pc('B9')]]
        self.assertEqual(top_group.stones, pc_set('B9 C9'))
        self.assertEqual(top_group.liberties, pc_set('A9 D9 B8 C8'))

        bottom_group = lib_tracker.groups[lib_tracker.group_index[pc('B7')]]
        self.assertEqual(bottom_group.stones, pc_set('B7 C7'))
        self.assertEqual(bottom_group.liberties, pc_set('B8 C8 A7 D7 B6 C6'))

        liberty_cache = lib_tracker.liberty_cache
        for stone in top_group.stones:
            self.assertEqual(liberty_cache[stone], 4, str(stone))
        for stone in left_group.stones:
            self.assertEqual(liberty_cache[stone], 3, str(stone))
        for stone in right_group.stones:
            self.assertEqual(liberty_cache[stone], 4, str(stone))
        for stone in bottom_group.stones:
            self.assertEqual(liberty_cache[stone], 6, str(stone))
        for stone in captured:
            self.assertEqual(liberty_cache[stone], 0, str(stone))
Beispiel #38
0
    def test_capture_many(self):
        board = load_board('''
            .XX......
            XOO......
            .XX......
        ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(board)
        captured = lib_tracker.add_stone(BLACK, pc('D8'))
        self.assertEqual(len(lib_tracker.groups), 4)
        self.assertEqual(lib_tracker.group_index[pc('B8')], go.MISSING_GROUP_ID)
        self.assertEqual(captured, pc_set('B8 C8'))

        left_group = lib_tracker.groups[lib_tracker.group_index[pc('A8')]]
        self.assertEqual(left_group.stones, pc_set('A8'))
        self.assertEqual(left_group.liberties, pc_set('A9 B8 A7'))

        right_group = lib_tracker.groups[lib_tracker.group_index[pc('D8')]]
        self.assertEqual(right_group.stones, pc_set('D8'))
        self.assertEqual(right_group.liberties, pc_set('D9 C8 E8 D7'))

        top_group = lib_tracker.groups[lib_tracker.group_index[pc('B9')]]
        self.assertEqual(top_group.stones, pc_set('B9 C9'))
        self.assertEqual(top_group.liberties, pc_set('A9 D9 B8 C8'))

        bottom_group = lib_tracker.groups[lib_tracker.group_index[pc('B7')]]
        self.assertEqual(bottom_group.stones, pc_set('B7 C7'))
        self.assertEqual(bottom_group.liberties, pc_set('B8 C8 A7 D7 B6 C6'))

        liberty_cache = lib_tracker.liberty_cache
        for stone in top_group.stones:
            self.assertEqual(liberty_cache[stone], 4, str(stone))
        for stone in left_group.stones:
            self.assertEqual(liberty_cache[stone], 3, str(stone))
        for stone in right_group.stones:
            self.assertEqual(liberty_cache[stone], 4, str(stone))
        for stone in bottom_group.stones:
            self.assertEqual(liberty_cache[stone], 6, str(stone))
        for stone in captured:
            self.assertEqual(liberty_cache[stone], 0, str(stone))
 def test_is_koish(self):
     self.assertEqual(go.is_koish(TEST_BOARD, pc('A9')), BLACK)
     self.assertEqual(go.is_koish(TEST_BOARD, pc('B8')), None)
     self.assertEqual(go.is_koish(TEST_BOARD, pc('B9')), None)
     self.assertEqual(go.is_koish(TEST_BOARD, pc('E5')), None)
Beispiel #40
0
def print_c(a,m):
  mi,m0,m1,m2 = m
  for i,x in a:
    sc("C"); pc("%%%dd. "%mi%(i+1))
    x = x.split("\t")
    sc("Y"); pc("%%-%ds"%(m0+1)%x[0])
    sc("W"); pc("%%-%ds"%(m1+1)%x[1])
    if len(x)>2: sc("M"); pc("%%-%ds"%(m2+1)%x[2])
    sc("w")
    if len(x)>3: pc(" ".join(x[3:]))
    pc()
Beispiel #41
0
 def test_parsing(self):
     self.assertEqual(pc('A9'), (0, 0))
     self.assertEqual(parse_sgf_coords('aa'), (0, 0))
     self.assertEqual(pc('A3'), (6, 0))
     self.assertEqual(parse_sgf_coords('ac'), (2, 0))
     self.assertEqual(pc('D4'), parse_sgf_coords('df'))
 def test_parsing(self):
     self.assertEqual(pc('A9'), (0, 0))
     self.assertEqual(parse_sgf_coords('aa'), (0, 0))
     self.assertEqual(pc('A3'), (6, 0))
     self.assertEqual(parse_sgf_coords('ac'), (2, 0))
     self.assertEqual(pc('D4'), parse_sgf_coords('df'))
Beispiel #43
0
        if r in t:
          print( "duplicated record" )
        else:
          t.append( r )
          print( "1 record added" )
          t = [l.strip() for l in t if len(l.strip())>0]
          if z: print( "File '%s' will be created" % PWFILE )
          z = False
          write_pw_file( PWFILE, p, t )
          print(len(t),"records written")
      elif o.startswith("d "):
        delrecs = sorted( list( set( int(n) for n in o[2:].split() if 0<int(n)<=len(t) ) ) )
        a = [(n,t[n-1]) for n in delrecs]
        mi,m0 = lengths(a)[:2]
        for n in delrecs:
          x,y = t[n-1].split("\t")[:2]
          sc("C"); pc("%%%dd. "%mi%n); sc("Y"); pc("%%-%ds "%m0%x); sc("W"); pc(y+"\n")
        sc("w")
        for n in delrecs[::-1]: # in desc. order! it's important for 'del'
          del t[n-1]
        print(len(delrecs),"records deleted,",len(t),"records left")
        write_pw_file( PWFILE, p, t )
        z = len(t)==0
      o = input("> ")
  except KeyboardInterrupt:
    pass

else: # help and exit
  help()

Beispiel #44
0
 def test_is_koish(self):
     self.assertEqual(go.is_koish(TEST_BOARD, pc('A9')), BLACK)
     self.assertEqual(go.is_koish(TEST_BOARD, pc('B8')), None)
     self.assertEqual(go.is_koish(TEST_BOARD, pc('B9')), None)
     self.assertEqual(go.is_koish(TEST_BOARD, pc('E5')), None)