Skip to content

Commit

Permalink
refactor: combine aggregate height, max_height and bumpiness
Browse files Browse the repository at this point in the history
  • Loading branch information
JonBergland committed Apr 24, 2024
1 parent ee028a2 commit 6eaeca7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
36 changes: 31 additions & 5 deletions src/agents/heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,42 @@ def utility(gameState: Tetris, aggregate_heights_weight: float, max_height_weigh
lines_cleared_weight: float, bumpiness_weight: float, holes_weight: float) -> int:
"""Returns the utility of the given game state."""
sum = 0
aggregate, max_height, bumpiness = calculate_heights(gameState)

sum += aggregate_heights_weight * aggregate_heights(gameState)
sum += max_height_weight * max_height(gameState)
sum += aggregate_heights_weight * aggregate
sum += max_height_weight * max_height
sum += lines_cleared_weight * lines_cleaned(gameState)
sum += bumpiness_weight * bumpiness(gameState)
sum += bumpiness_weight * bumpiness
sum += holes_weight * find_holes(gameState)

return sum

def calculate_heights(gameState: Tetris) -> tuple[int, int, int]:
"""Calculates the sum and maximum height of the columns in the game state."""
#sum_heights = 0
max_height = 0
checked_list = [0] * gameState.COLUMNS


total_bumpiness = 0
columnHeightMap = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}



for row in range(gameState.ROWS - 1, -1, -1):
for column in range(gameState.COLUMNS):
if gameState.prevBoard[row][column] != 0:
height = gameState.ROWS - row
checked_list[column] = height
max_height = max(max_height, height)
columnHeightMap[column] = gameState.ROWS - row


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


return sum(checked_list), max_height , total_bumpiness

def aggregate_heights(gameState: Tetris) -> int:
"""Returns the sum of the heights of the columns in the game state."""
Expand Down Expand Up @@ -51,13 +78,12 @@ def lines_cleaned(gameState: Tetris) -> int:
def bumpiness(gameState: Tetris) -> int:
"""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):
if gameState.prevBoard[row][column] > 0:
if columnHeightMap[column] == 0:
columnHeightMap[column] = max_height - row
columnHeightMap[column] = gameState.ROWS - row

for key in range(gameState.COLUMNS - 1):
total_bumpiness += abs(columnHeightMap[key] - columnHeightMap[key + 1])
Expand Down
27 changes: 14 additions & 13 deletions test/agents/test_heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ def test_heuristic_height_aggregate_empty_board():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
board = Tetris(initBoard)
assert aggregate_heights(board) == 0
assert calculate_heights(board)[0] == 0, "Expected aggregate height of 0 for an empty board"


def test_heuristic_aggregate_with_equal_heights():


initBoard = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Expand All @@ -57,7 +56,7 @@ def test_heuristic_aggregate_with_equal_heights():
]
board = Tetris(initBoard)
expected = 3 * 9
assert aggregate_heights(board) == expected
assert calculate_heights(board)[0] == expected


def test_heuristic_high_line_heights():
Expand Down Expand Up @@ -86,7 +85,7 @@ def test_heuristic_high_line_heights():
]
board = Tetris(initBoard)
expected = 3 * 9
assert aggregate_heights(board) == expected
assert calculate_heights(board)[0] == expected


def test_heuristic_different_heights():
Expand Down Expand Up @@ -115,7 +114,7 @@ def test_heuristic_different_heights():
]
board = Tetris(initBoard)
expected = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
assert aggregate_heights(board) == expected
assert calculate_heights(board)[0] == expected


def test_max_height_empty_board():
Expand All @@ -142,7 +141,7 @@ def test_max_height_empty_board():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
board = Tetris(initBoard)
assert max_height(board) == 0, "Expected max height of 0 for an empty board"
assert calculate_heights(board)[1] == 0, "Expected max height of 0 for an empty board"


def test_max_height_equal_heights():
Expand Down Expand Up @@ -170,7 +169,7 @@ def test_max_height_equal_heights():
]
board = Tetris(initBoard)
assert (
max_height(board) == 20
calculate_heights(board)[1] == 20
), "Expected max height of 20 for a board with equal heights"


Expand Down Expand Up @@ -199,7 +198,7 @@ def test_max_height_takes_highest():
]
board = Tetris(initBoard)
assert (
max_height(board) == 20
calculate_heights(board)[1] == 20
), "Expected max height of 20 for a single column with height 20"


Expand Down Expand Up @@ -335,7 +334,9 @@ def test_bumpiness_empty():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
assert bumpiness(board) == 0

assert calculate_heights(board)[2] == 0
#assert bumpiness(board) == 0


def test_bumpiness_five():
Expand Down Expand Up @@ -363,7 +364,7 @@ def test_bumpiness_five():
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
board = Tetris(initBoard)
assert bumpiness(board) == 2
assert calculate_heights(board)[2] == 2


def test_bumpiness_nine():
Expand Down Expand Up @@ -391,7 +392,7 @@ def test_bumpiness_nine():
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
]
board = Tetris(initBoard)
assert bumpiness(board) == 9
assert calculate_heights(board)[2] == 9


def test_bumpiness_with_holes():
Expand Down Expand Up @@ -419,7 +420,7 @@ def test_bumpiness_with_holes():
[1, 1, 1, 0, 1, 0, 1, 0, 1, 0],
]
board = Tetris(initBoard)
assert bumpiness(board) == 0
assert calculate_heights(board)[2] == 0


def test_bumpiness_40():
Expand Down Expand Up @@ -447,7 +448,7 @@ def test_bumpiness_40():
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
]
board = Tetris(initBoard)
assert bumpiness(board) == 40
assert calculate_heights(board)[2] == 40


def test_aggregate_height_zero():
Expand Down

0 comments on commit 6eaeca7

Please sign in to comment.