Beispiel #1
0
    def test_detect_need_to_merge_groups(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(1, 1), BoardLocation(1, 2)]
        g1_liberties = [BoardLocation(0, 0)]

        g2_stones = [BoardLocation(1, 3)]
        g2_liberties = [BoardLocation(0, 3), BoardLocation(0, 0)]

        g3_stones = [BoardLocation(1, 5)]
        g3_liberties = [BoardLocation(2, 5), BoardLocation(0, 0)]

        groups = [
            GoString(True, g1_stones, g1_liberties),
            GoString(True, g2_stones, g2_liberties),
            GoString(True, g3_stones, g3_liberties)
        ]
        myBoard.groups = groups

        # Detect friendly neighbors of g1
        groups = myBoard.neighboring_friendly_groups(BoardLocation(0, 0), True)
        self.assertEqual(len(groups), 3)

        groups = myBoard.neighboring_friendly_groups(BoardLocation(0, 0),
                                                     False)
        self.assertEqual(len(groups), 0)
Beispiel #2
0
    def test_place_stone_full_board_merge_groups(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(5, 5)]
        g1_liberties = [
            BoardLocation(5, 4),
            BoardLocation(4, 5),
            BoardLocation(5, 6),
            BoardLocation(6, 5)
        ]

        g2_stones = [BoardLocation(3, 5)]
        g2_liberties = [
            BoardLocation(4, 5),
            BoardLocation(2, 5),
            BoardLocation(3, 6),
            BoardLocation(3, 4)
        ]

        myBoard.groups = [
            GoString(True, g1_stones, g1_liberties),
            GoString(True, g2_stones, g2_liberties)
        ]

        myBoard.place_stone(BoardLocation(4, 5), False)
        self.assertEqual(len(myBoard.groups), 3)
Beispiel #3
0
    def test_legal_merge_groups(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(1, 1), BoardLocation(1, 2)]
        g1_liberties = [BoardLocation(0, 0)]

        g2_stones = [BoardLocation(1, 3)]
        g2_liberties = [BoardLocation(0, 3)]

        g3_stones = [BoardLocation(1, 5)]
        g3_liberties = [BoardLocation(2, 5)]

        groups = [
            GoString(True, g1_stones, g1_liberties),
            GoString(True, g2_stones, g2_liberties),
            GoString(True, g3_stones, g3_liberties)
        ]
        merged_group = myBoard.merge_groups(groups)

        # Check that all three have their items in the merged_group
        self.assertEqual(len(merged_group.stones), 4)
        self.assertEqual(len(merged_group.liberties), 3)
        self.assertEqual(BoardLocation(1, 1) in merged_group.stones, True)
        self.assertEqual(BoardLocation(1, 2) in merged_group.stones, True)
        self.assertEqual(BoardLocation(1, 3) in merged_group.stones, True)
        self.assertEqual(BoardLocation(1, 5) in merged_group.stones, True)

        self.assertEqual(BoardLocation(0, 0) in merged_group.liberties, True)
        self.assertEqual(BoardLocation(0, 3) in merged_group.liberties, True)
        self.assertEqual(BoardLocation(2, 5) in merged_group.liberties, True)
Beispiel #4
0
    def test_legal_place_stone(self):

        myBoard = GoBoard(9, 9)

        did_place = myBoard.place_stone(BoardLocation(8, 8), True)
        self.assertEqual(did_place is None, False)
        self.assertEqual(myBoard.isEmpty(BoardLocation(8, 8)), False)
Beispiel #5
0
    def test_place_stone_empty_board(self):

        myBoard = GoBoard(9, 9)

        myBoard.place_stone(BoardLocation(5, 5), True)
        self.assertEqual(len(myBoard.groups), 1)
        self.assertEqual(myBoard.groups[0].get_num_stones(), 1)
        self.assertEqual(myBoard.groups[0].get_num_liberties(), 4)
Beispiel #6
0
    def test_remove_groups(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(1, 1), BoardLocation(1, 2)]
        g1_liberties = [BoardLocation(0, 0)]

        g1 = GoString(True, g1_stones, g1_liberties)
        myBoard.groups = [g1]
        self.assertEqual(len(myBoard.groups), 1)
        myBoard.remove_group(g1)
        self.assertEqual(len(myBoard.groups), 0)
Beispiel #7
0
    def test_capturing_group(self):

        myBoard = GoBoard(3, 3)

        myBoard.place_stone(BoardLocation(1, 0), False)
        myBoard.place_stone(BoardLocation(3, 0), False)
        myBoard.place_stone(BoardLocation(2, 0), False)

        myBoard.place_stone(BoardLocation(0, 1), True)
        myBoard.place_stone(BoardLocation(1, 0), True)

        self.assertEqual(len(myBoard.groups), 2)

        myBoard.place_stone(BoardLocation(2, 1), True)
        self.assertEqual(len(myBoard.groups), 3)
Beispiel #8
0
    def test_add_illegal_stone_to_group(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(1, 1), BoardLocation(1, 2)]
        g1_liberties = [BoardLocation(0, 0)]

        g1 = GoString(True, g1_stones, g1_liberties)
        myBoard.groups = [g1]

        try:
            myBoard.add_stone_to_group(BoardLocation(0, 5), True, g1)
        except:
            pass
        self.assertEqual(g1.get_num_stones(), 3)
Beispiel #9
0
    def test_add_stone_to_group(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(1, 1), BoardLocation(1, 2)]
        g1_liberties = [BoardLocation(0, 0)]

        g1 = GoString(True, g1_stones, g1_liberties)
        myBoard.groups = [g1]

        myBoard.add_stone_to_group(BoardLocation(0, 0), True, g1)
        self.assertEqual(len(myBoard.groups), 1)
        self.assertEqual(g1.get_num_stones(), 3)
        self.assertEqual(g1.get_num_liberties(), 2)

        g2_stones = [BoardLocation(5, 5), BoardLocation(5, 4)]
        g2_liberties = [BoardLocation(5, 3)]

        g2 = GoString(True, g2_stones, g2_liberties)
        myBoard.groups.append(g2)

        myBoard.add_stone_to_group(BoardLocation(5, 3), True, g2)
        self.assertEqual(len(myBoard.groups), 2)
        self.assertEqual(g2.get_num_stones(), 3)
        self.assertEqual(g2.get_num_liberties(), 3)
Beispiel #10
0
    def test_isOnBoard(self):

        myBoard = GoBoard(9, 9)
        self.assertEqual(myBoard.isOnBoard(BoardLocation(4, 4)), True)
        self.assertEqual(myBoard.isOnBoard(BoardLocation(0, 0)), True)
        self.assertEqual(myBoard.isOnBoard(BoardLocation(8, 8)), True)

        self.assertEqual(myBoard.isOnBoard(BoardLocation(-1, 4)), False)
        self.assertEqual(myBoard.isOnBoard(BoardLocation(4, -1)), False)
        self.assertEqual(myBoard.isOnBoard(BoardLocation(50, 2)), False)
        self.assertEqual(myBoard.isOnBoard(BoardLocation(6, 100)), False)
Beispiel #11
0
    def test_zobrist_hashing(self):

        myBoard = GoBoard(9, 9)

        original_hash = myBoard.zobrist_hashes.current_hash
        myBoard.place_stone(BoardLocation(5, 5), True)
        self.assertNotEqual(myBoard.zobrist_hashes.current_hash, original_hash)
        myBoard.remove_group(myBoard.groups[0])
        self.assertEqual(myBoard.zobrist_hashes.current_hash, original_hash)
Beispiel #12
0
    def test_place_stone_full_board_create_new_group1(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(1, 1), BoardLocation(1, 2)]
        g1_liberties = [BoardLocation(0, 0), BoardLocation(0, 1)]

        g1 = GoString(True, g1_stones, g1_liberties)
        myBoard.groups = [g1]

        myBoard.place_stone(BoardLocation(5, 5), True)
        self.assertEqual(len(myBoard.groups), 2)
        self.assertEqual(g1.get_num_liberties(), 2)

        myBoard.place_stone(BoardLocation(7, 7), True)
        self.assertEqual(len(myBoard.groups), 3)

        myBoard.place_stone(BoardLocation(0, 0), False)
        self.assertEqual(len(myBoard.groups), 4)
        self.assertEqual(g1.get_num_liberties(), 1)
Beispiel #13
0
    def test_illegal_place_stone(self):

        myBoard = GoBoard(9, 9)

        did_place = myBoard.place_stone(BoardLocation(10, 8), True)
        self.assertEqual(did_place is None, True)
        did_place = myBoard.place_stone(BoardLocation(9, 8), True)
        self.assertEqual(did_place is None, True)
        did_place = myBoard.place_stone(BoardLocation(-1, 8), True)
        self.assertEqual(did_place is None, True)

        # Try to place a stone twice
        did_place = myBoard.place_stone(BoardLocation(8, 8), True)
        self.assertEqual(did_place is not None, True)
        did_place = myBoard.place_stone(BoardLocation(8, 8), True)
        self.assertEqual(did_place is None, True)
Beispiel #14
0
    def test_is_move_legal(self):

        myBoard = GoBoard(9, 9)
        isLegal = myBoard.isMoveLegal(BoardLocation(3, 3), True)
        self.assertEqual(isLegal, True)

        isLegal = myBoard.isMoveLegal(BoardLocation(10, 3), True)
        self.assertEqual(isLegal, False)

        isLegal = myBoard.isMoveLegal(BoardLocation(-1, 3), True)
        self.assertEqual(isLegal, False)

        isLegal = myBoard.isMoveLegal(BoardLocation(4, -1), True)
        self.assertEqual(isLegal, False)

        isLegal = myBoard.isMoveLegal(BoardLocation(4, 50), True)
        self.assertEqual(isLegal, False)
Beispiel #15
0
    def test_get_liberties(self):

        myBoard = GoBoard(9, 9)

        # Compute the liberties on the center of the empty board
        liberties = myBoard.get_liberties(BoardLocation(4, 4))
        self.assertEqual(len(liberties), 4)

        # Compute the liberties on the edge of the board
        liberties = myBoard.get_liberties(BoardLocation(8, 8))
        self.assertEqual(len(liberties), 2)

        # Place a piece and then compute the liberties
        did_place = myBoard.place_stone(BoardLocation(4, 4), True)
        liberties = myBoard.get_liberties(BoardLocation(4, 3))
        self.assertEqual(len(liberties), 3)
Beispiel #16
0
    def test_place_stone_that_captures_group(self):

        myBoard = GoBoard(9, 9)

        g1_stones = [BoardLocation(5, 5)]
        g1_liberties = [
            BoardLocation(5, 4),
            BoardLocation(4, 5),
            BoardLocation(5, 6),
            BoardLocation(6, 5)
        ]

        myBoard.groups = [GoString(True, g1_stones, g1_liberties)]

        myBoard.place_stone(BoardLocation(5, 4), False)
        myBoard.place_stone(BoardLocation(4, 5), False)
        myBoard.place_stone(BoardLocation(5, 6), False)
        myBoard.place_stone(BoardLocation(6, 5), False)

        self.assertEqual(len(myBoard.groups), 4)
Beispiel #17
0
    def test_capturing_group(self):

        myBoard = GoBoard(3, 3)

        myBoard.place_stone(BoardLocation(0, 0), False)
        myBoard.place_stone(BoardLocation(0, 2), True)
        myBoard.place_stone(BoardLocation(2, 0), False)

        self.assertEqual(myBoard.groups[0].get_num_liberties(),
                         2)  # 2 liberties
        self.assertEqual(myBoard.groups[1].get_num_liberties(),
                         2)  # 2 liberties
        self.assertEqual(myBoard.groups[2].get_num_liberties(),
                         2)  # 2 liberties

        myBoard.place_stone(BoardLocation(0, 1), True)

        myBoard.place_stone(BoardLocation(1, 0), False)

        self.assertEqual(len(myBoard.groups), 2)

        self.assertEqual(myBoard.groups[0].get_num_liberties(),
                         2)  # 2 liberties
        self.assertEqual(myBoard.groups[1].get_num_liberties(),
                         2)  # 1 liberties, but should be 2

        # This causes a capture of a group but it should not
        myBoard.place_stone(BoardLocation(2, 1), True)
Beispiel #18
0
    def test_place_stone_that_captures_group(self):

        myBoard = GoBoard(9, 9)

        myBoard.place_stone(BoardLocation(5, 5), True)
        myBoard.place_stone(BoardLocation(5, 6), True)
        myBoard.place_stone(BoardLocation(4, 5), True)
        myBoard.place_stone(BoardLocation(5, 4), True)
        myBoard.place_stone(BoardLocation(6, 5), True)

        self.assertEqual(len(myBoard.groups), 1)
        self.assertEqual(myBoard.groups[0].get_num_liberties(), 8)

        myBoard.place_stone(BoardLocation(7, 5), False)
        myBoard.place_stone(BoardLocation(5, 3), False)
        myBoard.place_stone(BoardLocation(3, 5), False)
        myBoard.place_stone(BoardLocation(5, 7), False)

        myBoard.place_stone(BoardLocation(4, 4), False)
        myBoard.place_stone(BoardLocation(6, 6), False)
        myBoard.place_stone(BoardLocation(4, 6), False)
        myBoard.place_stone(BoardLocation(6, 4), False)

        self.assertEqual(len(myBoard.groups), 8)
        num_black_groups = 0
        for group in myBoard.groups:
            if (group.isBlack):
                num_black_groups = num_black_groups + 1
        self.assertEqual(num_black_groups, 0)
Beispiel #19
0
    def test_illegal_merge_groups(self):

        myBoard = GoBoard(9, 9)
        merged_group = myBoard.merge_groups([])
        isNone = (merged_group == None)
        self.assertEqual(isNone, True)