From a6653d0266c6be16bc9eaa50b240508248ac8e57 Mon Sep 17 00:00:00 2001 From: Stefan Steininger Date: Wed, 31 Jan 2024 12:40:59 +0100 Subject: [PATCH] refactoring --- chesspp/engine_factory.py | 2 +- chesspp/{eval_pesto.py => pesto_strategy.py} | 35 +++++++++++++++++++- chesspp/web.py | 9 ++--- web.py | 2 +- 4 files changed, 38 insertions(+), 10 deletions(-) rename chesspp/{eval_pesto.py => pesto_strategy.py} (94%) diff --git a/chesspp/engine_factory.py b/chesspp/engine_factory.py index f402157..53d9ec6 100644 --- a/chesspp/engine_factory.py +++ b/chesspp/engine_factory.py @@ -5,7 +5,7 @@ from chesspp.lc0_strategy import Lc0Strategy from chesspp.random_strategy import RandomStrategy from chesspp.stockfish_strategy import StockFishStrategy from chesspp.random_stockfish_strategy import RandomStockfishStrategy -from chesspp.eval_pesto import PestoStrategy +from chesspp.pesto_strategy import PestoStrategy from chesspp.i_strategy import IStrategy import chess diff --git a/chesspp/eval_pesto.py b/chesspp/pesto_strategy.py similarity index 94% rename from chesspp/eval_pesto.py rename to chesspp/pesto_strategy.py index 2404ebb..79c3e80 100644 --- a/chesspp/eval_pesto.py +++ b/chesspp/pesto_strategy.py @@ -1,7 +1,9 @@ import chess.engine import chess -from chesspp.i_strategy import IStrategy +from functools import cache +from chesspp.i_strategy import IStrategy +import numba # Scoring based on PeSTO (Piece-Square Tables Only) Evaluation Functions # https://www.chessprogramming.org/PeSTO%27s_Evaluation_Function @@ -224,6 +226,37 @@ eg_table = [ ] +#import chess +import sys + + +def minimax(depth, board, alpha, beta, is_maximizing): + if depth == 0 or board.is_game_over(): + return score(board) + + if is_maximizing: + best_move = -9999 + for move in board.legal_moves: + board.push(move) + best_move = max(best_move, minimax(depth - 1, board, alpha, beta, not is_maximizing)) + board.pop() + alpha = max(alpha, best_move) + if beta <= alpha: + return best_move + return best_move + else: + best_move = 9999 + for x in board.legal_moves: + move = chess.Move.from_uci(str(x)) + board.push(move) + best_move = min(best_move, minimax(depth - 1, board, alpha, beta, not is_maximizing)) + board.pop() + beta = min(beta, best_move) + if beta <= alpha: + return best_move + return best_move + + def score(board: chess.Board) -> int: mg = [0, 0] eg = [0, 0] diff --git a/chesspp/web.py b/chesspp/web.py index 8e00e26..f4c773e 100644 --- a/chesspp/web.py +++ b/chesspp/web.py @@ -8,7 +8,7 @@ import chess from chesspp import engine from chesspp.engine_factory import EngineFactory from chesspp.stockfish_strategy import StockFishStrategy -from chesspp.eval_pesto import PestoStrategy +from chesspp.pesto_strategy import PestoStrategy _DIR = os.path.abspath(os.path.dirname(__file__)) _DATA_DIR = os.path.abspath(os.path.join(_DIR, "static_data")) @@ -75,7 +75,7 @@ class WebInterface: async def turns(): """ Simulates the game and sends the response to the client """ white = EngineFactory.create_engine(self.white, self.strategy1, chess.WHITE, self.stockfish_path, self.lc0_path) - black = EngineFactory.create_engine(self.black, self.strategy1, chess.BLACK, self.stockfish_path, self.lc0_path) + black = EngineFactory.create_engine(self.black, self.strategy2, chess.BLACK, self.stockfish_path, self.lc0_path) runner = Simulate(white, black).run(self.limit) def sim(): return next(runner, None) @@ -102,8 +102,3 @@ class WebInterface: web.static('/img/chesspieces/wikipedia/', _DATA_DIR), ]) web.run_app(app) - - -def run_sample(): - limit = engine.Limit(time=1) - WebInterface(engine.BayesMctsEngine, engine.ClassicMctsEngine, limit).run_app() diff --git a/web.py b/web.py index 0355157..ffadd5e 100644 --- a/web.py +++ b/web.py @@ -5,5 +5,5 @@ from main import read_arguments if __name__ == '__main__': engine1, engine2, strategy1, strategy2, n_games, time, stockfish_path, lc0_path, n_proc = read_arguments() - limit = engine.Limit(time=0.5) + limit = engine.Limit(time=time) web.WebInterface(engine1, engine2, strategy1, strategy2, stockfish_path, lc0_path, limit).run_app()