Beispiel #1
0
    def test_capture_multiple_groups(self):
        board = utils_test.load_board('''
      .OX......
      OXX......
      XX.......
    ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(utils_test.BOARD_SIZE, board)
        captured = lib_tracker.add_stone(BLACK, coords.from_kgs(
            utils_test.BOARD_SIZE, 'A9'))
        self.assertEqual(len(lib_tracker.groups), 2)
        self.assertEqual(captured, coords_from_kgs_set('B9 A8'))

        corner_stone = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'A9')]]
        self.assertEqual(corner_stone.stones, coords_from_kgs_set('A9'))
        self.assertEqual(corner_stone.liberties, coords_from_kgs_set('B9 A8'))

        surrounding_stones = lib_tracker.groups[lib_tracker.group_index[
            coords.from_kgs(utils_test.BOARD_SIZE, 'C9')]]
        self.assertEqual(surrounding_stones.stones,
                         coords_from_kgs_set('C9 B8 C8 A7 B7'))
        self.assertEqual(surrounding_stones.liberties,
                         coords_from_kgs_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 #2
0
 def test_place_stone_opposite_color(self):
     board = utils_test.load_board('X........' + EMPTY_ROW * 8)
     lib_tracker = LibertyTracker.from_board(utils_test.BOARD_SIZE, board)
     lib_tracker.add_stone(WHITE, coords.from_kgs(utils_test.BOARD_SIZE, 'B9'))
     self.assertEqual(len(lib_tracker.groups), 2)
     self.assertNotEqual(
         lib_tracker.group_index[coords.from_kgs(
             utils_test.BOARD_SIZE, 'A9')], go.MISSING_GROUP_ID)
     self.assertNotEqual(
         lib_tracker.group_index[coords.from_kgs(
             utils_test.BOARD_SIZE, 'B9')], go.MISSING_GROUP_ID)
     self.assertEqual(lib_tracker.liberty_cache[coords.from_kgs(
         utils_test.BOARD_SIZE, 'A9')], 1)
     self.assertEqual(lib_tracker.liberty_cache[coords.from_kgs(
         utils_test.BOARD_SIZE, 'B9')], 2)
     black_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
         utils_test.BOARD_SIZE, 'A9')]]
     white_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
         utils_test.BOARD_SIZE, 'B9')]]
     self.assertEqual(black_group.stones, coords_from_kgs_set('A9'))
     self.assertEqual(black_group.liberties, coords_from_kgs_set('A8'))
     self.assertEqual(black_group.color, BLACK)
     self.assertEqual(white_group.stones, coords_from_kgs_set('B9'))
     self.assertEqual(white_group.liberties, coords_from_kgs_set('C9 B8'))
     self.assertEqual(white_group.color, WHITE)
Beispiel #3
0
    def test_capture_many(self):
        board = utils_test.load_board('''
      .XX......
      XOO......
      .XX......
    ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(utils_test.BOARD_SIZE, board)
        captured = lib_tracker.add_stone(BLACK, coords.from_kgs(
            utils_test.BOARD_SIZE, 'D8'))
        self.assertEqual(len(lib_tracker.groups), 4)
        self.assertEqual(
            lib_tracker.group_index[coords.from_kgs(
                utils_test.BOARD_SIZE, 'B8')], go.MISSING_GROUP_ID)
        self.assertEqual(captured, coords_from_kgs_set('B8 C8'))

        left_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'A8')]]
        self.assertEqual(left_group.stones, coords_from_kgs_set('A8'))
        self.assertEqual(left_group.liberties,
                         coords_from_kgs_set('A9 B8 A7'))

        right_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'D8')]]
        self.assertEqual(right_group.stones, coords_from_kgs_set('D8'))
        self.assertEqual(right_group.liberties,
                         coords_from_kgs_set('D9 C8 E8 D7'))

        top_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'B9')]]
        self.assertEqual(top_group.stones, coords_from_kgs_set('B9 C9'))
        self.assertEqual(top_group.liberties,
                         coords_from_kgs_set('A9 D9 B8 C8'))

        bottom_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'B7')]]
        self.assertEqual(bottom_group.stones, coords_from_kgs_set('B7 C7'))
        self.assertEqual(bottom_group.liberties,
                         coords_from_kgs_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 #4
0
 def test_capture_stone(self):
     board = utils_test.load_board('''
   .X.......
   XO.......
   .X.......
 ''' + EMPTY_ROW * 6)
     lib_tracker = LibertyTracker.from_board(utils_test.BOARD_SIZE, board)
     captured = lib_tracker.add_stone(BLACK, coords.from_kgs(
         utils_test.BOARD_SIZE, 'C8'))
     self.assertEqual(len(lib_tracker.groups), 4)
     self.assertEqual(
         lib_tracker.group_index[coords.from_kgs(
             utils_test.BOARD_SIZE, 'B8')], go.MISSING_GROUP_ID)
     self.assertEqual(captured, coords_from_kgs_set('B8'))
Beispiel #5
0
    def test_lib_tracker_init(self):
        board = utils_test.load_board('X........' + EMPTY_ROW * 8)

        lib_tracker = LibertyTracker.from_board(utils_test.BOARD_SIZE, board)
        self.assertEqual(len(lib_tracker.groups), 1)
        self.assertNotEqual(
            lib_tracker.group_index[coords.from_kgs(
                utils_test.BOARD_SIZE, 'A9')], go.MISSING_GROUP_ID)
        self.assertEqual(lib_tracker.liberty_cache[coords.from_kgs(
            utils_test.BOARD_SIZE, 'A9')], 2)
        sole_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'A9')]]
        self.assertEqual(sole_group.stones, coords_from_kgs_set('A9'))
        self.assertEqual(sole_group.liberties, coords_from_kgs_set('B9 A8'))
        self.assertEqual(sole_group.color, BLACK)
Beispiel #6
0
    def test_same_friendly_group_neighboring_twice(self):
        board = utils_test.load_board('''
      XX.......
      X........
    ''' + EMPTY_ROW * 7)

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

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

        white_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'B8')]]
        self.assertEqual(white_group.stones, coords_from_kgs_set('B8'))
        self.assertEqual(white_group.liberties, coords_from_kgs_set('C8 B7'))

        self.assertEqual(captured, set())
Beispiel #8
0
    def test_merge_multiple_groups(self):
        board = utils_test.load_board('''
      .X.......
      X.X......
      .X.......
    ''' + EMPTY_ROW * 6)
        lib_tracker = LibertyTracker.from_board(utils_test.BOARD_SIZE, board)
        lib_tracker.add_stone(BLACK, coords.from_kgs(utils_test.BOARD_SIZE, 'B8'))
        self.assertEqual(len(lib_tracker.groups), 1)
        self.assertNotEqual(
            lib_tracker.group_index[coords.from_kgs(
                utils_test.BOARD_SIZE, 'B8')], go.MISSING_GROUP_ID)
        sole_group = lib_tracker.groups[lib_tracker.group_index[coords.from_kgs(
            utils_test.BOARD_SIZE, 'B8')]]
        self.assertEqual(sole_group.stones,
                         coords_from_kgs_set('B9 A8 B8 C8 B7'))
        self.assertEqual(sole_group.liberties,
                         coords_from_kgs_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))