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)
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())
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
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
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)
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)
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