Skip to content

Commit

Permalink
refactor: make scripts using Board.ROWS and Board.COLUMNS use correct…
Browse files Browse the repository at this point in the history
… cas
  • Loading branch information
SverreNystad committed Apr 9, 2024
1 parent c12510d commit fb4574d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
70 changes: 38 additions & 32 deletions src/agents/heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,88 @@

from src.game.board import Board


def utility(gameState: Board) -> int:
""" Returns the utility of the given game state. """
"""Returns the utility of the given game state."""
pass


def aggregate_heights(gameState: Board) -> int:
""" Returns the sum of the heights of the columns in the game state. """
checkedList = [0 for i in range(gameState.columns)]
for i in range(gameState.rows):
for j in range(gameState.columns):
"""Returns the sum of the heights of the columns in the game state."""
checkedList = [0 for i in range(gameState.COLUMNS)]
for i in range(gameState.ROWS):
for j in range(gameState.COLUMNS):
if gameState.board[i][j] > 0:
if checkedList[j] == 0:
checkedList[j] = gameState.rows - i
checkedList[j] = gameState.ROWS - i
return sum(checkedList)


def max_height(gameState: Board) -> int:
""" Returns the maximum height of the columns in the game state. """
checkedList = [0 for i in range(gameState.columns)]
for i in range(gameState.rows):
for j in range(gameState.columns):
"""Returns the maximum height of the columns in the game state."""
checkedList = [0 for i in range(gameState.COLUMNS)]
for i in range(gameState.ROWS):
for j in range(gameState.COLUMNS):
if gameState.board[i][j] > 0:
if checkedList[j] == 0:
checkedList[j] = gameState.rows - i
checkedList[j] = gameState.ROWS - i
return max(checkedList)


def lines_cleaned(gameState: Board) -> int:
""" Retrurns the number of lines cleared. """
"""Retrurns the number of lines cleared."""
sum = 0
for row in gameState.board:
if all(cell == 1 for cell in row):
sum += 1
return sum


def bumpiness(gameState: Board) -> int:
""" Returns the sum of the absolute height between all the columns"""
"""Returns the sum of the absolute height between all the columns"""
total_bumpiness = 0
max_height = 20
columnHeightMap = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}
for column in range(gameState.columns):
for row in range (gameState.rows):
columnHeightMap = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
for column in range(gameState.COLUMNS):
for row in range(gameState.ROWS):
if gameState.board[row][column] > 0:
if columnHeightMap[column] == 0:
columnHeightMap[column] = max_height - row
for key in range(gameState.columns-1):
total_bumpiness += abs(columnHeightMap[key] - columnHeightMap[key+1])
columnHeightMap[column] = max_height - row

for key in range(gameState.COLUMNS - 1):
total_bumpiness += abs(columnHeightMap[key] - columnHeightMap[key + 1])
return total_bumpiness


def aggregate_height(gameState: Board) -> int:
" Returns the sum of all column-heights "
"Returns the sum of all column-heights"
max_height = 20
total_height = 0
columnHeightMap = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}
for column in range(gameState.columns):
for row in range (gameState.rows):
columnHeightMap = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
for column in range(gameState.COLUMNS):
for row in range(gameState.ROWS):
if gameState.board[row][column] > 0:
if columnHeightMap[column] == 0:
columnHeightMap[column] = max_height - row
for key in range(gameState.columns):

for key in range(gameState.COLUMNS):
total_height += columnHeightMap[key]
return total_height



def find_holes(gameState: Board) -> int:
""" Returns number of empty cells on the board.
"""Returns number of empty cells on the board.
Args:
gameState (Board): the state to check
Returns:
Returns:
The heuristic value
"""
holes = 0
for i in range(gameState.columns):
for i in range(gameState.COLUMNS):
top_block = 20
for j in range(gameState.rows):
for j in range(gameState.ROWS):
if (gameState.board[j][i] == 1) and (j < top_block):
top_block = j
if (gameState.board[j][i] == 0) and (j > top_block):
Expand Down
23 changes: 10 additions & 13 deletions test/game/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_try_to_move_block_out_of_bound_left():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]

for _ in range(board.columns + 1):
for _ in range(board.COLUMNS + 1):
board.doAction(Action.MOVE_LEFT)

for board_row, expected_row in zip(board.board, expected_board):
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_try_to_move_block_out_of_bound_right():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]

for _ in range(board.columns + 1):
for _ in range(board.COLUMNS + 1):
board.doAction(Action.MOVE_RIGHT)

for board_row, expected_row in zip(board.board, expected_board):
Expand Down Expand Up @@ -172,12 +172,12 @@ def test_drop_block_on_top_of_another_block():
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
]

innitBlock = Block(3,0,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 @@ -202,15 +202,15 @@ def test_drop_block_on_top_of_another_block():
]
board.doAction(Action.HARD_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():
innitBlock = Block(3,0,0)
innitBoard= [

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],
Expand Down Expand Up @@ -265,10 +265,7 @@ def test_slide_left_block_on_top_of_another_block():


def test_slide_right_block_on_top_of_another_block():

initBlock = Block(3,0,5)


initBlock = Block(3, 0, 5)

innitBoard = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Expand Down

0 comments on commit fb4574d

Please sign in to comment.