|
from flask import Flask, render_template, request, jsonify, session |
|
from stockfish import Stockfish |
|
import chess |
|
|
|
app = Flask(__name__) |
|
app.secret_key = 'super_secret_key_for_session' |
|
|
|
|
|
|
|
try: |
|
stockfish_path = "/usr/games/stockfish" |
|
stockfish = Stockfish(path=stockfish_path, parameters={"Threads": 2, "Hash": 128}) |
|
except Exception as e: |
|
print(f"Erreur à l'initialisation de Stockfish: {e}") |
|
print("Veuillez vérifier que Stockfish est installé et accessible via le PATH, ou spécifiez le chemin correct.") |
|
|
|
stockfish = None |
|
|
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('index.html') |
|
|
|
@app.route('/new_game', methods=['POST']) |
|
def new_game(): |
|
if not stockfish: |
|
return jsonify({"error": "Stockfish non initialisé"}), 500 |
|
|
|
data = request.json |
|
mode = data.get('mode', 'human') |
|
|
|
initial_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" |
|
session['fen'] = initial_fen |
|
session['mode'] = mode |
|
session['turn'] = 'w' |
|
session['history'] = [] |
|
|
|
stockfish.set_fen_position(initial_fen) |
|
|
|
|
|
session['board_pgn'] = chess.Board().fen() |
|
|
|
return jsonify({ |
|
"fen": initial_fen, |
|
"turn": session['turn'], |
|
"message": "Nouvelle partie commencée." |
|
}) |
|
|
|
@app.route('/move', methods=['POST']) |
|
def handle_move(): |
|
if not stockfish: |
|
return jsonify({"error": "Stockfish non initialisé"}), 500 |
|
|
|
if 'fen' not in session: |
|
return jsonify({"error": "Aucune partie en cours. Commencez une nouvelle partie."}), 400 |
|
|
|
data = request.json |
|
move_uci = data.get('move') |
|
|
|
|
|
board = chess.Board(session['fen']) |
|
|
|
|
|
current_player_color = 'w' if board.turn == chess.WHITE else 'b' |
|
if session['turn'] != current_player_color: |
|
return jsonify({"error": "Pas votre tour.", "fen": session['fen'], "turn": session['turn']}), 400 |
|
|
|
try: |
|
move_obj = board.parse_uci(move_uci) |
|
except ValueError: |
|
return jsonify({"error": "Format de coup invalide.", "fen": session['fen'], "turn": session['turn']}), 400 |
|
|
|
|
|
if not stockfish.is_move_correct(move_uci) or move_obj not in board.legal_moves: |
|
return jsonify({"error": "Coup illégal.", "fen": session['fen'], "turn": session['turn']}), 400 |
|
|
|
|
|
stockfish.make_moves_from_current_position([move_uci]) |
|
board.push(move_obj) |
|
session['fen'] = stockfish.get_fen_position() |
|
session['history'].append(move_uci) |
|
session['turn'] = 'b' if current_player_color == 'w' else 'w' |
|
|
|
ai_move_uci = None |
|
game_status = "En cours" |
|
|
|
if board.is_checkmate(): |
|
game_status = f"Mat! {'Les Blancs' if board.turn == chess.BLACK else 'Les Noirs'} gagnent." |
|
elif board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition(): |
|
game_status = "Pat!" |
|
|
|
|
|
if session['mode'] == 'ai' and game_status == "En cours" and ( (board.turn == chess.BLACK and current_player_color == 'w') or \ |
|
(board.turn == chess.WHITE and current_player_color == 'b') ) : |
|
|
|
stockfish.set_fen_position(board.fen()) |
|
ai_move_uci = stockfish.get_best_move_time(1000) |
|
if ai_move_uci: |
|
ai_move_obj = board.parse_uci(ai_move_uci) |
|
stockfish.make_moves_from_current_position([ai_move_uci]) |
|
board.push(ai_move_obj) |
|
session['fen'] = stockfish.get_fen_position() |
|
session['history'].append(ai_move_uci) |
|
session['turn'] = 'w' if session['turn'] == 'b' else 'b' |
|
|
|
if board.is_checkmate(): |
|
game_status = f"Mat! {'Les Blancs' if board.turn == chess.BLACK else 'Les Noirs'} gagnent." |
|
elif board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition(): |
|
game_status = "Pat!" |
|
else: |
|
if board.is_checkmate(): |
|
game_status = f"Mat! {'Les Blancs' if board.turn == chess.BLACK else 'Les Noirs'} gagnent." |
|
elif board.is_stalemate(): |
|
game_status = "Pat!" |
|
|
|
|
|
return jsonify({ |
|
"fen": session['fen'], |
|
"turn": session['turn'], |
|
"last_move_human": move_uci, |
|
"last_move_ai": ai_move_uci, |
|
"game_status": game_status, |
|
"history": session['history'] |
|
}) |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |