Ejemplo n.º 1
0
    def test_move_if_one_liberty(self):
        string = '''
        XO
        X.
        ..
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        player = CleverVirtualPlayer(state.WHITE)
        state._turn = state.WHITE

        self.assertEqual(player.try_make_move(state, 60), (True, (1, 2), 0))

        string = '''
        .XO
        ...
        ...
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        player = CleverVirtualPlayer(state.WHITE)
        state._turn = state.WHITE

        self.assertEqual(player.try_make_move(state, 60), (True, (2, 1), 0))

        string = '''
        ....
        O#..
        .O#.
        .#..
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        player = CleverVirtualPlayer(state.GREY)
        state._turn = state.GREY

        self.assertEqual(player.try_make_move(state, 60), (True, (0, 2), 1))
Ejemplo n.º 2
0
    def test_get_group(self):
        string = '''
        XX...
        XX...
        .....
        .....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        group, boarder = state.get_group((0, 0))

        self.assertEqual(group, {(0, 0), (0, 1), (1, 0), (1, 1)})
        self.assertEqual(boarder, {(0, 2), (1, 2), (2, 0), (2, 1)})

        string = '''
        XX...
        XO...
        .....
        .....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        group, boarder = state.get_group((0, 0))

        self.assertEqual(group, {(0, 0), (0, 1), (1, 0)})
        self.assertEqual(boarder, {(0, 2), (1, 1), (2, 0)})
Ejemplo n.º 3
0
    def test_try_make_move(self):
        goban = Goban((3, 3))
        state = GameState(goban)
        player = CleverVirtualPlayer(state.GREY)

        state._turn = state.GREY
        points = state.get_free_points()
        move = player.try_make_move(state, 60)
        self.assertEqual(move[0], True)
        self.assertTrue(move[1] in points)

        string = '''
        ..X
        ...
        ...
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        state._turn = state.GREY
        move = player.try_make_move(state, 60)

        self.assertEqual(move[0], True)
        self.assertTrue(move[1] in [(1, 0), (2, 1)])

        string = '''
        .X.
        O.X
        .O.
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        state._turn = state.GREY

        self.assertEqual(player.try_make_move(state, 60), (False, None, 0))
Ejemplo n.º 4
0
    def test_count_liberties(self):
        string = '''
        X....
        .....
        .....
        .....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        self.assertEqual(state.count_liberties((0, 0)), 2)

        string = '''
        X....
        .X...
        .....
        .....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        self.assertEqual(state.count_liberties((1, 1)), 4)

        string = '''
        X....
        XX...
        .....
        .....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        self.assertEqual(state.count_liberties((0, 0)), 4)
Ejemplo n.º 5
0
    def test_convert_string_to_state(self):
        goban = Goban((2, 2))
        state = GameState(goban)

        state._state[(0, 0)] = GameState.BLACK
        state._state[(1, 0)] = GameState.GREY
        state._state[(0, 1)] = GameState.WHITE

        first_string = '''
        X#
        O.
        '''
        first_state = state.convert_string_to_state(first_string, '\n        ')

        second_string = 'X#;O.;'
        second_state = state.convert_string_to_state(second_string, ';')

        self.assertDictEqual(first_state._state, state._state)
        self.assertDictEqual(second_state._state, state._state)
Ejemplo n.º 6
0
    def test_take_stones(self):
        string = '''
        OX...
        OX...
        X....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')

        self.assertEqual(state.take_stones((0, 0)), 2)
Ejemplo n.º 7
0
    def test_check_moves_to_take_stones(self):
        string = '''
        ..X
        ...
        ...
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        player = CleverVirtualPlayer(state.GREY)
        self.assertEqual(player.check_moves_to_take_stones(state, (1, 0)), 2)

        string = '''
        .O.
        #X.
        .O.
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        self.assertEqual(player.check_moves_to_take_stones(state, (2, 1)), 1)

        state = GameState.convert_string_to_state(string, '\n        ')
        self.assertEqual(player.check_moves_to_take_stones(state, (2, 0)), 2)
Ejemplo n.º 8
0
    def test_suicide(self):
        string = '''
        XXX..
        XOX..
        XXX..
        .....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')

        with self.assertRaises(ValueError):
            state.check_for_suicide((1, 1))
Ejemplo n.º 9
0
    def test_random_move(self):
        string = '''
        ....
        .#X.
        ..X#
        ....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        player = CleverVirtualPlayer(state.GREY)
        state._turn = state.GREY

        move = player.try_make_move(state, 60)[1]
        self.assertTrue(move in [(2, 0), (3, 1), (2, 3), (1, 2)])
Ejemplo n.º 10
0
    def test_take_territory(self):
        string = '''
        .X...
        .X...
        X...O
        ...O.
        ....O
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        state.take_territory(GameState.BLACK)
        state.take_territory(GameState.WHITE)

        self.assertEqual(state._score[GameState.BLACK], 2)
        self.assertEqual(state._score[GameState.WHITE], 1)
Ejemplo n.º 11
0
    def test_catch_group(self):
        goban = Goban((5, 5))
        state = GameState(goban)

        state._state[(1, 1)] = GameState.BLACK
        self.assertEqual(state.catch_group((1, 1)), 1)
        self.assertEqual(state.get_state((1, 1)), GameState.FREE)

        string = '''
        XXX..
        XXX..
        .....
        .....
        .....
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        self.assertEqual(state.catch_group((1, 1)), 6)

        for x in range(0, 3):
            for y in range(0, 2):
                self.assertEqual(state.get_state((x, y)), GameState.FREE)
Ejemplo n.º 12
0
    def test_try_make_move(self):
        goban = Goban((2, 2))
        state = GameState(goban)
        player = SimpleVirtualPlayer(state.GREY)

        state._turn = state.GREY
        move = player.try_make_move(state, 60)
        self.assertEqual(move[0], True)
        self.assertTrue(move[1] in [(0, 0), (1, 0), (0, 1), (1, 1)])

        state._turn = state.GREY
        state._state[(0, 0)] = GameState.BLACK
        move = player.try_make_move(state, 60)
        self.assertEqual(move[0], True)
        self.assertTrue(move[1] in [(0, 1), (1, 0), (1, 1)])

        string = '''
        XX
        XX
        '''
        state = GameState.convert_string_to_state(string, '\n        ')
        state._turn = state.GREY

        self.assertEqual(player.try_make_move(state, 60), (False, None, 0))