From 0ebea765e2dffef675063dbf31b5e2ced8b2007c Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 1 Jan 2024 20:02:23 +0200 Subject: [PATCH] feat(game): add `is_full` method --- src/py2048/board.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/py2048/board.py b/src/py2048/board.py index b0b4b91..3af901f 100644 --- a/src/py2048/board.py +++ b/src/py2048/board.py @@ -32,7 +32,8 @@ class Board(pygame.sprite.Group): for block in blocks: block.move(direction) - self.generate_random_block() + if not self._is_full(): + self.generate_random_block() def generate_initial_blocks(self) -> None: """Generate the initial blocks.""" @@ -57,9 +58,16 @@ class Board(pygame.sprite.Group): y = random.randint(0, 3) * Config.BLOCK_SIZE block = Block(x, y, self) - colliding_blocks = pygame.sprite.spritecollide(block, self, False) # check for collisions + colliding_blocks = pygame.sprite.spritecollide( + block, self, False + ) # check for collisions if not colliding_blocks: self.add(block) logger.debug(f"Created block at {block.pos}") break + + def _is_full(self) -> bool: + """Check if the board is full.""" + return len(self.sprites()) == Config.GRID_SIZE**2 +