示例#1
0
    def test_high_oob(self):
        snake = core.Snake(np.array([5, 5]), 3, core.RIGHT)
        state = core.GameState(snake, 6, 6, 0)

        self.assertTrue(state.is_playable())
        state.update(core.RIGHT)
        self.assertFalse(state.is_playable())

        snake = core.Snake(np.array([5, 5]), 3, core.RIGHT)
        state = core.GameState(snake, 6, 6, 0)
        state.update(core.DOWN)
        self.assertFalse(state.is_playable())
示例#2
0
    def test_state_matrix_correct(self):
        snake = core.Snake(np.array([5, 5]), 3, core.RIGHT)
        state = core.GameState(snake, 10, 10)

        matrix = state.to_matrix()
        food = state.food()
        self._check_matrix(snake, matrix, food)
示例#3
0
    def test_multiple_food_items(self):
        snake = core.Snake(np.array([5, 5]), 1, core.RIGHT)
        state = core.GameState(snake, 10, 10, food_max=5)

        self.assertEqual(5, len(state.food()))

        state.update(core.RIGHT)
        self.assertEqual(5, len(state.food()))
示例#4
0
    def test_food_regenerated_upon_eating(self):
        snake = core.Snake(np.array([5, 5]), 1, core.RIGHT)
        state = core.GameState(snake, 10, 10, seed=10, food_max=5)

        self.assertEqual(5, len(state.food()))

        for _ in range(4):
            state.update(core.UP)

        self.assertEqual(5, len(state.food()))
示例#5
0
    def test_state_becomes_unplayable_on_update(self):
        snake = core.Snake(np.array([2, 1]), 3, core.LEFT)
        state = core.GameState(snake, 5, 5)
        self.assertTrue(state.is_playable())
        state.update(core.LEFT)
        self.assertTrue(state.is_playable())
        state.update(core.LEFT)
        self.assertFalse(state.is_playable())

        assert_snake_has_position(snake, [[0, 1], [1, 1], [2, 1]])
示例#6
0
    def test_punishes_game_over(self):
        snake = core.Snake(np.array([5, 5]), 2, core.LEFT)
        state = core.GameState(snake, 10, 10)

        for _ in range(4):
            state.update(core.UP)

        state.update(core.UP)

        self.assertFalse(state.is_playable())
        self.assertGreaterEqual(
            -1, trainer.determine_reward(False, state.is_playable(), False))
示例#7
0
    def test_game_state_becomes_invalid_after_drought(self):
        snake = core.Snake(np.array([5, 5]), 1, core.LEFT)
        state = core.GameState(snake, 10, 10, food_max=0, max_drought=10)

        moves = cycle([core.DOWN, core.RIGHT, core.UP, core.LEFT])
        for _ in range(10):
            move = next(moves)
            state.update(move)
            self.assertTrue(state.is_playable())

        state.update(core.UP)
        self.assertFalse(state.is_playable())
示例#8
0
    def test_game_state_does_not_degrade_normally(self):
        snake = core.Snake(np.array([5, 5]), 1, core.LEFT)
        state = core.GameState(snake, 10, 10, food_max=0)

        # Execute 1,000 moves to check against a drought.
        for i in range(250):
            state.update(core.DOWN)
            state.update(core.RIGHT)
            state.update(core.UP)
            state.update(core.LEFT)

        self.assertTrue(state.is_playable())
示例#9
0
    def test_snake_grows_from_state(self):
        # TODO(matthew-c21): Update this to match any changes to the seeding in GameState.
        snake = core.Snake(np.array([5, 5]), 2, core.LEFT)
        state = core.GameState(snake, 10, 10, seed=10)

        for _ in range(4):
            state.update(core.UP)

        for _ in range(2):
            state.update(core.LEFT)

        self.assertEqual(3, len(snake))
示例#10
0
    def test_rewards_eating(self):
        # Recall the 10 seed from test_core.
        snake = core.Snake(np.array([5, 5]), 2, core.LEFT)
        state = core.GameState(snake, 10, 10, seed=10)

        has_eaten = False

        for _ in range(4):
            has_eaten = state.update(core.UP)

        actual_reward = trainer.determine_reward(state.is_playable(), 0,
                                                 has_eaten)
        self.assertLess(0, actual_reward)
示例#11
0
    def test_punish_self_intersection(self):
        snake = core.Snake(np.array([5, 5]), 5, core.LEFT)
        state = core.GameState(snake, 10, 10)

        state.update(core.DOWN)
        state.update(core.RIGHT)

        old_state = state.to_matrix()
        state.update(core.UP)
        new_state = state.to_matrix()

        self.assertGreaterEqual(
            -1, trainer.determine_reward(state.is_playable(), 0, False))
示例#12
0
    def test_not_eating_gives_negative_distance(self):
        snake = core.Snake(np.array([5, 5]), 2, core.LEFT)
        state = core.GameState(snake, 10, 10, seed=10)

        old_state = state.to_matrix()
        state.update(core.UP)
        new_state = state.to_matrix()

        # snake at (5, 4). Food at (5, 1).
        self.assertEqual(
            -3.0,
            trainer.determine_reward(True, state.min_distance_to_food(),
                                     False))
示例#13
0
    def test_board_does_not_make_changes_after_game_over(self):
        snake = core.Snake(np.array([1, 1]), 1, core.LEFT)
        state = core.GameState(snake, 5, 5)

        self.assertTrue(state.is_playable())
        state.update(core.LEFT)
        self.assertFalse(state.is_playable())
        assert_snake_has_position(snake, [[0, 1]])

        for i in range(10):
            state.update(core.LEFT)

        assert_snake_has_position(snake, [[0, 1]])
示例#14
0
    def test_setting_food_items_persistent(self):
        snake = core.Snake(np.array([5, 5]), 1, core.RIGHT)
        state = core.GameState(snake, 10, 10, food_max=5)
        food = core._food_item(np.array([6, 5]), 100)

        state.set_food([food])

        self.assertEqual(5, len(state.food()))
        self.assertTrue(any(x is food for x in state.food()))

        state.update(core.RIGHT)

        self.assertTrue(all(x is not food for x in state.food()))
        self.assertEqual(5, len(state.food()))
示例#15
0
    def test_multiple_legal_moves(self):
        snake = core.Snake(np.array([5, 5]), 3, core.LEFT)
        state = core.GameState(snake, 10, 10, 0)

        self.assertTrue(state.is_playable())
        snake.move(core.LEFT)
        self.assertTrue(state.is_playable())
        snake.move(core.UP)
        self.assertTrue(state.is_playable())
        snake.move(core.RIGHT)
        self.assertTrue(state.is_playable())
        snake.move(core.UP)
        self.assertTrue(state.is_playable())

        assert_snake_has_position(snake, [[5, 3], [5, 4], [4, 4]])

        matrix = state.to_matrix()
        self._check_matrix(snake, matrix)