Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 18 additions & 4 deletions homemade.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,41 @@ 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)

if maximizing:
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 ---
Expand All @@ -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:
Expand Down
Loading