diff --git a/src/game/board.py b/src/game/board.py index 82a1bb3..ce79c92 100644 --- a/src/game/board.py +++ b/src/game/board.py @@ -34,17 +34,24 @@ class Board: rows = 20 columns = 10 - def __init__(self): + def __init__(self, board=None, block=None): """ Initializes a new game board instance, setting up an empty board, placing the first block, and selecting the next block. """ self.gameOver = False self.rowsRemoved = 0 - self.board = self._initBoard() + if board == None: + self.board = self._initBoard() + else: + self.board = board + if block == None: + self.block = Block(3, 0, 0) + else: + self.block = block self.prevBoard = copy.deepcopy(self.board) - self.block = Block(3, 0, 0) + self._placeBlock() self.nextBlock = Block(0, 5, random.randint(0, 6)) @@ -84,6 +91,7 @@ def doAction(self, action: Action) -> None: case Action.DROP: while True: new_block.moveDown() + self.printBoard() if not self.isValidBlockPosition(new_block): new_block.moveUp() break @@ -146,7 +154,7 @@ def _intersects(self, block: Block) -> bool: blockOverlaps = self.prevBoard[row + block.y][column + block.x] > 0 isItSelf = ( block.x + column == self.block.x - or block.y + row == self.block.y + and block.y + row == self.block.y ) if blockOverlaps and not isItSelf: @@ -196,12 +204,13 @@ def _checkGameState(self) -> int: return amount # Returnerer totalt antall fjernede rader def _clearRow(self, rownumber: int): + """Clears the specified row and moves all rows above down one step""" - # Fjerner den angitte raden og legger til en ny tom rad ved bunnen av matrisen newMatrix = self.board[:rownumber] + self.board[rownumber + 1 :] newMatrix.append([0 for _ in range(self.columns)]) self.board = newMatrix # Oppdaterer matrisen med den nye matrisen self.rowsRemoved += 1 # Oppdaterer antall fjernede rader + def getPossibleMoves(self) -> list["Board"]: possibleMoves = [] diff --git a/test/game/test_actions.py b/test/game/test_actions.py index a7909e8..350b476 100644 --- a/test/game/test_actions.py +++ b/test/game/test_actions.py @@ -1,4 +1,5 @@ from src.game.board import Action, Board +from src.game.block import Block def test_move_down(): @@ -148,13 +149,11 @@ def test_try_to_rotate_block_out_of_bound(): def test_drop_block_on_top_of_another_block(): - board: Board = Board() - - board.board = [ - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + innitBoard = [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -172,7 +171,13 @@ def test_drop_block_on_top_of_another_block(): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], ] + + innitBlock = Block(3,0,0) + + board: Board = Board(board=innitBoard, block=innitBlock) + board.printBoard() + expected_board = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -195,21 +200,20 @@ def test_drop_block_on_top_of_another_block(): [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], ] - + board.doAction(Action.DROP) + board.printBoard() + for board_row, expected_row in zip(board.board, expected_board): assert board_row == expected_row, "Board did not match expected board" def test_slide_left_block_on_top_of_another_block(): - board: Board = Board() - - board.board = [ - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + + innitBlock = Block(3,0,0) + innitBoard= [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -226,7 +230,11 @@ def test_slide_left_block_on_top_of_another_block(): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] + board: Board = Board(board=innitBoard, block=innitBlock) expected_board = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -244,10 +252,10 @@ def test_slide_left_block_on_top_of_another_block(): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], ] board.doAction(Action.DROP) @@ -259,13 +267,12 @@ def test_slide_left_block_on_top_of_another_block(): def test_slide_right_block_on_top_of_another_block(): - board: Board = Board() + + initBlock = Block(3,0,5) + + - board.board = [ - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + innitBoard = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -281,8 +288,13 @@ def test_slide_right_block_on_top_of_another_block(): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] + board: Board = Board(board=innitBoard, block=initBlock) expected_board = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], @@ -299,11 +311,11 @@ def test_slide_right_block_on_top_of_another_block(): [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] board.printBoard() board.doAction(Action.DROP)