def test_remove_front():
    list = DoubleLinkedList()
    list.addFront('Last').addFront('First')
    assert list.removeFront() == "First"
    assert list.front() == "Last"
    assert list.removeFront() == "Last"
    assert list.isEmpty()
def test_remove_front():
    list = DoubleLinkedList()
    list.addFront(1).addFront(2)
    assert list.removeFront() == 2
    assert list.front() == 1
    assert list.removeFront() == 1
    assert list.isEmpty()
    list.removeFront()
    assert list.isEmpty()
def test_copy():
    list = DoubleLinkedList()
    list.addFront(1).addFront(2).addFront(3)
    copy = list.copy()
    assert not copy == list
    assert list.front() == 3
    assert copy.front() == 3
    assert copy.removeBack() == 1
    assert copy.back() == 2
    assert list.back() == 1
def test_add_element_to_front():
    list = DoubleLinkedList()
    list.addFront(3)
    assert not list.isEmpty()
    list.addFront(2).addFront(1)
    assert list.front() == 1
def test_multiple_elements():
    list = DoubleLinkedList()
    list.addFront(1).addBack(2)
    assert list.front() == 1
    assert list.back() == 2
def test_get_back_node():
    list = DoubleLinkedList()
    assert list.back() is None
    list.addFront(1)
    assert list.back() == 1
Пример #7
0
class Snake:
    def __init__(self, x=0, y=0):
        self.__snake = DoubleLinkedList()
        self.__direction = Direction.RIGHT
        self.__length = 0
        self.move(x, y)
        self.move()

    def move(self, x=None, y=None):
        if x == None and y == None:
            (x, y) = self.__snake.front()

        if self.__direction == Direction.LEFT:
            self.__snake.addFront((x - 1, y))
        elif self.__direction == Direction.DOWN:
            self.__snake.addFront((x, y + 1))
        elif self.__direction == Direction.UP:
            self.__snake.addFront((x, y - 1))
        elif self.__direction == Direction.RIGHT:
            self.__snake.addFront((x + 1, y))

    def removeBack(self):
        self.__snake.removeBack()

    def get(self):
        return self.__snake

    def changeDirection(self, direction):
        if direction == Direction.RIGHT and self.__direction == Direction.LEFT:
            return
        elif direction == Direction.UP and self.__direction == Direction.DOWN:
            return
        elif direction == Direction.DOWN and self.__direction == Direction.UP:
            return
        elif direction == Direction.LEFT and self.__direction == Direction.RIGHT:
            return
        else:
            self.__direction = direction

    def getDirection(self):
        return self.__direction

    def collision(self):
        _Node = self.__snake.getFrontNode()
        coordinates = []
        while _Node.next() is not None:
            coordinates.append(_Node.get())
            _Node = _Node.next()
        _Node = self.__snake.getFrontNode()
        while _Node.next() is not None:
            if coordinates.count(_Node.get()) > 1:
                return True
            _Node = _Node.next()
        return False

    def collisionWalls(self):
        (x, y) = self.__snake.front()
        if (x <= 0 and self.__direction == Direction.LEFT):
            return True
        elif (x >= Draw.WIDTH / Draw.PIXEL_SIZE
              and self.__direction == Direction.RIGHT):
            return True
        elif (y <= 0 and self.__direction == Direction.UP):
            return True
        elif (y >= Draw.HEIGHT / Draw.PIXEL_SIZE
              and self.__direction == Direction.DOWN):
            return True
        else:
            return False
Пример #8
0
class Snake:
    def __init__(self, x: int, y: int, startLength: int):
        self.__snake = DoubleLinkedList()
        pixel = _Pixel(x, y, 's')
        for i in range(startLength):
            self.__snake.addBack(pixel)
            pixel = pixel.right()
        self.__direction = _Direction(random.randint(0, 2))
        self.__grow = False

    def direction(self):
        return self.__direction

    def head(self):
        return self.__snake.front()

    def draw(self, canvas):
        snake = self.__snake.copy()
        pixel = snake.removeFront()
        while (not pixel is None):
            pixel.draw(canvas)
            pixel = snake.removeFront()

    def grow(self):
        self.__grow = True

    def update(self):
        if (self.direction() == _Direction.LEFT):
            self.__snake.addFront(self.__snake.front().left())
        elif (self.direction() == _Direction.RIGHT):
            self.__snake.addFront(self.__snake.front().right())
        elif (self.direction() == _Direction.UP):
            self.__snake.addFront(self.__snake.front().up())
        elif (self.direction() == _Direction.DOWN):
            self.__snake.addFront(self.__snake.front().down())
        if (not self.__grow):
            self.__snake.removeBack()
        self.__grow = False

    def cut_itself(self):
        copy = self.__snake.copy()
        front = copy.removeFront()
        body = copy.removeFront()
        while (body is not None):
            if (front.x() == body.x() and front.y() == body.y()):
                return True
            body = copy.removeFront()
        return False

    def try_set_direction_left(self):
        return self.__try_set_direction(_Direction.LEFT)

    def try_set_direction_right(self):
        return self.__try_set_direction(_Direction.RIGHT)

    def try_set_direction_up(self):
        return self.__try_set_direction(_Direction.UP)

    def try_set_direction_down(self):
        return self.__try_set_direction(_Direction.DOWN)

    def __try_set_direction(self, newDirection):
        if (_check_direction(self.direction(),
                             newDirection) == _Direction_check_result.OK):
            self.__direction = newDirection
            return True
        return False

    def has_ball_collision(self, ball):
        snake = self.__snake.copy()
        snake_element = snake.removeFront()
        while (snake_element is not None):
            if (ball.x() == snake_element.x()
                    and ball.y() == snake_element.y()):
                return True
            snake_element = snake.removeFront()
        return False
def test_add_element_to_front():
    list = DoubleLinkedList()
    list.addFront('First')
    assert not list.isEmpty()
def test_get_back_node():
    list = DoubleLinkedList()
    list.addFront("Second")
    assert list.back() == "Second"
    list.addFront("First")
    assert list.back() == "Second"
def test_get_front_node():
    list = DoubleLinkedList()
    assert list.front() == None
    list.addFront("First")
    assert list.front() == "First"
def test_multiple_items_added_to_front():
    list = DoubleLinkedList()
    list.addFront('Last').addFront('First')
    assert list.front() == 'First'
    assert list.back() == 'Last'
def test_add_element_to_back():
    list = DoubleLinkedList()
    list.addFront('First')
    list.addBack('Second')
    assert list.front() == 'First'