Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/CogitoNTNU/TetrisAI into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
henrinha committed Apr 9, 2024
2 parents 72d34ed + a6f674b commit 3abdfc4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
19 changes: 14 additions & 5 deletions src/game/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []
Expand Down
68 changes: 40 additions & 28 deletions test/game/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from src.game.board import Action, Board
from src.game.block import Block


def test_move_down():
Expand Down Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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)
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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)
Expand Down

0 comments on commit 3abdfc4

Please sign in to comment.