o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Further enhance, and hide the chain of thought processing only make it output the chess…
- User
Further enhance, and hide the chain of thought processing only make it output the chess move and the explanation. ALSO add an advanced superpositional chess method with AI tuning.
Keep the g4f module, because it's important.
import chess import chess.pgn import g4f from g4f.client import Client import random import re import time from typing import List, Tuple, Optional import numpy as np class NapoleonChessEngine: def __init__(self): self.board = chess.Board() self.client = Client(provider=g4f.Provider.Bixin123) self.depth = 5 self.opening_book = self.create_opening_book() self.game_history = chess.pgn.Game() self.current_node = self.game_history self.personality_traits = [ "Aggressive", "Tactically brilliant", "Risk-taker", "Center-focused", "Rapid development" ] self.piece_values = { chess.PAWN: 100, chess.KNIGHT: 320, chess.BISHOP: 330, chess.ROOK: 500, chess.QUEEN: 900, chess.KING: 20000 } self.position_tables = self.create_position_tables() self.transposition_table = {} self.killer_moves = [[None, None] for _ in range(100)] self.history_table = np.zeros((64, 64), dtype=np.int32) def create_opening_book(self): # Expanded opening book return { "": ["e2e4", "d2d4", "c2c4", "g1f3"], "e2e4": ["e7e5", "c7c5", "e7e6", "c7c6"], "d2d4": ["d7d5", "g8f6", "e7e6", "c7c5"], "c2c4": ["e7e5", "c7c5", "g8f6", "e7e6"], "g1f3": ["d7d5", "g8f6", "c7c5", "e7e6"], "e2e4 e7e5": ["g1f3", "b1c3", "f2f4"], "d2d4 d7d5": ["c2c4", "g1f3", "b1c3"], "c2c4 e7e5": ["b1c3", "g1f3", "g2g3"], "g1f3 d7d5": ["d2d4", "c2c4", "g2g3"] } def create_position_tables(self): return { chess.PAWN: [ 0, 0, 0, 0, 0, 0, 0, 0, 50, 50, 50, 50, 50, 50, 50, 50, 10, 10, 20, 30, 30, 20, 10, 10, 5, 5, 10, 25, 25, 10, 5, 5, 0, 0, 0, 20, 20, 0, 0, 0, 5, -5,-10, 0, 0,-10, -5, 5, 5, 10, 10,-20,-20, 10, 10, 5, 0, 0, 0, 0, 0, 0, 0, 0 ], chess.KNIGHT: [ -50,-40,-30,-30,-30,-30,-40,-50, -40,-20, 0, 0, 0, 0,-20,-40, -30, 0, 10, 15, 15, 10, 0,-30, -30, 5, 15, 20, 20, 15, 5,-30, -30, 0, 15, 20, 20, 15, 0,-30, -30, 5, 10, 15, 15, 10, 5,-30, -40,-20, 0, 5, 5, 0,-20,-40, -50,-40,-30,-30,-30,-30,-40,-50 ], chess.BISHOP: [ -20,-10,-10,-10,-10,-10,-10,-20, -10, 0, 0, 0, 0, 0, 0,-10, -10, 0, 5, 10, 10, 5, 0,-10, -10, 5, 5, 10, 10, 5, 5,-10, -10, 0, 10, 10, 10, 10, 0,-10, -10, 10, 10, 10, 10, 10, 10,-10, -10, 5, 0, 0, 0, 0, 5,-10, -20,-10,-10,-10,-10,-10,-10,-20 ], chess.ROOK: [ 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 10, 10, 10, 10, 10, 5, -5, 0, 0, 0, 0, 0, 0, -5, -5, 0, 0, 0, 0, 0, 0, -5, -5, 0, 0, 0, 0, 0, 0, -5, -5, 0, 0, 0, 0, 0, 0, -5, -5, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 5, 5, 0, 0, 0 ], chess.QUEEN: [ -20,-10,-10, -5, -5,-10,-10,-20, -10, 0, 0, 0, 0, 0, 0,-10, -10, 0, 5, 5, 5, 5, 0,-10, -5, 0, 5, 5, 5, 5, 0, -5, 0, 0, 5, 5, 5, 5, 0, -5, -10, 5, 5, 5, 5, 5, 0,-10, -10, 0, 5, 0, 0, 0, 0,-10, -20,-10,-10, -5, -5,-10,-10,-20 ], chess.KING: [ -30,-40,-40,-50,-50,-40,-40,-30, -30,-40,-40,-50,-50,-40,-40,-30, -30,-40,-40,-50,-50,-40,-40,-30, -30,-40,-40,-50,-50,-40,-40,-30, -20,-30,-30,-40,-40,-30,-30,-20, -10,-20,-20,-20,-20,-20,-20,-10, 20, 20, 0, 0, 0, 0, 20, 20, 20, 30, 10, 0, 0, 10, 30, 20 ] } def get_legal_moves(self) -> List[str]: return list(map(self.board.san, self.board.legal_moves)) def is_legal_move(self, move: str) -> bool: try: self.board.push_san(move) self.board.pop() return True except ValueError: return False def make_move(self, move: str) -> Optional[str]: try: move_obj = self.board.parse_san(move) self.board.push(move_obj) self.current_node = self.current_node.add_variation(move_obj) return None except ValueError: return "Invalid move. Please try again." def evaluate_position(self) -> int: if self.board.is_checkmate(): return -99999 if self.board.turn else 99999 if self.board.is_stalemate() or self.board.is_insufficient_material(): return 0 material = sum(len(self.board.pieces(piece_type, chess.WHITE)) * value - len(self.board.pieces(piece_type, chess.BLACK)) * value for piece_type, value in self.piece_values.items()) position = sum(self.position_tables[piece_type][square] for piece_type in chess.PIECE_TYPES for square in self.board.pieces(piece_type, chess.WHITE)) position -= sum(self.position_tables[piece_type][chess.square_mirror(square)] for piece_type in chess.PIECE_TYPES for square in self.board.pieces(piece_type, chess.BLACK)) # Mobility mobility = len(list(self.board.legal_moves)) if not self.board.turn: self.board.push(chess.Move.null()) mobility -= len(list(self.board.legal_moves)) self.board.pop() else: self.board.push(chess.Move.null()) mobility += len(list(self.board.legal_moves)) self.board.pop() # Pawn structure pawn_structure = self.evaluate_pawn_structure() # King safety king_safety = self.evaluate_king_safety() eval = material + position + mobility * 10 + pawn_structure + king_safety return eval if self.board.turn else -eval def evaluate_pawn_structure(self) -> int: white_pawns = self.board.pieces(chess.PAWN, chess.WHITE) black_pawns = self.board.pieces(chess.PAWN, chess.BLACK) doubled_pawns = sum(len(list(filter(lambda sq: sq & 7 == file, white_pawns))) > 1 for file in range(8)) doubled_pawns -= sum(len(list(filter(lambda sq: sq & 7 == file, black_pawns))) > 1 for file in range(8)) isolated_pawns = sum(not any(sq & 7 in (file - 1, file + 1) for sq in white_pawns) for file in range(8) if any(sq & 7 == file for sq in white_pawns)) isolated_pawns -= sum(not any(sq & 7 in (file - 1, file + 1) for sq in black_pawns) for file in range(8) if any(sq & 7 == file for sq in black_pawns)) return -doubled_pawns * 50 - isolated_pawns * 30 def evaluate_king_safety(self) -> int: white_king_sq = self.board.king(chess.WHITE) black_king_sq = self.board.king(chess.BLACK) white_attackers = len(self.board.attackers(chess.BLACK, white_king_sq)) black_attackers = len(self.board.attackers(chess.WHITE, black_king_sq)) return (black_attackers - white_attackers) * 50 def generate_smart_prompt(self) -> str: evaluation = self.evaluate_position() legal_moves = self.get_legal_moves() return f"""As Napoleon, analyze this chess position and suggest the best move: FEN: {self.board.fen()} Move history: {" ".join([move.uci() for move in self.board.move_stack])} Legal moves: {", ".join(legal_moves)} The current evaluation score is: {evaluation} (A positive score means White is winning, while a negative score means Black is winning.) Consider these aspects of my playstyle: 1. Aggressive control of the center 2. Swift development of pieces 3. Calculated risks for positional advantages 4. Emphasis on mobility and rapid attacks 5. Willingness to sacrifice for initiative Analyze the position step by step: 1. Material balance 2. Piece activity and development 3. King safety 4. Pawn structure 5. Control of key squares and lines Choose the best legal move from the provided options to maximize your winning chances. Ensure the move is valid. Explain your thought process and the reasons behind your choice in a step-by-step manner, reflecting Napoleon's strategic thinking. Move: [Your chosen move] Explanation: 1. [First step of your reasoning] 2. [Second step of your reasoning] 3. [Third step of your reasoning] ... Conclusion: [Summarize why this move aligns with Napoleon's style and the current position]""" def get_napoleon_move(self) -> Optional[chess.Move]: prompt = self.generate_smart_prompt() response = self.client.chat.completions.create( model="gpt-4-turbo", messages=[{"role": "user", "content": prompt}] ) content = response.choices[0].message.content move_match = re.search(r"Move:\s*(\S+)", content) explanation = re.search(r"Explanation:(.*?)Conclusion:", content, re.DOTALL) conclusion = re.search(r"Conclusion:\s*(.*)", content) if move_match and self.is_legal_move(move_match.group(1)): move = move_match.group(1) print(f"Napoleon's move: {move}") if explanation: print(f"Explanation:\n{explanation.group(1).strip()}") if conclusion: print(f"Conclusion: {conclusion.group(1).strip()}") return self.board.parse_san(move) return None def iterative_deepening(self, time_limit=5): best_move = None start_time = time.time() for current_depth in range(1, self.depth + 1): move, _ = self.negamax(current_depth, float('-inf'), float('inf'), 1) if move: best_move = move if time.time() - start_time > time_limit: break return best_move def negamax(self, depth, alpha, beta, color): alpha_orig = alpha # Transposition table lookup tt_entry = self.transposition_table.get(self.board.fen()) if tt_entry and tt_entry['depth'] >= depth: if tt_entry['flag'] == 'EXACT': return tt_entry['move'], tt_entry['score'] elif tt_entry['flag'] == 'LOWERBOUND': alpha = max(alpha, tt_entry['score']) elif tt_entry['flag'] == 'UPPERBOUND': beta = min(beta, tt_entry['score']) if alpha >= beta: return tt_entry['move'], tt_entry['score'] if depth == 0 or self.board.is_game_over(): return None, color * self.quiescence_search(alpha, beta) best_move = None max_score = float('-inf') for move in self.get_sorted_moves(): self.board.push(move) _, score = self.negamax(depth - 1, -beta, -alpha, -color) score = -score self.board.pop() if score > max_score: max_score = score best_move = move alpha = max(alpha, score) if alpha >= beta: self.killer_moves[depth] = [self.killer_moves[depth][1], move] break # Transposition table store flag = 'EXACT' if max_score <= alpha_orig: flag = 'UPPERBOUND' elif max_score >= beta: flag = 'LOWERBOUND' self.transposition_table[self.board.fen()] = { 'move': best_move, 'score': max_score, 'depth': depth, 'flag': flag } return best_move, max_score def quiescence_search(self, alpha, beta): stand_pat = self.evaluate_position() if stand_pat >= beta: return beta if alpha < stand_pat: alpha = stand_pat for move in self.board.legal_moves: if self.board.is_capture(move): self.board.push(move) score = -self.quiescence_search(-beta, -alpha) self.board.pop() if score >= beta: return beta if score > alpha: alpha = score return alpha def get_sorted_moves(self): def move_value(move): if move == self.killer_moves[len(self.board.move_stack)][0]: return 9000 if move == self.killer_moves[len(self.board.move_stack)][1]: return 8000 if self.board.is_capture(move): return 7000 + self.mvv_lva(move) return self.history_table[move.from_square][move.to_square] return sorted(self.board.legal_moves, key=move_value, reverse=True) def mvv_lva(self, move): if self.board.is_en_passant(move): return self.piece_values[chess.PAWN] return (self.piece_values[self.board.piece_type_at(move.to_square)] * 100 - self.piece_values[self.board.piece_type_at(move.from_square)]) def get_opening_move(self): move_history = " ".join([move.uci() for move in self.board.move_stack]) if move_history in self.opening_book: return random.choice(self.opening_book[move_history]) return None def play_engine_move(self): opening_move = self.get_opening_move() if opening_move: chosen_move = chess.Move.from_uci(opening_move) else: napoleon_move = self.get_napoleon_move() calculated_move = self.iterative_deepening() # 50/50 chance of choosing Napoleon's move or the calculated move chosen_move = napoleon_move if napoleon_move and random.random() < 0.5 else calculated_move if chosen_move: self.board.push(chosen_move) self.current_node = self.current_node.add_variation(chosen_move) self.history_table[chosen_move.from_square][chosen_move.to_square] += self.depth * self.depth return chosen_move def display_board(self): return str(self.board) def get_game_pgn(self): game = self.game_history.game() game.headers["Event"] = "Napoleon vs Human" game.headers["Site"] = "G4F Chess Engine" game.headers["Date"] = time.strftime("%Y.%m.%d") return str(game) def main(): engine = NapoleonChessEngine() print("Welcome to the Enhanced Napoleon Chess Engine!") print("Enter your moves in standard algebraic notation (e.g., e4, Nf3).") print("Napoleon's personality traits:", ", ".join(engine.personality_traits)) while not engine.board.is_game_over(): print(engine.display_board()) print("FEN:", engine.board.fen()) print("Evaluation:", engine.evaluate_position()) print("Legal moves:", engine.get_legal_moves()) if engine.board.turn == chess.WHITE: user_move = input("Your move: ") if user_move.lower() == 'quit': break response = engine.make_move(user_move) if response: print(response) continue else: engine_move = engine.play_engine_move() if not engine_move: print("Napoleon failed to make a move. Game over.") break print(f"Napoleon's move: {engine_move.uci()}") print("Game over!") print(engine.display_board()) print("\nGame PGN:") print(engine.get_game_pgn()) if __name__ == "__main__": main() - Assistant