feat: add IDDFS player

This commit is contained in:
Nemo D'ACREMONT 2025-05-08 10:42:34 +02:00
parent 78b90d4027
commit 614bb5acb2
No known key found for this signature in database
GPG Key ID: 85F245EC3BB1E022
3 changed files with 106 additions and 86 deletions

View File

@ -1,26 +1,6 @@
import chess
import math
def b(board: chess.Board, color: chess.Color):
piece_values = {
chess.PAWN: 1,
chess.KNIGHT: 3,
chess.BISHOP: 3,
chess.ROOK: 5,
chess.QUEEN: 9,
chess.KING: 0
}
score = 0
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece is not None:
if piece.color == chess.WHITE:
score += piece_values[piece.piece_type]
else:
score -= piece_values[piece.piece_type]
return score
def h_shannon(board: chess.Board, color: chess.Color):
piece_values = {
chess.PAWN: 1,
@ -42,6 +22,7 @@ def h_shannon(board: chess.Board, color: chess.Color):
for square in board.piece_map():
piece = board.piece_at(square)
if piece is not None:
n = 1 if piece.color == color else -1
acc += n * piece_values[piece.piece_type]

View File

@ -24,14 +24,21 @@ def play(white: player.PlayerInterface, black: player.PlayerInterface):
board = chess.Board()
white.newGame(chess.WHITE)
black.newGame(chess.BLACK)
whiteTime = 0
blackTime = 0
i = 1
while not board.is_game_over():
try:
st = time.time()
if board.turn == chess.WHITE:
move = step(board, white, black)
nd = time.time()
whiteTime += (nd - st)
else:
move = step(board, black, white)
nd = time.time()
blackTime += (nd - st)
except:
print(board)
print(f"{color2str(board.turn)} cheated, end the game")
@ -42,9 +49,12 @@ def play(white: player.PlayerInterface, black: player.PlayerInterface):
print(board)
print(f"Move {move}")
print(f"Turn {i}")
print(f"Time consumed : {nd - st:.5}")
i += 1
print(board.outcome())
print(f"White time : {whiteTime:.5}")
print(f"Black time : {blackTime:.5}")
return board
@ -74,6 +84,7 @@ def t_enum(board, depth):
if __name__ == "__main__":
black = player.RandomPlayer()
white = player.AlphaBetaPlayer()
# white = player.IDDFSPlayer()
board = play(white, black)
print(board)

View File

@ -1,4 +1,5 @@
from random import randint, choice
import time
import math
from typing import Callable
import chess
@ -125,13 +126,8 @@ class MinMaxPlayer(PlayerInterface):
return move
def alphabeta(
board: chess.Board,
heuristic: Callable[[chess.Board, chess.Color], float],
color: chess.Color,
depth: int = 3,
) -> chess.Move:
def aux(
# Returns heuristic, move
def _alphabeta(
board: chess.Board,
heuristic: Callable[[chess.Board, chess.Color], float],
color: chess.Color,
@ -148,7 +144,7 @@ def alphabeta(
for move in board.generate_legal_moves():
board.push(move)
value = (
aux(
_alphabeta(
board,
alpha=alpha,
beta=beta,
@ -174,7 +170,7 @@ def alphabeta(
for move in board.generate_legal_moves():
board.push(move)
value = (
aux(
_alphabeta(
board,
alpha=alpha,
beta=beta,
@ -197,7 +193,15 @@ def alphabeta(
return acc
_, move = aux(board, heuristic=heuristic, color=color, depth=depth)
def alphabeta(
board: chess.Board,
heuristic: Callable[[chess.Board, chess.Color], float],
color: chess.Color,
depth: int = 3,
) -> chess.Move:
_, move = _alphabeta(board, heuristic=heuristic, color=color, depth=depth)
return move
@ -214,3 +218,27 @@ class AlphaBetaPlayer(PlayerInterface):
)
self.board.push(move)
return move
def IDDFS(
board: chess.Board, heuristic, color: chess.Color, duration: float, maxdepth=42
) -> chess.Move:
st = time.time()
depth = 1
move = chess.Move.null()
while time.time() - st < duration and depth < maxdepth:
move = _alphabeta(board, heuristic, color, alpha=-10, beta=10, depth=depth)[1]
depth += 1
return move
class IDDFSPlayer(PlayerInterface):
# Returns your player name , as to be displayed during the game
def getPlayerName(self) -> str:
return "AlphaBeta Player"
def getPlayerMove(self):
move = IDDFS(self.board, h_shannon, self.color, 1)
self.board.push(move)
return move