Skip to content

Commit

Permalink
fix: changed up the tests and underlying code so that they are correct
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Prokhorikhin committed Apr 22, 2024
1 parent 16fa161 commit db75d86
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/agents/heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def aggregate_heights(gameState: Tetris) -> int:
checkedList = [0 for i in range(gameState.COLUMNS)]
for row in range(gameState.ROWS):
for column in range(gameState.COLUMNS):
if gameState.board[row][column] != 0:
if gameState.prevBoard[row][column] != 0:
if checkedList[column] == 0:
checkedList[column] = gameState.ROWS - row
return sum(checkedList)
Expand All @@ -40,7 +40,7 @@ def max_height(gameState: Tetris) -> int:
checkedList = [0 for i in range(gameState.COLUMNS)]
for row in range(gameState.ROWS):
for column in range(gameState.COLUMNS):
if gameState.board[row][column] > 0:
if gameState.prevBoard[row][column] > 0:
if checkedList[column] == 0:
checkedList[column] = gameState.ROWS - row
return max(checkedList)
Expand All @@ -62,7 +62,7 @@ def bumpiness(gameState: Tetris) -> int:
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.SPAWN_ROWS, gameState.ROWS):
if gameState.board[row][column] > 0:
if gameState.prevBoard[row][column] > 0:
if columnHeightMap[column] == 0:
columnHeightMap[column] = max_height - row

Expand Down Expand Up @@ -100,9 +100,9 @@ def find_holes(gameState: Tetris) -> int:
for column in range(gameState.COLUMNS):
top_block = gameState.ROWS
for row in range(gameState.ROWS):
if (gameState.board[row][column] == 1) and (row < top_block):
if (gameState.prevBoard[row][column] == 1) and (row < top_block):
top_block = row
if (gameState.board[row][column] == 0) and (row > top_block):
if (gameState.prevBoard[row][column] == 0) and (row > top_block):
holes += 1

return holes
2 changes: 1 addition & 1 deletion src/game/tetris.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def transition_model(current_state: Tetris, target_state: Tetris) -> list[Action
actions += [Action.MOVE_RIGHT] * (target_block.x - current_state.block.x)
elif current_state.block.x > target_block.x:
actions += [Action.MOVE_LEFT] * (current_state.block.x - target_block.x)
# Move the block down to the correct y position
# Move the block down to the correct y position as it would be used in reality
actions.append(Action.HARD_DROP)

return actions
13 changes: 6 additions & 7 deletions test/agents/test_heuristic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_result_heuristic_agent():
]

expected_board = [
[0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 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 @@ -53,18 +53,17 @@ def test_result_heuristic_agent():
[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, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]

block = Block(3, 0, 1)
board = Tetris(initBoard, block)

agent: HeuristicAgent = create_agent("heuristic")
possible_moves = board.getPossibleBoards()
result = agent.result(board)
for i in range(len(result)-1):
for i in range(len(result)):
board.doAction(result[i])
print(result)
for row in board.board:
Expand Down
20 changes: 4 additions & 16 deletions test/game/test_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ def test_transition_model_x_direction():
target_board.doAction(action)
actions = transition_model(current_board, target_board)
assert isinstance(actions, list)
assert len(actions) == 1
assert action in actions
assert len(actions) == 2 # 1 for moving right, 1 for hard drop which is always appended


def test_transition_model_complex_target():
current_board: Tetris = Tetris()
initial_block = Block(0, 3, 0)
current_board: Tetris = Tetris(None, initial_block)
target_board: Tetris = copy.deepcopy(current_board)
actual_actions = [
Action.ROTATE_CLOCKWISE,
Expand All @@ -211,7 +213,6 @@ def test_transition_model_complex_target():
]
for action in actual_actions:
target_board.doAction(action)
target_board.updateBoard()

actions = transition_model(current_board, target_board)
assert isinstance(actions, list)
Expand All @@ -230,7 +231,6 @@ def test_transition_model_left_movement():
]
for action in actual_actions:
target_board.doAction(action)
target_board.updateBoard()

actions = transition_model(current_board, target_board)
assert isinstance(actions, list)
Expand All @@ -250,14 +250,10 @@ def test_transition_model_execution():
]
for action in actual_actions:
target_board.doAction(action)
if target_board.blockHasLanded:
target_board.updateBoard()

actions = transition_model(current_board, target_board)
for action in actions:
current_board.doAction(action)
if current_board.blockHasLanded:
current_board.updateBoard()
assert current_board == target_board


Expand All @@ -274,14 +270,10 @@ def test_transition_model_execution_complex():
]
for action in actual_actions:
target_board.doAction(action)
if target_board.blockHasLanded:
target_board.updateBoard()

actions = transition_model(current_board, target_board)
for action in actions:
current_board.doAction(action)
if current_board.blockHasLanded:
current_board.updateBoard()
assert current_board == target_board


Expand All @@ -293,12 +285,8 @@ def test_transition_model_execution_of_invalid_move_sequence():
actual_actions += [Action.HARD_DROP]
for action in actual_actions:
target_board.doAction(action)
if target_board.blockHasLanded:
target_board.updateBoard()

actions = transition_model(current_board, target_board)
for action in actions:
current_board.doAction(action)
if current_board.blockHasLanded:
current_board.updateBoard()
assert current_board == target_board

0 comments on commit db75d86

Please sign in to comment.