Skip to content

Commit

Permalink
feat: added maxHeight heuristic and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Prokhorikhin committed Apr 8, 2024
1 parent 774ea6c commit b972a2e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
31 changes: 31 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from src.game.board import Board
from src.agents.heuristic import *

def test_heuristic():
board = Board()
board.board =\
[[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],
[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, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 1, 0, 1, 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, 0, 0, 0, 0, 0, 0]]

print(aggregate_heights(board))
print(max_height(board))

test_heuristic()
29 changes: 10 additions & 19 deletions src/agents/heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,15 @@ def aggregate_heights(gameState: Board) -> int:
for j in range(gameState.columns):
if gameState.board[i][j] > 0:
if checkedList[j] == 0:
checkedList[j] = gameState.rows - i+1
checkedList[j] = gameState.rows - i
return sum(checkedList)




if __name__ == "__main__":
board = Board()
board.board =\
[[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, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 1, 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, 0, 0, 0, 0, 0, 0]]

assert aggregate_heights(board) == 27
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):
if gameState.board[i][j] > 0:
if checkedList[j] == 0:
checkedList[j] = gameState.rows - i
return max(checkedList)
2 changes: 0 additions & 2 deletions src/game/block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import random

import random

class Block:

def __init__(self, x, y, blockType):
Expand Down
2 changes: 1 addition & 1 deletion src/game/board.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pygame
import random
import copy
from block import Block
from src.game.block import Block

'''
Denne skriver ut brettet i terminal bare.
Expand Down
28 changes: 28 additions & 0 deletions test/agents/test_heuristic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from src.game.board import Board
from src.agents.heuristic import aggregate_heights

def test_heuristic():
board = Board()
board.board =\
[[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],
[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, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 1, 0, 1, 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, 0, 0, 0, 0, 0, 0]]

assert aggregate_heights(board) == 27

0 comments on commit b972a2e

Please sign in to comment.