Skip to content

Commit

Permalink
feat: add intersection logic and test
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad committed Apr 9, 2024
1 parent 72c9cb2 commit 88b7ae3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
30 changes: 15 additions & 15 deletions src/game/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def doAction(self, action: Action) -> None:
case Action.ROTATE_COUNTERCLOCKWISE:
new_block.rotateLeft()
case Action.DROP:
# TODO: Correct THE HARD DROP
while True:
new_block.moveDown()
if not self.isValidBlockPosition(new_block):
Expand Down Expand Up @@ -105,19 +104,21 @@ def _outOfBounds(self, block: Block) -> bool:
return False

def _intersects(self, block: Block) -> bool:
"""Checks if the block intersects with the board"""
"""Checks if the block intersects with another block on the board"""
## TODO: Fix this
# for row in range(4):
# for column in range(4):
# if row * 4 + column in block.image():
# if (
# row + block.y > self.columns - 1
# or row + block.y < 0
# or column + block.x > self.rows - 1
# or column + block.x < 0
# or self.prevBoard[row + block.y][column + block.x] > 0
# ):
# return True
for row in range(4):
for column in range(4):
if row * 4 + column in block.image():
# Check if the block intersects with the board
# That is, if the block is on top of another block that is not itself
blockOverlaps = self.prevBoard[row + block.y][column + block.x] > 0
isItSelf = (
block.x + column != self.block.x
or block.y + row != self.block.y
)

if blockOverlaps and not isItSelf:
return True
return False

def isGameOver(self):
Expand All @@ -134,7 +135,7 @@ def clearRow(self, rownumber: int):
self.board = newMatrix # Oppdaterer matrisen med den nye matrisen
self.rowsRemoved += 1 # Oppdaterer antall fjernede rader

def checkGameState(self):
def checkGameState(self) -> int:
amount = 0
fullRows = []

Expand All @@ -145,7 +146,6 @@ def checkGameState(self):
fullRows.append(
rowIndex
) # Legger til indeksen til listen over fulle rader

for rowIndex in reversed(
fullRows
): # Går gjennom listen over fulle rader i reversert rekkefølge for å fjerne dem
Expand Down
16 changes: 8 additions & 8 deletions test/game/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ def test_drop_block_on_top_of_another_block():
[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],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 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],
Expand All @@ -171,14 +170,11 @@ def test_drop_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],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
]
board.printBoard()
expected_board = [
[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, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 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],
Expand All @@ -193,10 +189,14 @@ def test_drop_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, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
]
for _ in range(board.rows - 1):
board.doAction(Action.SOFT_DROP)

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"

0 comments on commit 88b7ae3

Please sign in to comment.