diff --git a/README.md b/README.md index 8adb718..505e227 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/FN98O3k7) ## Environment setup (Python 3.10+ recommended) - Clone repo - Open VSCode and go to the program folder diff --git a/homemade.py b/homemade.py index 8d7f558..2ef2e9b 100644 --- a/homemade.py +++ b/homemade.py @@ -180,7 +180,7 @@ def evaluate(b: chess.Board) -> int: return score # --- plain minimax (no alpha-beta) --- - def minimax(b: chess.Board, depth: int, maximizing: bool) -> int: + def minimax(b: chess.Board, depth: int, maximizing: bool, alpha, beta) -> int: if depth == 0 or b.is_game_over(): return evaluate(b) @@ -188,19 +188,33 @@ def minimax(b: chess.Board, depth: int, maximizing: bool) -> int: best = -10**12 for m in b.legal_moves: b.push(m) - val = minimax(b, depth - 1, False) + val = minimax(b, depth - 1, False, alpha, beta) b.pop() if val > best: best = val + + if best > alpha: + alpha = best + + if beta <= alpha: + break + return best else: best = 10**12 for m in b.legal_moves: b.push(m) - val = minimax(b, depth - 1, True) + val = minimax(b, depth - 1, True, alpha, beta) b.pop() if val < best: best = val + + if best < beta: + beta = best + + if beta <= alpha: + break + return best # --- root move selection --- @@ -216,7 +230,7 @@ def minimax(b: chess.Board, depth: int, maximizing: bool) -> int: # Lookahead depth chosen by the simple time heuristic; subtract one for the root move for m in legal: board.push(m) - val = minimax(board, total_depth - 1, not maximizing) + val = minimax(board, total_depth - 1, not maximizing, -10**12, 10**12) board.pop() if maximizing and val > best_eval: