From 6704d2db24b88f22388a2f912fcda988afaabf37 Mon Sep 17 00:00:00 2001 From: Eduard Prokhorikhin Date: Tue, 23 Apr 2024 11:31:02 +0200 Subject: [PATCH] feat: added backbone for multiple corols --- src/agents/heuristic.py | 7 ++++--- src/game/TetrisGameManager.py | 11 ++++++----- src/game/tetris.py | 6 +++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/agents/heuristic.py b/src/agents/heuristic.py index 13bd8e9..0870416 100644 --- a/src/agents/heuristic.py +++ b/src/agents/heuristic.py @@ -46,11 +46,12 @@ def max_height(gameState: Tetris) -> int: return max(checkedList) +# Does this work? row cleared in get_possible_boards?? def lines_cleaned(gameState: Tetris) -> int: """Retrurns the number of lines cleared.""" sum = 0 for row in gameState.board: - if all(cell == 1 for cell in row): + if all(cell >= 1 for cell in row): sum += 1 return sum @@ -70,7 +71,7 @@ def bumpiness(gameState: Tetris) -> int: total_bumpiness += abs(columnHeightMap[key] - columnHeightMap[key + 1]) return total_bumpiness - +# Henrik exluse ??? def aggregate_height(gameState: Tetris) -> int: "Returns the sum of all column-heights" max_height = gameState.ROWS @@ -100,7 +101,7 @@ def find_holes(gameState: Tetris) -> int: for column in range(gameState.COLUMNS): top_block = gameState.ROWS for row in range(gameState.ROWS): - if (gameState.prevBoard[row][column] == 1) and (row < top_block): + if (gameState.prevBoard[row][column] >= 1) and (row < top_block): top_block = row if (gameState.prevBoard[row][column] == 0) and (row > top_block): holes += 1 diff --git a/src/game/TetrisGameManager.py b/src/game/TetrisGameManager.py index 2c81b91..a7b6a96 100644 --- a/src/game/TetrisGameManager.py +++ b/src/game/TetrisGameManager.py @@ -16,6 +16,7 @@ SCREEN_WIDTH = WIDTH * BLOCK_SIZE SCREEN_HEIGHT = (HEIGHT - START_HEIGHT) * BLOCK_SIZE +# Colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) BLUE = (0, 0, 255) @@ -47,7 +48,7 @@ def startGame(self): clock = pygame.time.Clock() while not self.board.gameOver: - self.draw_board(self.board.board) + self.draw_board(self.board) self.inputHandling() if self.board.blockHasLanded: self.board.updateBoard() # Update the board after a block has landed and spawn a new block @@ -81,13 +82,13 @@ def checkTimer(self): self.currentTime = newTime self.movePiece(Action.SOFT_DROP) - def draw_board(self, board): + def draw_board(self, gameState: Tetris): self.screen.fill(BLACK) - temp = deepcopy(board) - temp = temp[START_HEIGHT:] + temp = deepcopy(gameState) + temp_board = temp.board[START_HEIGHT:] for y in range(HEIGHT-START_HEIGHT): for x in range(WIDTH): - if temp[y][x] == 1: + if temp_board[y][x] == 1: pygame.draw.rect(self.screen, BLUE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(self.screen, WHITE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1) diff --git a/src/game/tetris.py b/src/game/tetris.py index 44c0961..83efd88 100644 --- a/src/game/tetris.py +++ b/src/game/tetris.py @@ -202,7 +202,7 @@ def _placeBlock(self): if i * 4 + j in self.block.image(): self.board[i + self.block.y][ j + self.block.x - ] = 1 # self.block.color + ] = self.block.type + 1 # implicit color 1 to 7 def _shiftToNewBlock(self): @@ -215,7 +215,7 @@ def _shiftToNewBlock(self): if i * 4 + j in self.block.image(): self.board[i + self.block.y][ j + self.block.x - ] = 1 # self.block.color + ] = self.block.type + 1 # implicit color 1 to 7 def _checkGameOver(self): """Checks if the game is over""" @@ -304,7 +304,7 @@ def printBoard(self): print("‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾") def _checkCharacter(self, character) -> str: - if character == 1: + if character >= 1: return "■" else: return "▧"