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
maiahi committed Apr 16, 2024
2 parents 007eb89 + 63fab48 commit 3bc03df
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
32 changes: 16 additions & 16 deletions src/agents/heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ def utility(gameState: Tetris, aggregate_heights_weight: int, max_height_weight:
return sum


def aggregate_heights(gameState: Tetris) -> int:
def aggregate_heights(gameState: Tetris) -> 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):
if gameState.prevBoard[i][j] > 0:
if checkedList[j] == 0:
checkedList[j] = gameState.ROWS - i
for row in range(gameState.ROWS):
for column in range(gameState.COLUMNS):
if gameState.prevBoard[row][column] != 0:
if checkedList[column] == 0:
checkedList[column] = gameState.ROWS - row
return sum(checkedList)


def max_height(gameState: Tetris) -> 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):
if gameState.prevBoard[i][j] > 0:
if checkedList[j] == 0:
checkedList[j] = gameState.ROWS - i
for row in range(gameState.ROWS):
for column in range(gameState.COLUMNS):
if gameState.prevBoard[row][column] > 0:
if checkedList[column] == 0:
checkedList[column] = gameState.ROWS - row
return max(checkedList)


Expand Down Expand Up @@ -90,12 +90,12 @@ def find_holes(gameState: Tetris) -> int:
The heuristic value
"""
holes = 0
for i in range(gameState.COLUMNS):
for column in range(gameState.COLUMNS):
top_block = 20
for j in range(gameState.ROWS):
if (gameState.prevBoard[j][i] == 1) and (j < top_block):
top_block = j
if (gameState.prevBoard[j][i] == 0) and (j > top_block):
for row in range(gameState.ROWS):
if (gameState.prevBoard[row][column] == 1) and (row < top_block):
top_block = row
if (gameState.prevBoard[row][column] == 0) and (row > top_block):
holes += 1

return holes
48 changes: 29 additions & 19 deletions test/agents/test_heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ def test_bumpiness_empty():


def test_bumpiness_five():
board = Tetris()
board.board = [

initBoard = [
[0, 0, 0, 0, 0, 0, 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 @@ -362,12 +362,13 @@ def test_bumpiness_five():
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
board = Tetris(initBoard)
assert bumpiness(board) == 2


def test_bumpiness_nine():
board = Tetris()
board.board = [

initBoard = [
[0, 0, 0, 0, 0, 0, 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 @@ -389,12 +390,13 @@ def test_bumpiness_nine():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
]
board = Tetris(initBoard)
assert bumpiness(board) == 9


def test_bumpiness_with_holes():
board = Tetris()
board.board = [

initBoard = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Expand All @@ -416,12 +418,13 @@ def test_bumpiness_with_holes():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 1, 0, 1, 0, 1, 0],
]
board = Tetris(initBoard)
assert bumpiness(board) == 0


def test_bumpiness_40():
board = Tetris()
board.board = [

initBoard = [
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
Expand All @@ -443,12 +446,13 @@ def test_bumpiness_40():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
]
board = Tetris(initBoard)
assert bumpiness(board) == 40


def test_aggregate_height_zero():
board = Tetris()
board.board = [

initBoard = [
[0, 0, 0, 0, 0, 0, 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 @@ -470,12 +474,13 @@ def test_aggregate_height_zero():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
board = Tetris(initBoard)
assert aggregate_height(board) == 0


def test_aggregate_height_full():
board = Tetris()
board.board = [

initBoard = [
[1, 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],
Expand All @@ -497,12 +502,13 @@ def test_aggregate_height_full():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
board = Tetris(initBoard)
assert aggregate_height(board) == 200


def test_aggregate_height_half():
board = Tetris()
board.board = [

initBoard = [
[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],
Expand All @@ -524,12 +530,13 @@ def test_aggregate_height_half():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
board = Tetris(initBoard)
assert aggregate_height(board) == 100


def test_no_holes():
board = Tetris()
board.board = [

initBoard = [
[0, 0, 0, 0, 0, 0, 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 @@ -551,12 +558,14 @@ def test_no_holes():
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
board = Tetris(initBoard)
assert find_holes(board) == 0, "Expected 0 holes"


def test_no_holes():
board = Tetris()
board.board = [

def test_24_holes():

initBoard = [
[0, 0, 0, 0, 0, 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],
Expand All @@ -578,4 +587,5 @@ def test_no_holes():
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
]
board = Tetris(initBoard)
assert find_holes(board) == 24, "Expected 24 holes"

0 comments on commit 3bc03df

Please sign in to comment.