Skip to content
Snippets Groups Projects
Commit 4cb5059f authored by DannyAbdi's avatar DannyAbdi
Browse files

Modified minmax algorithm to incorporate alpha beta pruning

parent d53d2a20
No related branches found
No related tags found
Loading
......@@ -34,37 +34,42 @@ class MinMax:
self.maze.current_level[y][x] != 1)
"""
Performs the MinMax algorithm to determine the best move the enemy can take.
Performs the MinMax algorithm with Alpha-beta pruning to determine the best move the enemy can take.
:param player_position: The current position of the player agent.
:param enemy_position: The current position of the enemy agent.
:param depth: The depth of the MinMax search tree.
:param maximising_player: True if maximising player (enemy), False if minimising player (player).
:param alpha: The best value that the maximizing player currently can guarantee.
:param beta: The best value that the minimizing player currently can guarantee.
:param maximizing_player: True if maximizing player (enemy), False if minimizing player (player).
:return: The best move for the enemy agent.
"""
def minmax(self, player_position, enemy_position, depth, maximising_player):
def minmax(self, player_position, enemy_position, depth, alpha, beta, maximizing_player):
if depth == 0 or self.game_over():
# Evaluate the current game state
return self.evaluate(player_position, enemy_position)
if maximising_player:
if maximizing_player:
max_eval = float('-inf')
valid_moves = self.get_valid_moves(enemy_position)
best_move = None
for move in valid_moves:
if self.is_valid_move(move):
eval = self.minmax(player_position, move, depth - 1, False)
if eval > max_eval:
max_eval = eval
best_move = move
return max_eval if depth == 1 else best_move
eval = self.minmax(player_position, move, depth - 1, alpha, beta, False)
max_eval = max(max_eval, eval)
alpha = max(alpha, eval)
if beta <= alpha:
break
return max_eval
else:
min_eval = float('inf')
valid_moves = self.get_valid_moves(player_position)
for move in valid_moves:
if self.is_valid_move(move):
eval = self.minmax(move, enemy_position, depth - 1, True)
eval = self.minmax(move, enemy_position, depth - 1, alpha, beta, True)
min_eval = min(min_eval, eval)
beta = min(beta, eval)
if beta <= alpha:
break
return min_eval
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment