From cfe54f83104b5c8f35674eac1fe0c7e4c60cba92 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 23 Oct 2023 18:15:03 +0530 Subject: [PATCH 1/7] Enhance readability of Minimax --- backtracking/minimax.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 6e310131e069..595f670c85f0 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -12,10 +12,22 @@ import math -def minimax( - depth: int, node_index: int, is_max: bool, scores: list[int], height: float -) -> int: +def minimax(depth: int, node_index: int, is_max: bool, scores: list[int], height: float) -> int: """ + This function implements the minimax algorithm, + which helps achieve the maximum score in a game + by checking all possible moves. + + Parameters: + - depth: Current depth in the game tree. + - node_index: Index of the current node in the scores list. + - is_max: A boolean indicating whether the current move is for the maximizer (True) or minimizer (False). + - scores: A list containing the scores of the leaves of the game tree. + - height: The maximum height of the game tree. + + Returns: + - An integer representing the optimal score for the current player. + >>> import math >>> scores = [90, 23, 6, 33, 21, 65, 123, 34423] >>> height = math.log(len(scores), 2) @@ -35,21 +47,24 @@ def minimax( 12 """ + # Check for invalid inputs if depth < 0: raise ValueError("Depth cannot be less than 0") - if len(scores) == 0: raise ValueError("Scores cannot be empty") + # Base case: If the current depth equals the height of the tree, return the score of the current node. if depth == height: return scores[node_index] + # If it's the maximizer's turn, choose the maximum score between the two possible moves. if is_max: return max( minimax(depth + 1, node_index * 2, False, scores, height), minimax(depth + 1, node_index * 2 + 1, False, scores, height), ) + # If it's the minimizer's turn, choose the minimum score between the two possible moves. return min( minimax(depth + 1, node_index * 2, True, scores, height), minimax(depth + 1, node_index * 2 + 1, True, scores, height), @@ -57,8 +72,11 @@ def minimax( def main() -> None: + # Sample scores and height calculation scores = [90, 23, 6, 33, 21, 65, 123, 34423] height = math.log(len(scores), 2) + + # Calculate and print the optimal value using the minimax algorithm print("Optimal value : ", end="") print(minimax(0, 0, True, scores, height)) @@ -66,5 +84,5 @@ def main() -> None: if __name__ == "__main__": import doctest - doctest.testmod() - main() + doctest.testmod() # Run doctests to ensure the function behaves as expected + main() # Run the main function to demonstrate the minimax algorithm in action From cd9f3f48ba6f54ea1e793b23360411438974039d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 12:47:38 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/minimax.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 595f670c85f0..baf5732dd5fe 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -12,7 +12,9 @@ import math -def minimax(depth: int, node_index: int, is_max: bool, scores: list[int], height: float) -> int: +def minimax( + depth: int, node_index: int, is_max: bool, scores: list[int], height: float +) -> int: """ This function implements the minimax algorithm, which helps achieve the maximum score in a game From eed78caabdb6ba2c4611a07a4f23e1ace6df45f9 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 23 Oct 2023 18:19:31 +0530 Subject: [PATCH 3/7] Reduce line overflow --- backtracking/minimax.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 595f670c85f0..96359d8636a5 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -12,7 +12,9 @@ import math -def minimax(depth: int, node_index: int, is_max: bool, scores: list[int], height: float) -> int: +def minimax( + depth: int, node_index: int, is_max: bool, scores: list[int], + height: float) -> int: """ This function implements the minimax algorithm, which helps achieve the maximum score in a game @@ -21,7 +23,8 @@ def minimax(depth: int, node_index: int, is_max: bool, scores: list[int], height Parameters: - depth: Current depth in the game tree. - node_index: Index of the current node in the scores list. - - is_max: A boolean indicating whether the current move is for the maximizer (True) or minimizer (False). + - is_max: A boolean indicating whether the current move + is for the maximizer (True) or minimizer (False). - scores: A list containing the scores of the leaves of the game tree. - height: The maximum height of the game tree. @@ -53,18 +56,21 @@ def minimax(depth: int, node_index: int, is_max: bool, scores: list[int], height if len(scores) == 0: raise ValueError("Scores cannot be empty") - # Base case: If the current depth equals the height of the tree, return the score of the current node. + # Base case: If the current depth equals the height of the tree, + # return the score of the current node. if depth == height: return scores[node_index] - # If it's the maximizer's turn, choose the maximum score between the two possible moves. + # If it's the maximizer's turn, choose the maximum score + # between the two possible moves. if is_max: return max( minimax(depth + 1, node_index * 2, False, scores, height), minimax(depth + 1, node_index * 2 + 1, False, scores, height), ) - # If it's the minimizer's turn, choose the minimum score between the two possible moves. + # If it's the minimizer's turn, choose the minimum score + # between the two possible moves. return min( minimax(depth + 1, node_index * 2, True, scores, height), minimax(depth + 1, node_index * 2 + 1, True, scores, height), From e0282c92e3e66985f534126e0f6f8b3eef612952 Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:23:23 +0530 Subject: [PATCH 4/7] Update backtracking/minimax.py Co-authored-by: Tianyi Zheng --- backtracking/minimax.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 085f6d5ec17b..2f42c5fbe7f1 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -50,7 +50,6 @@ def minimax( 12 """ - # Check for invalid inputs if depth < 0: raise ValueError("Depth cannot be less than 0") if len(scores) == 0: From 36ca2c92652770d4a1dda1f61ab7325fd688c02f Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:23:43 +0530 Subject: [PATCH 5/7] Update backtracking/minimax.py Co-authored-by: Tianyi Zheng --- backtracking/minimax.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 2f42c5fbe7f1..3545f3caea9c 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -89,5 +89,5 @@ def main() -> None: if __name__ == "__main__": import doctest - doctest.testmod() # Run doctests to ensure the function behaves as expected - main() # Run the main function to demonstrate the minimax algorithm in action + doctest.testmod() + main() From 9474d7546ec64da5e867d7ba8f6a60fcc90891d8 Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:23:54 +0530 Subject: [PATCH 6/7] Update backtracking/minimax.py Co-authored-by: Tianyi Zheng --- backtracking/minimax.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 3545f3caea9c..3ece19402353 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -16,9 +16,10 @@ def minimax( depth: int, node_index: int, is_max: bool, scores: list[int], height: float ) -> int: """ - This function implements the minimax algorithm, - which helps achieve the maximum score in a game - by checking all possible moves. + This function implements the minimax algorithm, which helps achieve the optimal + score for a player in a two-player game by checking all possible moves. If the player + is the maximizer, then the score is maximized. If the player is the minimizer, then + the score is minimized. Parameters: - depth: Current depth in the game tree. From b17d6d7ff6f57f9876fbc4cf04653f53f17c4d1b Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Mon, 23 Oct 2023 19:26:27 +0530 Subject: [PATCH 7/7] Remove line overflow --- backtracking/minimax.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backtracking/minimax.py b/backtracking/minimax.py index 3ece19402353..6dece2990a1c 100644 --- a/backtracking/minimax.py +++ b/backtracking/minimax.py @@ -17,9 +17,9 @@ def minimax( ) -> int: """ This function implements the minimax algorithm, which helps achieve the optimal - score for a player in a two-player game by checking all possible moves. If the player - is the maximizer, then the score is maximized. If the player is the minimizer, then - the score is minimized. + score for a player in a two-player game by checking all possible moves. + If the player is the maximizer, then the score is maximized. + If the player is the minimizer, then the score is minimized. Parameters: - depth: Current depth in the game tree.