Skip to content

A chess AI project created using basically no real knowledge of how chess works above the basics

Notifications You must be signed in to change notification settings

Winnrman/chess_ai

Repository files navigation

chess_ai

Project Overview

This is a passion project built because everyone thinks a chess AI is cool. The goal was to create a simple but functional chess AI you can play in your browser, with a focus on clarity and learnability of the code, rather than making the strongest chess engine possible. The project is a work in progress and open to improvements!

How It Works

  • Frontend: The main interface is a web page (index.html) with a chessboard (rendered on a <canvas>) and a sidebar that shows the AI's thought process and the game history.
  • Chess Logic: All chess logic (legal move generation, move execution, AI move selection) is implemented in JavaScript, directly in the HTML file.
  • AI: The AI uses a basic approach, sometimes picking a random legal move, and sometimes using a simple minimax search with evaluation heuristics and an opening book.
  • Visuals: Chess pieces are rendered using images, loaded dynamically via JavaScript.
  • History: The sidebar tracks all moves and displays how the AI evaluated positions.

File Breakdown

index.html

  • Purpose: Main entry point and the only file you need to open to play. Contains all the JavaScript for the UI, chess logic, and AI.
  • Key Functions:
    • Board rendering and user interaction.
    • Move history and AI thought display.
    • Functions for generating legal moves, executing moves, and handling special rules (promotion, castling, en passant).
    • AI move logic: integrates a basic opening book, random moves, and a simple minimax search.
  • What could be improved:
    • Split logic into multiple JS modules/files for maintainability.
    • Use a modern framework (React, Vue) to manage UI for better scalability.
    • Add support for more advanced chess rules (threefold repetition, fifty-move rule).
    • Improve the AI's evaluation and search depth for stronger play.
    • Add tests for core chess logic.

(old)main.js

  • Purpose: Previous standalone JavaScript implementation of the chess logic and AI, for experimentation or reference.
  • Key Functions:
    • Board representation and basic move generation.
    • Simple AI selecting random legal moves.
    • Console-based interaction and debug functions like printBoard().
  • What could be improved:
    • Remove or archive if no longer needed to avoid confusion.
    • Refactor for clarity and modularity if planning future CLI or Node.js support.

previousVersionWithJSONData.txt

  • Purpose: Archive of an earlier version, including the HTML, CSS, and a JS block with an opening book in JSON format.
  • Key Contents:
    • Example of how an opening book can be represented in JSON for the AI to use.
    • Historical reference for UI and logic structure.
  • What could be improved:
    • Move the JSON opening book to a dedicated file for easy updating and reuse.
    • Consider deleting or archiving outside the main repo if it's just for personal reference.

Notes & Future Improvements

  • Why this project?
    This was built for fun and learning—because writing a chess AI is an iconic programming challenge, and it's a cool way to explore algorithms, UI, and game logic all in one project.
  • Potential next steps:
    • Refactor codebase to separate UI, game logic, and AI into modules.
    • Add a difficulty slider to tune AI strength.
    • Support for multiplayer or online play.
    • Animate moves and add sound effects.
    • Make the UI mobile-friendly.
    • Add a PGN export for completed games.

Feel free to fork, play, and contribute! Every programmer should write a chess AI at least once—it's a rite of passage!

About

A chess AI project created using basically no real knowledge of how chess works above the basics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published