def test_snake_wall(self):
        self.board = Board(width=50, height=50)
        self.board.add_object("snake", Point(x=1, y=30))

        snake = self.board.objects['snake'][0]
        _, _, done, _ = self.board.step(constants.GET_ACTION_MEANING.index("LEFT"))
        self.board.step(1)

        self.assertEqual(True, done, "Game over is not detected upon dying")
        self.assertEqual(False, snake.alive,
                         "Snake didn't die upon hitting the wall")
    def test_snake_eat_apple(self):
        self.board = Board(width=50, height=50)
        self.board.add_object("snake", Point(x=20, y=33))
        self.board.add_object("apple", Point(x=20, y=32))

        snake = self.board.objects['snake'][0]
        _, reward, _, _ = self.board.step(constants.GET_ACTION_MEANING.index("UP"))

        self.assertEqual(constants.DEFAULT_REWARD_PER_APPLE + constants.DEFAULT_REWARD_PER_STEP, reward,
                         "Snake eating apple incorrect points awarded")

        self.assertEqual(LEN_SNAKE_START+1, len(snake),
                         "Snake length is not increased after eating apple")
class TestCollision(unittest.TestCase):

    def setUp(self) -> None:
        self.objects_name = ["wall", "ground", "apple", "snake"]

        self.wall = Wall(Point(0, 0))
        self.ground = Ground(Point(10, 10))
        self.apple = Apple(Point(20, 20))
        self.snake = Snake(Point(30, 30))

        self.wall_same = Wall(Point(0, 0))
        self.ground_same = Ground(Point(0, 0))
        self.apple_same = Apple(Point(0, 0))
        self.snake_same = Snake(Point(0, 0))

        self.objects_diff = [getattr(self, name) for name in self.objects_name]
        self.objects_same = [getattr(self, name + "_same") for name in self.objects_name]

    def test_collisions(self):
        for each in self.objects_name:
            test_objects_diff = [item.clone() for item in self.objects_diff]
            test_objects_diff.pop(self.objects_name.index(each))
            result = [getattr(self, each).collide(item) for item in test_objects_diff]
            self.assertEqual([False, False, False], result, "Incorrect collision detected")

            test_objects_same = [item.clone() for item in self.objects_same]
            test_objects_same.pop(self.objects_name.index(each))
            result = [getattr(self, each + "_same").collide(item) for item in test_objects_same]
            self.assertEqual([True, True, True], result, "Incorrect collision detected")

    def test_snake_eat_apple(self):
        self.board = Board(width=50, height=50)
        self.board.add_object("snake", Point(x=20, y=33))
        self.board.add_object("apple", Point(x=20, y=32))

        snake = self.board.objects['snake'][0]
        _, reward, _, _ = self.board.step(constants.GET_ACTION_MEANING.index("UP"))

        self.assertEqual(constants.DEFAULT_REWARD_PER_APPLE + constants.DEFAULT_REWARD_PER_STEP, reward,
                         "Snake eating apple incorrect points awarded")

        self.assertEqual(LEN_SNAKE_START+1, len(snake),
                         "Snake length is not increased after eating apple")

    def test_snake_wall(self):
        self.board = Board(width=50, height=50)
        self.board.add_object("snake", Point(x=1, y=30))

        snake = self.board.objects['snake'][0]
        _, _, done, _ = self.board.step(constants.GET_ACTION_MEANING.index("LEFT"))
        self.board.step(1)

        self.assertEqual(True, done, "Game over is not detected upon dying")
        self.assertEqual(False, snake.alive,
                         "Snake didn't die upon hitting the wall")
Exemplo n.º 4
0
 def __init__(self):
     board = Board()
     render = SingleImage(board.width, board.height)
     self._image = np.zeros((board.width, board.height, 3), dtype=np.uint8)
     super().__init__(board, render)
Exemplo n.º 5
0
 def setUp(self) -> None:
     self.board = Board(width=25, height=25)
Exemplo n.º 6
0
class TestBoard(unittest.TestCase):
    def setUp(self) -> None:
        self.board = Board(width=25, height=25)

    def test_add_snake_random(self):
        self.board.add_object('snake')
        self.assertEqual(1, len(self.board.objects['snake']), "Single snake not added correctly")

    def test_add_snake_specific(self):
        self.board.snakes = []

        self.board.add_object("snake", Point(x=10, y=15))
        snake = self.board.objects['snake'].pop()

        self.assertEqual(10, snake.get_head().x, "x position not set correctly")
        self.assertEqual(15, snake.get_head().y, "y position not set correctly")
        self.assertEqual(constants.LEN_SNAKE_START, len(snake), "Snake is not start length")

    def test_add_snake_collision(self):
        self.board.snakes = []

        self.board.add_object("snake", Point(x=20, y=20))
        self.board.add_object("snake", Point(x=20, y=20))

        snake_1 = self.board.objects['snake'].pop()
        snake_2 = self.board.objects['snake'].pop()

        self.assertEqual(False, snake_1 in snake_2, "These are the same snakes")

    def test_setting_seed(self):
        """ Test if the same board is produced after setting a seed.  """
        seed = self.board.seed()

        for _ in range(10):
            self.board.reset()
        state = self.board.board

        self.board.seed(seed)
        for _ in range(10):
            self.board.reset()

        self.assertEqual([o.__class__.__name__ for x in state for o in x],
                         [o.__class__.__name__ for x in self.board.board for o in x], "Unable to reproduce same board")
Exemplo n.º 7
0
 def __init__(self):
     board = Board()
     render = Console()
     super().__init__(board, render)
Exemplo n.º 8
0
 def __init__(self, width=None, height=None):
     self.board = Board(width, height)
     self.image = np.zeros((self.board.width, self.board.height, 3),
                           dtype=np.uint8)
Exemplo n.º 9
0
class SnakeGame:
    """ Remapping of all board functions to the public available environment functions.  """
    def __init__(self, width=None, height=None):
        self.board = Board(width, height)
        self.image = np.zeros((self.board.width, self.board.height, 3),
                              dtype=np.uint8)

    def obs(self, attribute="rgb"):
        return self.board.obs(attribute)

    def reward(self):
        return self.board.reward()

    def done(self):
        return self.board.done()

    def info(self):
        return self.board.info()

    def step(self, action: int):
        self.board.step(action, values=False)
        return self.obs(), self.reward(), self.done(), self.info()

    def reset(self):
        """ Reset the game, and should return the start observation of the game. """
        self.board.reset()
        return self.obs()

    def render(self):
        return self.board.obs(attribute='rgb')

    def close(self):
        self.board.close()

    def seed(self, seed=None):
        return self.board.seed(seed)