From db75d86dd06a9fa2a6c1ce841cb6214c7c39814c Mon Sep 17 00:00:00 2001 From: Eduard Prokhorikhin Date: Mon, 22 Apr 2024 18:48:49 +0200 Subject: [PATCH] fix: changed up the tests and underlying code so that they are correct --- src/agents/heuristic.py | 10 +++++----- src/game/tetris.py | 2 +- test/agents/test_heuristic_agent.py | 13 ++++++------- test/game/test_board.py | 20 ++++---------------- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/agents/heuristic.py b/src/agents/heuristic.py index 0394496..13bd8e9 100644 --- a/src/agents/heuristic.py +++ b/src/agents/heuristic.py @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/src/game/tetris.py b/src/game/tetris.py index 3e4e6e2..44c0961 100644 --- a/src/game/tetris.py +++ b/src/game/tetris.py @@ -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 diff --git a/test/agents/test_heuristic_agent.py b/test/agents/test_heuristic_agent.py index bcf749a..c88a4f5 100644 --- a/test/agents/test_heuristic_agent.py +++ b/test/agents/test_heuristic_agent.py @@ -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], @@ -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: diff --git a/test/game/test_board.py b/test/game/test_board.py index b635be6..5ce765d 100644 --- a/test/game/test_board.py +++ b/test/game/test_board.py @@ -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, @@ -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) @@ -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) @@ -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 @@ -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 @@ -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