Welcome to the AI-Powered Chess Tutor, a web-based application designed to help you improve your chess skills by providing not just strong computer opponents, but also detailed, human-like explanations for the best moves in any given position.
This project combines the raw analytical power of the Stockfish chess engine with the advanced natural language capabilities of the Groq API (running LLaMA 3) to create a unique and effective learning experience.
The primary goal of this project is to bridge the gap between traditional chess engines and human understanding. While engines like Stockfish can tell you the best move, they don't explain why it's the best move in a way that's intuitive for learners. This AI Tutor solves that problem by:
- Analyzing the position with the Stockfish engine to find the optimal moves.
- Sending this data to the Groq API.
- Generating a rich, contextual explanation that covers the strategic and tactical reasons behind the best move, making complex chess concepts easier to grasp.
Whether you're a beginner learning the basics or an advanced player looking to refine your positional understanding, this tool provides personalized feedback to elevate your game.
- Play Against AI: Challenge the Stockfish engine with multiple difficulty levels, from beginner to ultimate.
- Real-time Analysis: See the top engine moves and the board evaluation (centipawn score) updated in real-time.
- AI-Generated Explanations: Click the "Get Explanation" button at any time to receive a detailed breakdown of the best move, its purpose, and how it compares to other options.
- Endgame Trainer: Use the
endgame.htmlpage to set up custom positions using FEN strings and practice specific endgame scenarios. - Interactive Interface: A clean and simple UI built with
chessboard.jsthat allows for easy move input, game resets, and move history tracking (PGN and FEN). - Undo Moves: Made a mistake? Easily take back your last move and try a different line.
- Custom Position Setup: Paste any FEN (Forsyth-Edwards Notation) string to set up a specific board position and play or analyze from that point. This is perfect for studying specific openings, analyzing your own games, or practicing tactics.
The application has a client-server architecture that leverages several key technologies:
index.html: The landing page where users select a difficulty level or navigate to other modes.game.html: The main game interface.chessboard.js: Renders the interactive chessboard.chess.js: Handles game logic, move validation, and PGN/FEN generation.stockfish.js: The powerful Stockfish engine compiled to WebAssembly, running directly in the browser to analyze positions and provide the best moves without server load.
endgame.html: A dedicated page for setting up and practicing endgame positions.styles.css: Provides the visual styling for the application.
server.js: A simple Node.js server using the Express framework.- It exposes a single API endpoint (
/groq-text). - When the user requests an explanation, the frontend sends the current game state (FEN, PGN, best moves from Stockfish) to this endpoint.
- The server then securely communicates with the Groq API, sending a carefully crafted prompt to the LLaMA 3 model.
- Groq's AI generates the textual explanation, which the server sends back to the frontend to be displayed to the user.
- It exposes a single API endpoint (
- The user plays a move on the board in
game.html. - The browser-based Stockfish engine analyzes the new position and displays the evaluation and top 3 moves.
- The user clicks "Get Explanation".
- A
fetchrequest is sent from the client to the Node.js server with the current FEN, PGN, and best moves. - The Node.js server forwards this information in a prompt to the Groq API.
- Groq's LLaMA 3 model processes the prompt and returns a detailed explanation.
- The server sends this explanation back to the client, where it's displayed to the user.
To run this project locally, follow these steps:
- Node.js installed on your machine.
- A Groq API key. You can get one for free from the Groq Console.
-
Clone the repository:
git clone <your-repository-url> cd <repository-directory>
-
Install backend dependencies:
npm install
This will install
express,groq-sdk, anddotenv. -
Set up your environment variables:
- Create a file named
.envin the root of the project. - Add your Groq API key to it:
GROQ_API_KEY=your_groq_api_key_here - Create a file named
-
Run the server:
node server.js
The server will start on port
2000. You should seeServer is running on port 2000in your console. -
Launch the application:
- Open the
index.htmlfile in your web browser. You can do this by right-clicking it and selecting "Open with Live Server" in VS Code or by simply double-clicking the file.
- Open the
You can now select a difficulty and start playing and learning!