Esempio n. 1
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
Esempio n. 2
0
 def test_returnBrick(self):
     col = LegoBrickCollection()
     col.initialize(10, list([LegoBrick(1, 1)]))
     b = col.getRandomBrick()
     self.assertFalse(col.returnBrick(LegoBrick(1, 1)))
     self.assertTrue(col.returnBrick(b))
     self.assertFalse(col.returnBrick(b))
Esempio n. 3
0
 def test_equals(self):
     b1 = LegoBrick(5, 5)
     b1copy = b1.copy()
     self.assertTrue(b1 == b1copy)
     b1copy.setId(b1.getId() + 10)
     self.assertFalse(b1 == b1copy)
     b2 = LegoBrick(10, 5)
     self.assertFalse(b2 == b1)
Esempio n. 4
0
 def __createBrickCollection(self, area: int) -> LegoBrickCollection:
     bricks = []
     bricks.append(LegoBrick(2, 3))
     bricks.append(LegoBrick(3, 2))
     bricks.append(LegoBrick(2, 2))
     collection = LegoBrickCollection()
     collection.initialize(area, bricks, uniform=True)
     return collection
Esempio n. 5
0
 def test_nonUniformCollection(self):
     col = LegoBrickCollection()
     col.initialize(10, list([LegoBrick(1, 1), LegoBrick(1, 2)]), False)
     amount = col.getAmountOfAvailableBricks()
     while amount != 0:
         rnd = col.getRandomBrick()
         self.assertIsNotNone(rnd)
         amount -= 1
     self.assertEqual(amount, col.getAmountOfAvailableBricks())
     rnd = col.getRandomBrick()
     self.assertIsNone(rnd)
Esempio n. 6
0
 def test_amountOfBricks(self):
     bricks = list([LegoBrick(1, 1)])
     col = LegoBrickCollection()
     col.initialize(10, bricks)
     startAmount = col.getAmountOfAvailableBricks()
     rndBrick = col.getRandomBrick()
     self.assertEqual(col.getAmountOfAvailableBricks(), startAmount - 1)
     col.returnBrick(LegoBrick(12, 12))
     self.assertEqual(col.getAmountOfAvailableBricks(), startAmount - 1)
     col.returnBrick(rndBrick)
     self.assertEqual(col.getAmountOfAvailableBricks(), startAmount)
Esempio n. 7
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)
Esempio n. 8
0
 def __createBrickLayout(self, width: int, height: int) -> LegoBrickLayout:
     bricks = []
     bricks.append(LegoBrick(1, 1))
     bricks.append(LegoBrick(1, 2))
     bricks.append(LegoBrick(2, 2))
     collection = LegoBrickCollection()
     collection.initialize(width * height, bricks, uniform=True)
     self.assertTrue(collection.isInitialized())
     layout = LegoBrickLayout()
     layout.initialize(width, height, collection)
     self.assertTrue(layout.isInitialized())
     return layout
Esempio n. 9
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)
Esempio n. 10
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
Esempio n. 11
0
 def test_numberOfBricksTypes(self):
     bricks = list([LegoBrick(1, 1)])
     col = LegoBrickCollection()
     col.initialize(10, bricks)
     self.assertEqual(col.getNumberOfBricksTypes(), len(bricks))
     col = LegoBrickCollection()
     col.initialize(10, [])
     self.assertEqual(col.getNumberOfBricksTypes(), 0)
Esempio n. 12
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())
Esempio n. 13
0
 def test_copy(self):
     col = LegoBrickCollection()
     colCopy = col.copy()
     self.assertEqual(col.isInitialized(), colCopy.isInitialized())
     col.initialize(5, list([LegoBrick(1, 1)]), False)
     colCopy = col.copy()
     self.assertEqual(col.isInitialized(), colCopy.isInitialized())
     self.assertEqual(col.getAmountOfAvailableBricks(),
                      colCopy.getAmountOfAvailableBricks())
Esempio n. 14
0
 def test_randomBrickId(self):
     col = LegoBrickCollection()
     col.initialize(5, list([LegoBrick(1, 1)]))
     colCopy = col.copy()
     self.assertNotEqual(col.getRandomBrick().getId(),
                         colCopy.getRandomBrick().getId())
     self.assertEqual(col.getRandomBrick().getId() + 1,
                      colCopy.getRandomBrick().getId())
     self.assertEqual(colCopy.getRandomBrick().getId() + 1,
                      col.getRandomBrick().getId())
Esempio n. 15
0
    def test_wrongInitialization(self):
        throws = False
        try:
            LegoBrick(10, 0)
        except ValueError as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrick constructor didn't throw ValueError on illegal parameter"
        )

        throws = False
        try:
            LegoBrick(0, 10)
        except ValueError as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrick constructor didn't throw ValueError on illegal parameter"
        )
Esempio n. 16
0
    def test_notInitializeExceptions(self):
        col = LegoBrickCollection()
        throws = False
        try:
            col.getAmountOfAvailableBricks()
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getAmountOfAvailableBricks() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.getNumberOfBricksTypes()
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getNumberOfBricksTypes() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.getRandomBrick()
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getRandomBrick() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.getBrick(1, 1)
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getBrick() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.returnBrick(LegoBrick(1, 1))
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.returnBrick() didn't throw NotInitializedException after calling method before initialization"
        )
Esempio n. 17
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
Esempio n. 18
0
 def test_addMutationToFullLayer(self):
     width, height = 5, 5
     collection = LegoBrickCollection()
     collection.initialize(width * height * 2, [LegoBrick(1, 1)],
                           uniform=True)
     self.assertTrue(collection.isInitialized())
     layout = LegoBrickLayout()
     layout.initialize(width, height, collection)
     self.assertTrue(layout.isInitialized())
     copy = layout.copy()
     sizeBeforeMutation = len(layout.getAreaBricks())
     if GaUtils.addMutation(layout):
         self.assertEqual(sizeBeforeMutation + 1,
                          len(layout.getAreaBricks()))
         self.assertFalse(copy.hasSameCoverage(layout))
     else:
         self.assertEqual(sizeBeforeMutation, len(layout.getAreaBricks()))
         self.assertTrue(copy.hasSameCoverage(layout))
Esempio n. 19
0
    def test_initialization(self):
        col = LegoBrickCollection()
        self.assertFalse(col.isInitialized())

        try:
            col.initialize(10, [])
        except Exception as e:
            self.fail(
                "LegoBrickCollection constructor raise exception on unexpected place"
            )
        self.assertTrue(col.isInitialized())

        col = LegoBrickCollection()
        self.assertFalse(col.isInitialized())
        try:
            col.initialize(50, list([LegoBrick(1, 1)]))
        except Exception as e:
            self.fail(
                "LegoBrickCollection constructor raise exception on unexpected place"
            )
        self.assertTrue(col.isInitialized())
Esempio n. 20
0
 def test_getSpecificBrick(self):
     col = LegoBrickCollection()
     col.initialize(10, list([LegoBrick(1, 1)]))
     self.assertIsNotNone(col.getBrick(1, 1))
     self.assertIsNone(col.getBrick(2, 1))
Esempio n. 21
0
def generateBricks(width: int, height: int, numberOfBricksTypes: int,
                   maxBrickRibSize: int) -> List[LegoBrick]:
    bricks = []
    if numberOfBricksTypes == -1:
        bricks.append(LegoBrick(1, 1))
        bricks.append(LegoBrick(2, 1))
        bricks.append(LegoBrick(3, 1))
        bricks.append(LegoBrick(4, 1))
        bricks.append(LegoBrick(5, 1))
        bricks.append(LegoBrick(8, 1))
        bricks.append(LegoBrick(2, 2))
        bricks.append(LegoBrick(3, 2))
        bricks.append(LegoBrick(4, 2))
        bricks.append(LegoBrick(5, 2))
        bricks.append(LegoBrick(8, 2))
    else:
        maxBrickSize = maxBrickRibSize + 1
        bricks = []
        while len(bricks) < numberOfBricksTypes:
            width = np.random.randint(1, maxBrickSize)
            height = np.random.randint(1, maxBrickSize)
            if width > height:
                temp = height
                height = width
                width = temp
            brick = LegoBrick(width, height)
            if brick not in bricks:
                bricks.append(brick)

    print("\nSelected Bricks:")
    for i in range(len(bricks)):
        print("%d - %s" % (i + 1, str(bricks[i])))
    return bricks
Esempio n. 22
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)