import chess# Potential maximum attacks from a piece on an empty boardPOTENTIAL = { chess.PAWN: 2, chess.KNIGHT: 8, chess.BISHOP: 13, chess.ROOK: 14, chess.QUEEN: 27, chess.KING: 8,}def actual_control(board, square): """Number of empty squares attacked by the piece on 'square'.""" piece = board.piece_at(square) if not piece: return 0 attacked = board.attacks(square) empty = set(range(64)) - set(board.piece_map().keys()) return len(attacked & empty)def king_safety_score(board, color): """Number of safe empty squares around the king (0-8).""" king = board.king(color) if king is None: return 0 safe = 0 for dr in (-1, 0, 1): for df in (-1, 0, 1): if dr == 0 and df == 0: continue r = chess.square_rank(king) + dr f = chess.square_file(king) + df if 0 <= r < 8 and 0 <= f < 8: sq = chess.square(f, r) if board.piece_at(sq) is None and not board.is_attacked_by(not color, sq): safe += 1 return safedef positional_supremacy(board): """White supremacy = Σ(actual * potential) - Σ(actual * potential) for Black, plus king safety weighted at 0.5 per safe square.""" white_score = 0 black_score = 0 for sq in range(64): piece = board.piece_at(sq) if piece: act = actual_control(board, sq) pot = POTENTIAL[piece.piece_type] contrib = act * pot + pot if piece.color == chess.WHITE: white_score += contrib else: black_score += contrib white_score += 0.5 * king_safety_score(board, chess.WHITE) black_score += 0.5 * king_safety_score(board, chess.BLACK) return white_score - black_scoredef piece_geometric_value(board, square): """Return the geometric contribution (act * pot) of the piece on the square.""" piece = board.piece_at(square) if not piece: return 0 return actual_control(board, square) * POTENTIAL[piece.piece_type]def attackers_defenders(board, square, color): """Return (attackers_count, defenders_count) for the given square from perspective of 'color'.""" attackers = 0 defenders = 0 for sq in chess.SQUARES: piece = board.piece_at(sq) if not piece: continue if square in board.attacks(sq): if piece.color == color: defenders += 1 else: attackers += 1 return attackers, defendersdef is_threatened(board, square, color): """Return True if the piece on 'square' is under threat and not adequately defended.""" piece = board.piece_at(square) if not piece or piece.color != color: return False attackers, defenders = attackers_defenders(board, square, color) if attackers == 0: return False if attackers > defenders: return True # Even if defended, check for bad trade (attacker less valuable than piece) piece_val = POTENTIAL[piece.piece_type] for sq in chess.SQUARES: attacker = board.piece_at(sq) if attacker and attacker.color == (not color) and square in board.attacks(sq): if POTENTIAL[attacker.piece_type] < piece_val: return True return Falsedef get_threatened_squares(board, color): """Return list of squares containing threatened pieces of the given color.""" threatened = [] for sq in chess.SQUARES: if is_threatened(board, sq, color): threatened.append(sq) return threateneddef geometric_see(board, move, color): """Simplified Static Exchange Evaluation using geometric scores. Simulate capture 'move', then opponent's best recapture on the same square. Return net change in positional supremacy from 'color's perspective. Positive means favorable trade.""" if not board.is_capture(move): return 0 original_score = positional_supremacy(board) if color == chess.BLACK: original_score = -original_score board_copy = board.copy() captured_piece = board_copy.piece_at(move.to_square) if captured_piece is None: return 0 board_copy.push(move) recapture_square = move.to_square best_opponent_score = float('inf') if color == chess.WHITE else -float('inf') for opp_move in board_copy.legal_moves: if opp_move.to_square == recapture_square and board_copy.is_capture(opp_move): b2 = board_copy.copy() b2.push(opp_move) score_after = positional_supremacy(b2) if color == chess.WHITE: if score_after < best_opponent_score: best_opponent_score = score_after else: if -score_after > best_opponent_score: best_opponent_score = -score_after if best_opponent_score == float('inf') or best_opponent_score == -float('inf'): score_after = positional_supremacy(board_copy) if color == chess.BLACK: score_after = -score_after return score_after - original_score return best_opponent_score - original_scoredef get_new_attacks(board_before, board_after, color): """Return set of enemy squares that are attacked in board_after but were not in board_before.""" new_attacks = set() enemy_color = not color for sq in chess.SQUARES: piece = board_after.piece_at(sq) if piece and piece.color == enemy_color: if board_after.is_attacked_by(color, sq) and not board_before.is_attacked_by(color, sq): new_attacks.add(sq) return new_attacksdef resolve_threats(board, color): """Three-tier threat resolution with forward safety check. Rejects moves that create new threatened pieces, unless they are favorable captures.""" threatened_squares = get_threatened_squares(board, color) candidates = set() for move in board.legal_moves: board_copy = board.copy() board_copy.push(move) new_threats = get_threatened_squares(board_copy, color) if new_threats: if board.is_capture(move): see_score = geometric_see(board, move, color) if see_score >= 0: candidates.add(move) continue if move.from_square in threatened_squares: piece_still_threatened = is_threatened(board_copy, move.to_square, color) if not piece_still_threatened: other_threats = [sq for sq in new_threats if sq != move.to_square] if not other_threats: candidates.add(move) continue if not candidates: max_threat_val = 0 for sq in threatened_squares: piece = board.piece_at(sq) if piece: val = POTENTIAL[piece.piece_type] if val > max_threat_val: max_threat_val = val if board.is_capture(move): target = board.piece_at(move.to_square) if target and POTENTIAL[target.piece_type] >= max_threat_val: candidates.add(move) continue else: new_attacks = get_new_attacks(board, board_copy, color) for enemy_sq in new_attacks: enemy_piece = board_copy.piece_at(enemy_sq) if enemy_piece and POTENTIAL[enemy_piece.piece_type] >= max_threat_val: candidates.add(move) break else: candidates.add(move) if candidates: return list(candidates) else: return list(board.legal_moves)def best_move_minimax(board): """Depth-1 minimax with threat pre-filter.""" moves_to_consider = resolve_threats(board, chess.WHITE) best_score = -float('inf') best_move = None for move in moves_to_consider: b1 = board.copy() b1.push(move) if b1.is_checkmate(): return move worst_black = float('inf') for black_move in b1.legal_moves: b2 = b1.copy() b2.push(black_move) score = positional_supremacy(b2) if score < worst_black: worst_black = score if worst_black == float('inf'): worst_black = positional_supremacy(b1) if worst_black > best_score: best_score = worst_black best_move = move return best_move or next(iter(board.legal_moves))def play(): board = chess.Board() print("=== Geometric Supremacy Engine with 3-Tier Threat Reflex ===") print("Evaluation: actual * potential + 0.5 * king_safety") print("Depth-1 minimax. Enter moves in UCI format (e.g., e2e4).\n") while not board.is_game_over(): if board.turn == chess.WHITE: move = best_move_minimax(board) print(f"Engine plays: {move}") board.push(move) else: print(board) human_move = input("Your move: ").strip() if human_move.lower() == "quit": break try: move = chess.Move.from_uci(human_move) if move in board.legal_moves: board.push(move) else: print("Illegal move. Legal UCI moves:") print(", ".join(m.uci() for m in board.legal_moves)) continue except Exception: print("Invalid format. Use e.g., e2e4") continue sup = positional_supremacy(board) print(f"Positional supremacy (White - Black): {sup:.2f}\n") print("Game over") print(board.result())if __name__ == "__main__": play()
End of Document // Next Steps
Deploy structural intelligence to your infrastructure.
Ciber-Fabbrica systems are built on these scientific pillars. If you are ready to move beyond brute-force computation, our research division is available for strategic integration.