A modern chess application built with Next.js and TypeScript featuring real-time gameplay, timed matches, and per-user statistics (wins, losses, draws, and dynamic ELO ratings) powered by Redis. Authentication is handled by Clerk for secure, multi-device access. The chess AI utilizes a minimax algorithm with alpha-beta pruning to determine optimal moves.
- Real-Time Chess Gameplay: Play chess against an AI powered by minimax algorithm with move-by-move updates.
- Timed Matches: Choose between different time controls (3, 5, or 10 minutes).
- Per-User Statistics: Track games played, wins, losses, draws/stalemates, and dynamic ELO ratings.
- Authentication: Secure login using Clerk.
- Backend Integration: Persistent storage for user stats via Redis.
- Modern UI: Responsive and visually appealing design using Tailwind CSS.
- Next.js: React framework for server-side rendering and API routes.
- TypeScript: Type-safe JavaScript for robust, scalable code.
- Redis: Fast, in-memory data store for persisting per-user statistics.
- Clerk Auth: Comprehensive user authentication and management.
Check out the live deployment of this application: RonyChess
The application is hosted on Vercel and includes all features from the local version, including authentication via Clerk and persistent statistics storage with Redis.
git clone https://github.com/RonyBubnovsky/Chess-Next.js.git
cd Chess-Next.jsInstall the required packages:
npm installThis project uses:
- next
- react and react-dom
- typescript
- redis
- @clerk/nextjs
- Tailwind CSS
Create a .env.local file in the root of your project with the following variables:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=
CLERK_SECRET_KEY=
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_HOST=
REDIS_PORT=
- NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY & CLERK_SECRET_KEY:
- Sign up or log in to Clerk.dev.
- Create a new application in the Clerk dashboard.
- In the application settings, locate the Publishable Key and Secret Key.
- Copy and paste them into your
.env.localfile.
- REDIS_USERNAME, REDIS_PASSWORD, REDIS_HOST, REDIS_PORT:
- Sign up or log in to Redis Cloud.
- Create a new Redis database instance.
- In the database dashboard, you will find the connection details (username, password, host, and port).
- Copy these values into your
.env.localfile.
Start the development server:
npm run devOpen http://localhost:3000 in your browser to view the application.
Users must log in via Clerk before accessing the chess page.
- Choose your color (or randomize).
- Select a time control (3, 5, or 10 minutes).
- Play chess against a basic AI.
- Moves are tracked, and the game ends with a win, loss, or draw.
- When the game ends, user stats (games played, wins/losses/draws, and ELO) are updated in Redis via the API.
Before starting a game, your current statistics are displayed in a modern stats box.
Use the provided "Home" button to navigate back to the main landing page.
-
Move Generation and Decision Making:
- Utilizes a minimax algorithm enhanced with alpha-beta pruning to efficiently evaluate potential moves.
- Searches the game tree recursively to find optimal moves based on scoring positions.
-
Algorithm Features:
- Positional Evaluation: Assigns values to chess pieces not only by their standard worth (pawn, knight, bishop, rook, queen) but also by their position on the board, encouraging strategic gameplay.
- Depth Configuration: The search depth can be customized within the codebase, enabling control over AI difficulty by determining how many moves ahead the algorithm evaluates.
-
Performance Optimization:
- Alpha-Beta Pruning: Optimizes the move selection by pruning branches in the game tree that don't affect the final outcome, significantly improving performance.
-
Adjustability:
- Flexible difficulty adjustment through code-based depth settings to cater to varying player skill levels.
The AI evaluates board positions based on:
- Material advantage
- Piece positioning
- Control of the center
- Pawn structure
- King safety
This implementation provides a challenging opponent while maintaining reasonable performance for a web application.
This project is licensed under the MIT License.
Contributions are welcome! If you'd like to contribute to this project, please feel free to submit a pull request or open an issue with your ideas and suggestions.
Potential areas for contribution include:
- Multiplayer functionality
- Advanced AI improvements
- Additional game modes
- UI/UX enhancements
- Performance optimizations
Please ensure your code follows the project's coding standards and includes appropriate tests where applicable.