How virgs/2048-ai works
Project Type and Purpose
• This is a front-end oriented game project that combines an interactive UI (built with React and TypeScript) with a game engine and an artificial intelligence (AI) agent.
• The AI uses a variation of Pure Monte Carlo Tree Search (MCTS) to make decisions for playing the 2048 game.
• The project also involves performance reporting and uses modern tooling (Vite, TypeScript) and component-based UI design.
File Structure
• Top-level directories include:
– “src”: Contains the application code (React components, AI modules, game engine logic, and assets).
– “docs”: Contains support files like WebWorker scripts that might be used for offloading computation-intensive tasks (e.g., running Monte Carlo simulations in a separate thread).
– “public”: Likely holds static assets and the HTML template for hosting the React app (e.g., index.html).
– “reports”: Contains JSON and JS files that aggregate data/results from the AI runs, indicating that performance/analytics is a key aspect.
• Within “src”:
– “components” holds the UI pieces (board, grid, score, game modals, etc.).
– “engine” holds the core game logic (board representation, movements, game rules, etc.).
– “ai” contains files dedicated to AI’s Monte Carlo simulation logic (e.g., MonteCarlo.ts, NextMoveAiWorker.ts, ReportGenerator.ts) and support code for using Web Workers.
README for Additional Insights
• The README gives a good overview of the game’s rules, the overall goal, and details on how the AI works using a Pure Monte Carlo Tree Search approach.
• It describes the simulation-based decision making (i.e., playing multiple simulated games to decide the best move) and even touches on performance metrics.
• The README also emphasizes the learning objectives for the developer (React, HTML/CSS) and provides details on how to run, build, and report on the application.
• Components:
– UI Components (Board, Tile, Grid, Score, Modals)
– Game Engine components (Board, BoardMover, Direction, etc.)
– AI components (Monte Carlo Simulation, NextMoveAiWorker, WebWorker integration, ReportGenerator)
– Reporting Module (to track win ratios and move performance).
• Relationships and Interactions:
– Connections between the AI engine and Web Workers should be explicitly drawn.
• Visual Distinctions:
– Use clear labels, such as “Controller”, “Game Engine”, “AI Simulation”, “UI”, etc.
– A “Presentation Layer” block hosting all React UI components.
– A “Game Engine” block directly below that.
– An “AI Agent” block alongside or beneath the engine showing Monte Carlo simulation, with an auxiliary “Web Worker” sub-component if applicable.
– Optionally, a “Reporting & Analytics” block demonstrating flow of simulation data from AI to performance reports.
– Client actions (user inputs) trigger state updates in the Game Engine.
– The Game Engine communicates with the AI Agent for move decisions.
– The AI Agent utilizes Web Workers to perform simulations asynchronously.
– The results loop back to update the Game Engine and hence the UI.
This is the core loop for every move in the game.