From c5e67f92de07778c3ffb9eb12bdbe631a0df07a9 Mon Sep 17 00:00:00 2001 From: henrinha Date: Tue, 16 Apr 2024 18:06:16 +0200 Subject: [PATCH 1/2] refactor: changed varible-symbol in for loops --- src/agents/heuristic.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/agents/heuristic.py b/src/agents/heuristic.py index e7121fb..26eea48 100644 --- a/src/agents/heuristic.py +++ b/src/agents/heuristic.py @@ -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) @@ -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 From 40c449e1a22199f983287d81eec463bcb0932966 Mon Sep 17 00:00:00 2001 From: Sindre Fossdal Date: Tue, 16 Apr 2024 18:29:41 +0200 Subject: [PATCH 2/2] test: fixed heuristic tests --- test/agents/test_heuristic.py | 48 +++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/test/agents/test_heuristic.py b/test/agents/test_heuristic.py index 96c40b7..4236afc 100644 --- a/test/agents/test_heuristic.py +++ b/test/agents/test_heuristic.py @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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"