Ejemplo n.º 1
0
 def test_initialization(self):
     width = 5
     height = 5
     b = LegoBrick(width, height)
     self.assertEqual(b.getHeight(), height)
     self.assertEqual(b.getWidth(), width)
     self.assertEqual(b.getArea(), width * height)
     self.assertEqual(b.getId(), LegoBrick.NONE_ID)
     id = 13
     b = LegoBrick(width, height, id)
     self.assertEqual(b.getHeight(), height)
     self.assertEqual(b.getWidth(), width)
     self.assertEqual(b.getArea(), width * height)
     self.assertEqual(b.getId(), id)
Ejemplo n.º 2
0
 def test_copy(self):
     b = LegoBrick(5, 5)
     copy = b.copy()
     self.assertEqual(b.getHeight(), copy.getHeight())
     self.assertEqual(b.getWidth(), copy.getWidth())
     self.assertEqual(b.getArea(), copy.getArea())
     self.assertEqual(b.getId(), copy.getId())
Ejemplo n.º 3
0
    def returnBrick(self, brick: LegoBrick) -> bool:
        """
        Returns a brick to the collection.

        Returns
        -------
        bool
            True if the brick belong to the collection and returned, and false if doesn't.

        Raises
        ------
        NotInitializedException
            If this method called before initialize() method
        """
        if not self.__initialized:
            raise NotInitializedException(
                "The instance used before calling initialize method")

        if brick in self.__generatedBricks:
            self.__generatedBricks.remove(brick)
            for i in range(len(self.__brickTypes)):
                if self.__brickTypes[i].getWidth() == brick.getWidth(
                ) and self.__brickTypes[i].getHeight() == brick.getHeight():
                    self.__availableBricks[i] += 1
            self.__amountOfAvailableBricks += 1
            return True

        return False
Ejemplo n.º 4
0
    def __tryAddVertical(self, row: int, column: int,
                         brick: LegoBrick) -> bool:
        if column + brick.getHeight() > self.__height or row + brick.getWidth(
        ) > self.__width:
            return False

        for i in range(row, row + brick.getWidth()):
            for j in range(column, column + brick.getHeight()):
                if self.__area[i][j] != 0:
                    return False

        self.__layout.append(
            (row, column, brick, LegoBrickLayout.Orientation.VERTICAL))

        for i in range(row, row + brick.getWidth()):
            self.__area[i][column:column + brick.getHeight()] = brick.getId()

        self.__coveredArea += brick.getArea()
        return True
Ejemplo n.º 5
0
 def test_setAttributes(self):
     height = 5
     b = LegoBrick(height, height)
     width = 10
     b.setWidth(width)
     self.assertEqual(b.getHeight(), height)
     self.assertEqual(b.getWidth(), width)
     self.assertEqual(b.getArea(), width * height)
     id = 13
     b.setId(id)
     self.assertEqual(b.getId(), id)
Ejemplo n.º 6
0
    def tryAddBrick(self,
                    row: int,
                    column: int,
                    brick: LegoBrick,
                    orientation: Enum = None) -> bool:
        """
        Try to add the received brick to a specific place at the layer.

         Parameters
        ----------
        row : int
            The row index in the layer.
        column : int
            The column index in the layer.
        brick : LegoBrick
            The brick to add.
        orientation : LegoBrickLayout.Orientation
            default=None.
            If none will try to add vertically and horizontally (random order), else will try to add as required.

        Returns
        -------
        bool
            True if the brick added successfully and false if did not.

        Raises
        ------
        NotInitializedException
            If this method called before initialize() method
        """
        if not self.__initialized:
            raise NotInitializedException(
                "The instance used before calling initialize method")

        if row < 0 or column < 0 or row >= self.__width or column >= self.__height:
            return False

        if brick.getWidth() == brick.getHeight():
            # Symmetric bricks have no meaning to insert orientation
            return self.__tryAddHorizontal(row, column, brick)

        if orientation is None:
            firstVertical = bool(random.getrandbits(1))
            return self.__tryAdd(row, column, brick, firstVertical)
        elif orientation == LegoBrickLayout.Orientation.HORIZONTAL:
            return self.__tryAddHorizontal(row, column, brick)
        else:
            return self.__tryAddVertical(row, column, brick)
Ejemplo n.º 7
0
    def __tryAdd(self, row: int, column: int, brick: LegoBrick,
                 firstVertical: bool) -> bool:
        if brick.getWidth() == brick.getHeight():
            # Symmetric bricks have no meaning to insert orientation
            return self.__tryAddHorizontal(row, column, brick)

        if firstVertical:
            if not self.__tryAddVertical(row, column, brick):
                return self.__tryAddHorizontal(row, column, brick)
            else:
                return True
        else:
            if not self.__tryAddHorizontal(row, column, brick):
                return self.__tryAddVertical(row, column, brick)
            else:
                return True