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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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())
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
    def test_replay_position(self):
        sgf_positions = list(replay_sgf(NO_HANDICAP_SGF))
        initial = sgf_positions[0]
        self.assertEqual(initial.metadata.result, 'W+1.5')
        self.assertEqual(initial.metadata.board_size, 9)
        self.assertEqual(initial.position.komi, 6.5)

        final = sgf_positions[-1].position

        # sanity check to ensure we're working with the right position
        final_board = load_board('''
            .OXX.....
            O.OX.X...
            .OOX.....
            OOOOXXXXX
            XOXXOXOOO
            XOOXOO.O.
            XOXXXOOXO
            XXX.XOXXO
            X..XOO.O.
        ''')
        expected_final_position = go.Position(final_board,
                                              n=62,
                                              komi=6.5,
                                              caps=(3, 2),
                                              ko=None,
                                              recent=tuple(),
                                              to_play=go.BLACK)
        self.assertEqualPositions(expected_final_position, final)
        self.assertEqual(final.n, len(final.recent))

        replayed_positions = list(replay_position(final))
        for sgf_pos, replay_pos in zip(sgf_positions, replayed_positions):
            self.assertEqualPositions(sgf_pos.position, replay_pos.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))
 def test_is_move_suicidal(self):
     board = load_board('''
         ...O.O...
         ....O....
         XO.....O.
         OXO...OXO
         O.XO.OX.O
         OXO...OOX
         XO.......
         ......XXO
         .....XOO.
     ''')
     position = Position(
         board=board,
         to_play=BLACK,
     )
     suicidal_moves = pc_set('E9 H5')
     nonsuicidal_moves = pc_set('B5 J1 A9')
     for move in suicidal_moves:
         assert (position.board[move] == go.EMPTY
                 )  #sanity check my coordinate input
         self.assertTrue(position.is_move_suicidal(move), str(move))
     for move in nonsuicidal_moves:
         assert (position.board[move] == go.EMPTY
                 )  #sanity check my coordinate input
         self.assertFalse(position.is_move_suicidal(move), str(move))
Ejemplo n.º 10
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())
Ejemplo n.º 11
0
    def test_scoring(self):
        board = load_board('''
                .XX......
                OOXX.....
                OOOX...X.
                OXX......
                OOXXXXXX.
                OOOXOXOXX
                .O.OOXOOX
                .O.O.OOXX
                ......OOO
            ''')
        position = Position(
            board=board,
            n=54,
            komi=6.5,
            caps=(2, 5),
            ko=None,
            recent=tuple(),
            to_play=BLACK,
        )
        expected_score = 1.5
        self.assertEqual(position.score(), expected_score)

        board = load_board('''
                XXX......
                OOXX.....
                OOOX...X.
                OXX......
                OOXXXXXX.
                OOOXOXOXX
                .O.OOXOOX
                .O.O.OOXX
                ......OOO
            ''')
        position = Position(
            board=board,
            n=55,
            komi=6.5,
            caps=(2, 5),
            ko=None,
            recent=tuple(),
            to_play=WHITE,
        )
        expected_score = 2.5
        self.assertEqual(position.score(), expected_score)
Ejemplo n.º 12
0
    def test_scoring(self):
            board = load_board('''
                .XX......
                OOXX.....
                OOOX...X.
                OXX......
                OOXXXXXX.
                OOOXOXOXX
                .O.OOXOOX
                .O.O.OOXX
                ......OOO
            ''')
            position = Position(
                board=board,
                n=54,
                komi=6.5,
                caps=(2, 5),
                ko=None,
                recent=tuple(),
                to_play=BLACK,
            )
            expected_score = 1.5
            self.assertEqual(position.score(), expected_score)

            board = load_board('''
                XXX......
                OOXX.....
                OOOX...X.
                OXX......
                OOXXXXXX.
                OOOXOXOXX
                .O.OOXOOX
                .O.O.OOXX
                ......OOO
            ''')
            position = Position(
                board=board,
                n=55,
                komi=6.5,
                caps=(2, 5),
                ko=None,
                recent=tuple(),
                to_play=WHITE,
            )
            expected_score = 2.5
            self.assertEqual(position.score(), expected_score)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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'))
Ejemplo n.º 15
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)
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
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'))
Ejemplo n.º 19
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)
Ejemplo n.º 20
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())
Ejemplo n.º 21
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())
Ejemplo n.º 22
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)
Ejemplo n.º 23
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())
Ejemplo n.º 24
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())
Ejemplo n.º 25
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))
Ejemplo n.º 26
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)
Ejemplo n.º 27
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))
Ejemplo n.º 28
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)
Ejemplo n.º 29
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)
Ejemplo n.º 30
0
 def test_is_eyeish(self):
     board = load_board('''
         .XX...XXX
         X.X...X.X
         XX.....X.
         ........X
         XXXX.....
         OOOX....O
         X.OXX.OO.
         .XO.X.O.O
         XXO.X.OO.
     ''')
     B_eyes = pc_set('A2 A9 B8 J7 H8')
     W_eyes = pc_set('H2 J1 J3')
     not_eyes = pc_set('B3 E5')
     for be in B_eyes:
         self.assertEqual(go.is_eyeish(board, be), BLACK, str(be))
     for we in W_eyes:
         self.assertEqual(go.is_eyeish(board, we), WHITE, str(we))
     for ne in not_eyes:
         self.assertEqual(go.is_eyeish(board, ne), None, str(ne))
Ejemplo n.º 31
0
 def test_is_eyeish(self):
     board = load_board('''
         .XX...XXX
         X.X...X.X
         XX.....X.
         ........X
         XXXX.....
         OOOX....O
         X.OXX.OO.
         .XO.X.O.O
         XXO.X.OO.
     ''')
     B_eyes = pc_set('A2 A9 B8 J7 H8')
     W_eyes = pc_set('H2 J1 J3')
     not_eyes = pc_set('B3 E5')
     for be in B_eyes:
         self.assertEqual(go.is_eyeish(board, be), BLACK, str(be))
     for we in W_eyes:
         self.assertEqual(go.is_eyeish(board, we), WHITE, str(we))
     for ne in not_eyes:
         self.assertEqual(go.is_eyeish(board, ne), None, str(ne))
Ejemplo n.º 32
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))
Ejemplo n.º 33
0
 def test_is_move_reasonable(self):
     board = load_board('''
         .XXOOOXXX
         X.XO.OX.X
         XXXOOOXX.
         ...XXX..X
         XXXX.....
         OOOX....O
         X.OXX.OO.
         .XO.X.O.O
         XXO.X.OO.
     ''')
     position = Position(
         board=board,
         to_play=BLACK,
     )
     reasonable_moves = pc_set('E8 B3')
     unreasonable_moves = pc_set('A9 B8 H8 J7 A2 J3 H2 J1')
     for move in reasonable_moves:
         self.assertTrue(is_move_reasonable(position, move), str(move))
     for move in unreasonable_moves:
         self.assertFalse(is_move_reasonable(position, move), str(move))
Ejemplo n.º 34
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))
Ejemplo n.º 35
0
 def test_is_move_reasonable(self):
     board = load_board('''
         .XXOOOXXX
         X.XO.OX.X
         XXXOOOXX.
         ...XXX..X
         XXXX.....
         OOOX....O
         X.OXX.OO.
         .XO.X.O.O
         XXO.X.OO.
     ''')
     position = Position(
         board=board,
         to_play=BLACK,
     )
     reasonable_moves = pc_set('E8 B3')
     unreasonable_moves = pc_set('A9 B8 H8 J7 A2 J3 H2 J1')
     for move in reasonable_moves:
         self.assertTrue(is_move_reasonable(position, move), str(move))
     for move in unreasonable_moves:
         self.assertFalse(is_move_reasonable(position, move), str(move))
Ejemplo n.º 36
0
    def test_replay_position(self):
        sgf_positions = list(replay_sgf(NO_HANDICAP_SGF))
        initial = sgf_positions[0]
        self.assertEqual(initial.metadata.result, 'W+1.5')
        self.assertEqual(initial.metadata.board_size, 9)
        self.assertEqual(initial.position.komi, 6.5)

        final = sgf_positions[-1].position

        # sanity check to ensure we're working with the right position
        final_board = load_board('''
            .OXX.....
            O.OX.X...
            .OOX.....
            OOOOXXXXX
            XOXXOXOOO
            XOOXOO.O.
            XOXXXOOXO
            XXX.XOXXO
            X..XOO.O.
        ''')
        expected_final_position = go.Position(
            final_board,
            n=62,
            komi=6.5,
            caps=(3, 2),
            ko=None,
            recent=tuple(),
            to_play=go.BLACK
        )
        self.assertEqualPositions(expected_final_position, final)
        self.assertEqual(final.n, len(final.recent))

        replayed_positions = list(replay_position(final))
        for sgf_pos, replay_pos in zip(sgf_positions, replayed_positions):
            self.assertEqualPositions(sgf_pos.position, replay_pos.position)
Ejemplo n.º 37
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))
Ejemplo n.º 38
0
 def test_is_move_suicidal(self):
     board = load_board('''
         ...O.O...
         ....O....
         XO.....O.
         OXO...OXO
         O.XO.OX.O
         OXO...OOX
         XO.......
         ......XXO
         .....XOO.
     ''')
     position = Position(
         board=board,
         to_play=BLACK,
     )
     suicidal_moves = pc_set('E9 H5')
     nonsuicidal_moves = pc_set('B5 J1 A9')
     for move in suicidal_moves:
         assert(position.board[move] == go.EMPTY) #sanity check my coordinate input
         self.assertTrue(position.is_move_suicidal(move), str(move))
     for move in nonsuicidal_moves:
         assert(position.board[move] == go.EMPTY) #sanity check my coordinate input
         self.assertFalse(position.is_move_suicidal(move), str(move))
Ejemplo n.º 39
0
 def test_load_board(self):
     self.assertEqualNPArray(go.EMPTY_BOARD, np.zeros([go.N, go.N]))
     self.assertEqualNPArray(go.EMPTY_BOARD, load_board('. \n' * go.N**2))
Ejemplo n.º 40
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import unittest
from go import Position, PlayerMove, LibertyTracker, WHITE, BLACK, EMPTY
import go
from utils import parse_kgs_coords as pc, parse_sgf_coords
from test_utils import GoPositionTestCase, load_board

go.set_board_size(9)

EMPTY_ROW = '.' * go.N + '\n'
TEST_BOARD = load_board('''
.X.....OO
X........
''' + EMPTY_ROW * 7)


def pc_set(string):
    return set(map(pc, string.split()))


class TestGoBoard(GoPositionTestCase):
    def test_load_board(self):
        self.assertEqualNPArray(go.EMPTY_BOARD, np.zeros([go.N, go.N]))
        self.assertEqualNPArray(go.EMPTY_BOARD, load_board('. \n' * go.N**2))

    def test_parsing(self):
        self.assertEqual(pc('A9'), (0, 0))
        self.assertEqual(parse_sgf_coords('aa'), (0, 0))
        self.assertEqual(pc('A3'), (6, 0))
Ejemplo n.º 41
0
import numpy as np

import features
import go
from test_utils import load_board, GoPositionTestCase

go.set_board_size(9)
EMPTY_ROW = '.' * go.N + '\n'
TEST_BOARD = load_board('''
.X.....OO
X........
XXXXXXXXX
''' + EMPTY_ROW * 6)

TEST_POSITION = go.Position(
    board=TEST_BOARD,
    n=0,
    komi=6.5,
    caps=(1,2),
    ko=None,
    recent=(go.PlayerMove(go.BLACK, (0, 1)),
            go.PlayerMove(go.WHITE, (0, 8)),
            go.PlayerMove(go.BLACK, (1, 0))),
    to_play=go.BLACK,
)

TEST_BOARD2 = load_board('''
.XOXXOO..
XO.OXOX..
XXO..X...
''' + EMPTY_ROW * 6)
Ejemplo n.º 42
0
 def test_load_board(self):
     self.assertEqualNPArray(go.EMPTY_BOARD, np.zeros([go.N, go.N]))
     self.assertEqualNPArray(go.EMPTY_BOARD, load_board('. \n' * go.N ** 2))
Ejemplo n.º 43
0
import numpy as np
import unittest
from go import Position, PlayerMove, LibertyTracker, WHITE, BLACK, EMPTY
import go
from utils import parse_kgs_coords as pc, parse_sgf_coords
from test_utils import GoPositionTestCase, load_board

go.set_board_size(9)

EMPTY_ROW = '.' * go.N + '\n'
TEST_BOARD = load_board('''
.X.....OO
X........
''' + EMPTY_ROW * 7)

def pc_set(string):
    return set(map(pc, string.split()))

class TestGoBoard(GoPositionTestCase):
    def test_load_board(self):
        self.assertEqualNPArray(go.EMPTY_BOARD, np.zeros([go.N, go.N]))
        self.assertEqualNPArray(go.EMPTY_BOARD, load_board('. \n' * go.N ** 2))

    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_neighbors(self):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np

import features
import go
from test_utils import load_board, GoPositionTestCase

go.set_board_size(9)
EMPTY_ROW = '.' * go.N + '\n'
TEST_BOARD = load_board('''
.X.....OO
X........
XXXXXXXXX
''' + EMPTY_ROW * 6)

TEST_POSITION = go.Position(
    board=TEST_BOARD,
    n=0,
    komi=6.5,
    caps=(1,2),
    ko=None,
    recent=(go.PlayerMove(go.BLACK, (0, 1)),
            go.PlayerMove(go.WHITE, (0, 8)),
            go.PlayerMove(go.BLACK, (1, 0))),
    to_play=go.BLACK,
)

TEST_BOARD2 = load_board('''
.XOXXOO..
XO.OXOX..