Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +31 -6
- app.py +208 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,12 +1,37 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Ajedrez con Stockfish
|
| 3 |
+
emoji: ♟️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.0.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# ♟️ Ajedrez con Stockfish
|
| 13 |
+
|
| 14 |
+
Juega contra Stockfish, uno de los motores de ajedrez más fuertes del mundo.
|
| 15 |
+
|
| 16 |
+
## Características
|
| 17 |
+
|
| 18 |
+
- **Interfaz visual** del tablero de ajedrez
|
| 19 |
+
- **Integración con Stockfish** para movimientos de IA
|
| 20 |
+
- **Sugerencias** de mejores movimientos
|
| 21 |
+
- **Validación** de movimientos legales
|
| 22 |
+
- **Detección** de jaque, jaque mate y tablas
|
| 23 |
+
|
| 24 |
+
## Cómo jugar
|
| 25 |
+
|
| 26 |
+
1. **Ingresa movimientos** en formato UCI (ej: "e2e4" para mover peón de e2 a e4)
|
| 27 |
+
2. **Haz clic en "Realizar Movimiento"** para ejecutar tu jugada
|
| 28 |
+
3. **Usa "Sugerencia"** para obtener ayuda de Stockfish
|
| 29 |
+
4. **"Nuevo Juego"** para reiniciar la partida
|
| 30 |
+
|
| 31 |
+
## Formatos de movimiento
|
| 32 |
+
|
| 33 |
+
- **e2e4**: Mover pieza de e2 a e4
|
| 34 |
+
- **e1g1**: Enroque corto (rey blanco)
|
| 35 |
+
- **e7e8q**: Coronación a dama
|
| 36 |
+
|
| 37 |
+
¡Disfruta del juego!
|
app.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import chess
|
| 3 |
+
import chess.engine
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
+
import requests
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
# Configuración
|
| 11 |
+
BOARD_SIZE = 400
|
| 12 |
+
SQUARE_SIZE = BOARD_SIZE // 8
|
| 13 |
+
|
| 14 |
+
class ChessGame:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.board = chess.Board()
|
| 17 |
+
self.selected_square = None
|
| 18 |
+
self.valid_moves = []
|
| 19 |
+
self.game_over = False
|
| 20 |
+
self.player_turn = True
|
| 21 |
+
|
| 22 |
+
def create_board_image(self):
|
| 23 |
+
"""Crear imagen del tablero con las piezas"""
|
| 24 |
+
# Colores
|
| 25 |
+
light_square = (238, 238, 210)
|
| 26 |
+
dark_square = (118, 150, 86)
|
| 27 |
+
highlight = (186, 202, 68)
|
| 28 |
+
|
| 29 |
+
# Crear imagen
|
| 30 |
+
img = Image.new('RGB', (BOARD_SIZE, BOARD_SIZE), color='white')
|
| 31 |
+
draw = ImageDraw.Draw(img)
|
| 32 |
+
|
| 33 |
+
# Dibujar tablero
|
| 34 |
+
for row in range(8):
|
| 35 |
+
for col in range(8):
|
| 36 |
+
color = light_square if (row + col) % 2 == 0 else dark_square
|
| 37 |
+
x1 = col * SQUARE_SIZE
|
| 38 |
+
y1 = row * SQUARE_SIZE
|
| 39 |
+
x2 = x1 + SQUARE_SIZE
|
| 40 |
+
y2 = y1 + SQUARE_SIZE
|
| 41 |
+
|
| 42 |
+
draw.rectangle([x1, y1, x2, y2], fill=color)
|
| 43 |
+
|
| 44 |
+
# Dibujar piezas (usaremos emojis como texto)
|
| 45 |
+
piece_chars = {
|
| 46 |
+
'r': '♜', 'n': '♞', 'b': '♝', 'q': '♛', 'k': '♚', 'p': '♟',
|
| 47 |
+
'R': '♖', 'N': '♘', 'B': '♗', 'Q': '♕', 'K': '♔', 'P': '♙'
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
for square in chess.SQUARES:
|
| 51 |
+
piece = self.board.piece_at(square)
|
| 52 |
+
if piece:
|
| 53 |
+
col = chess.square_file(square)
|
| 54 |
+
row = 7 - chess.square_rank(square)
|
| 55 |
+
|
| 56 |
+
x = col * SQUARE_SIZE + SQUARE_SIZE // 2
|
| 57 |
+
y = row * SQUARE_SIZE + SQUARE_SIZE // 2
|
| 58 |
+
|
| 59 |
+
# Usar emoji para la pieza
|
| 60 |
+
piece_char = piece_chars[piece.symbol()]
|
| 61 |
+
draw.text((x-10, y-15), piece_char, fill='black', font_size=30)
|
| 62 |
+
|
| 63 |
+
return img
|
| 64 |
+
|
| 65 |
+
def get_move_suggestion(self):
|
| 66 |
+
"""Obtener sugerencia de Stockfish usando API"""
|
| 67 |
+
try:
|
| 68 |
+
# Usar API de stockfish.js como fallback
|
| 69 |
+
fen = self.board.fen()
|
| 70 |
+
url = f"https://stockfish.online/api/s/v2.php?fen={fen}&depth=10"
|
| 71 |
+
response = requests.get(url)
|
| 72 |
+
|
| 73 |
+
if response.status_code == 200:
|
| 74 |
+
data = response.json()
|
| 75 |
+
if 'bestmove' in data:
|
| 76 |
+
return data['bestmove']
|
| 77 |
+
except:
|
| 78 |
+
pass
|
| 79 |
+
|
| 80 |
+
# Fallback: elegir el primer movimiento legal
|
| 81 |
+
legal_moves = list(self.board.legal_moves)
|
| 82 |
+
return str(legal_moves[0]) if legal_moves else None
|
| 83 |
+
|
| 84 |
+
def make_move(self, move_uci):
|
| 85 |
+
"""Realizar movimiento"""
|
| 86 |
+
try:
|
| 87 |
+
move = chess.Move.from_uci(move_uci)
|
| 88 |
+
if move in self.board.legal_moves:
|
| 89 |
+
self.board.push(move)
|
| 90 |
+
self.player_turn = False
|
| 91 |
+
return True
|
| 92 |
+
except:
|
| 93 |
+
pass
|
| 94 |
+
return False
|
| 95 |
+
|
| 96 |
+
def ai_move(self):
|
| 97 |
+
"""Movimiento de la IA"""
|
| 98 |
+
if not self.player_turn and not self.board.is_game_over():
|
| 99 |
+
best_move = self.get_move_suggestion()
|
| 100 |
+
if best_move:
|
| 101 |
+
self.board.push(chess.Move.from_uci(best_move))
|
| 102 |
+
self.player_turn = True
|
| 103 |
+
|
| 104 |
+
def get_game_status(self):
|
| 105 |
+
"""Obtener estado del juego"""
|
| 106 |
+
if self.board.is_checkmate():
|
| 107 |
+
return "¡Jaque mate! " + ("Ganaste" if self.player_turn else "Stockfish gana")
|
| 108 |
+
elif self.board.is_stalemate():
|
| 109 |
+
return "Tablas por ahogado"
|
| 110 |
+
elif self.board.is_insufficient_material():
|
| 111 |
+
return "Tablas por material insuficiente"
|
| 112 |
+
elif self.board.is_check():
|
| 113 |
+
return "¡Jaque!"
|
| 114 |
+
else:
|
| 115 |
+
return "Tu turno" if self.player_turn else "Pensando..."
|
| 116 |
+
|
| 117 |
+
# Instancia global del juego
|
| 118 |
+
game = ChessGame()
|
| 119 |
+
|
| 120 |
+
def update_interface():
|
| 121 |
+
"""Actualizar la interfaz completa"""
|
| 122 |
+
board_img = game.create_board_image()
|
| 123 |
+
status = game.get_game_status()
|
| 124 |
+
|
| 125 |
+
# Generar lista de movimientos legales
|
| 126 |
+
legal_moves = [str(move) for move in game.board.legal_moves]
|
| 127 |
+
|
| 128 |
+
return board_img, status, "\n".join(legal_moves[:10]) # Mostrar primeros 10 movimientos
|
| 129 |
+
|
| 130 |
+
def handle_move(move_input):
|
| 131 |
+
"""Manejar movimiento del jugador"""
|
| 132 |
+
if game.player_turn and not game.board.is_game_over():
|
| 133 |
+
if game.make_move(move_input):
|
| 134 |
+
# Movimiento de la IA después de un breve delay
|
| 135 |
+
game.ai_move()
|
| 136 |
+
|
| 137 |
+
return update_interface()
|
| 138 |
+
|
| 139 |
+
def handle_suggestion():
|
| 140 |
+
"""Obtener sugerencia de movimiento"""
|
| 141 |
+
if game.player_turn:
|
| 142 |
+
suggestion = game.get_move_suggestion()
|
| 143 |
+
return suggestion if suggestion else "No hay sugerencias disponibles"
|
| 144 |
+
return "Es el turno de Stockfish"
|
| 145 |
+
|
| 146 |
+
def reset_game():
|
| 147 |
+
"""Reiniciar juego"""
|
| 148 |
+
global game
|
| 149 |
+
game = ChessGame()
|
| 150 |
+
return update_interface()
|
| 151 |
+
|
| 152 |
+
# Crear interfaz Gradio
|
| 153 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Ajedrez con Stockfish") as demo:
|
| 154 |
+
gr.Markdown("""
|
| 155 |
+
# ♟️ Ajedrez con Stockfish
|
| 156 |
+
|
| 157 |
+
Juega contra Stockfish, uno de los motores de ajedrez más fuertes del mundo.
|
| 158 |
+
""")
|
| 159 |
+
|
| 160 |
+
with gr.Row():
|
| 161 |
+
with gr.Column():
|
| 162 |
+
board_image = gr.Image(label="Tablero", interactive=False)
|
| 163 |
+
status_text = gr.Textbox(label="Estado del Juego", interactive=False)
|
| 164 |
+
|
| 165 |
+
with gr.Column():
|
| 166 |
+
move_input = gr.Textbox(
|
| 167 |
+
label="Ingresa tu movimiento (ej: e2e4)",
|
| 168 |
+
placeholder="Formato: casilla_origen casilla_destino"
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
with gr.Row():
|
| 172 |
+
submit_btn = gr.Button("Realizar Movimiento", variant="primary")
|
| 173 |
+
suggest_btn = gr.Button("Sugerencia")
|
| 174 |
+
reset_btn = gr.Button("Nuevo Juego")
|
| 175 |
+
|
| 176 |
+
suggestion_text = gr.Textbox(label="Sugerencia de Stockfish", interactive=False)
|
| 177 |
+
legal_moves_text = gr.Textbox(
|
| 178 |
+
label="Movimientos Legales (primeros 10)",
|
| 179 |
+
lines=10,
|
| 180 |
+
interactive=False
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
# Conexiones de eventos
|
| 184 |
+
submit_btn.click(
|
| 185 |
+
fn=handle_move,
|
| 186 |
+
inputs=move_input,
|
| 187 |
+
outputs=[board_image, status_text, legal_moves_text]
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
suggest_btn.click(
|
| 191 |
+
fn=handle_suggestion,
|
| 192 |
+
outputs=suggestion_text
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
reset_btn.click(
|
| 196 |
+
fn=reset_game,
|
| 197 |
+
outputs=[board_image, status_text, legal_moves_text]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# Inicializar interfaz
|
| 201 |
+
demo.load(
|
| 202 |
+
fn=update_interface,
|
| 203 |
+
outputs=[board_image, status_text, legal_moves_text]
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Para Hugging Face Spaces
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
chess>=1.10.0
|
| 3 |
+
pillow>=10.0.0
|
| 4 |
+
requests>=2.25.0
|