Ejemplo n.º 1
0
    def __init__(self, body: list, speed: float = 14.0):
        """
        :param body: list[SnakePart], перечисленный в порядке head -> tail
        """

        self.speed = speed
        self.length_change = 0

        self.body = Queue()
        for snake_part in body[::-1]:
            self.body.enqueue(snake_part)
Ejemplo n.º 2
0
    def test_enqueue_dequeue(self):
        q = Queue()
        q.enqueue(1)
        self.assertEqual(1, q.dequeue())

        q.enqueue(1)
        q.enqueue(4)
        self.assertEqual(1, q.dequeue())
        self.assertEqual(4, q.dequeue())
Ejemplo n.º 3
0
class TestQueue(unittest.TestCase):
    def setUp(self):
        self.queue = Queue()
        for i in range(5):
            self.queue.enqueue(i)

    def test_get_head_and_tail(self):
        self.assertEqual(0, self.queue.head)
        self.assertEqual(4, self.queue.tail)

        q = Queue()
        self.assertTrue(q.head == q.tail is None)

    def test_enqueue(self):
        self.queue.enqueue(0)
        self.assertEqual(0, self.queue.tail)

    def test_dequeue(self):
        head = self.queue.head
        self.assertEqual(head, self.queue.dequeue())

        q = Queue()
        self.assertRaises(ValueError, q.dequeue)

    def test_get_len(self):
        self.assertEqual(5, len(self.queue))

        q = Queue()
        self.assertEqual(0, len(q))

    def test_iter(self):
        i = 0
        for item in self.queue:
            self.assertEqual(i, item)
            i += 1

    def test_enqueue_dequeue(self):
        q = Queue()
        q.enqueue(1)
        self.assertEqual(1, q.dequeue())

        q.enqueue(1)
        q.enqueue(4)
        self.assertEqual(1, q.dequeue())
        self.assertEqual(4, q.dequeue())
Ejemplo n.º 4
0
class Snake:
    def __init__(self, body: list, speed: float = 14.0):
        """
        :param body: list[SnakePart], перечисленный в порядке head -> tail
        """

        self.speed = speed
        self.length_change = 0

        self.body = Queue()
        for snake_part in body[::-1]:
            self.body.enqueue(snake_part)

    def _get_direction_for_step(self,
                                proposed_direction: Direction = None
                                ) -> Direction:
        if proposed_direction is None:
            return self.head.direction
        if proposed_direction == TranslateDirection.opposite_dir[
                self.head.direction]:
            return self.head.direction
        return proposed_direction

    def step(self, direction: Direction = None, size_field: tuple = None):
        direction = self._get_direction_for_step(direction)
        new_head_location = self.head.location + TranslateDirection.dir_offset[
            direction]
        if size_field is not None:
            new_head_location.x %= size_field[0]
            new_head_location.y %= size_field[1]
        self.body.enqueue(SnakePart(new_head_location, direction))
        if self.length_change > 0:
            self.length_change -= 1
            return
        if self.length_change < 0:
            self.length_change += 1
            self.body.dequeue()
        self.body.dequeue()

    def check_intersection(self, points: set = None) -> bool:
        if points is None:
            points = set()
        count = len(points)
        _points = copy(points)
        for part in self:
            count += 1
            _points.add(part.location)

        return count != len(_points)

    def eat_food(self, food: Food):
        self.speed *= food.speed_change
        self.length_change += food.length_change

    @property
    def head(self) -> SnakePart:
        return self.body.tail

    @property
    def tail(self) -> SnakePart:
        return self.body.head

    def __iter__(self):
        """Итератор для змейки пробегающий от головы до хвоста"""
        snake = []
        for part in self.body:
            snake.append(part)
        return iter(snake[::-1])

    def __len__(self):
        return len(self.body)
Ejemplo n.º 5
0
    def test_get_len(self):
        self.assertEqual(5, len(self.queue))

        q = Queue()
        self.assertEqual(0, len(q))
Ejemplo n.º 6
0
    def test_dequeue(self):
        head = self.queue.head
        self.assertEqual(head, self.queue.dequeue())

        q = Queue()
        self.assertRaises(ValueError, q.dequeue)
Ejemplo n.º 7
0
    def test_get_head_and_tail(self):
        self.assertEqual(0, self.queue.head)
        self.assertEqual(4, self.queue.tail)

        q = Queue()
        self.assertTrue(q.head == q.tail is None)
Ejemplo n.º 8
0
 def setUp(self):
     self.queue = Queue()
     for i in range(5):
         self.queue.enqueue(i)