๐ Snake vs AI
A modern, browser-based Snake game where you compete against an intelligent AI opponent. Built with vanilla JavaScript, HTML5 Canvas, and CSS3.
๐ฎ Play Now
Simply open index.html in any modern web browser โ no installation required!
โจ Features
- AI Opponent: Compete against a pathfinding AI that adapts to different difficulty levels
- 4 Difficulty Modes:
- ๐ข Easy โ Casual AI with 30% random moves
- ๐ก Medium โ Smart AI with 15% random moves
- ๐ด Hard โ Expert AI with 5% random moves
- โซ Impossible โ Perfect AI, always optimal moves
- Wrap-Around Edges: Snakes teleport to the opposite side when hitting walls
- Real-Time Stats: Track game time, scores, snake lengths, and food eaten
- Responsive Design: Works on desktop and mobile devices
- Touch Controls: On-screen D-pad for mobile play
- Clean UI: Modern dark theme with smooth animations
๐น๏ธ Controls
| Key | Action |
|---|
โ W | Move Up |
โ S | Move Down |
โ A | Move Left |
โ D | Move Right |
Space | Pause/Resume |
R | Restart Game |
๐ง How the AI Works
The AI uses a scoring system to evaluate each possible move:
- Safety Check: Avoids collisions with itself and the player
- Food Seeking: Calculates Manhattan distance to food
- Space Analysis: Uses flood-fill to estimate available movement space
- Player Avoidance: On harder difficulties, keeps distance from the player's head
The difficulty level determines how often the AI makes optimal vs random decisions.
๐ Win Conditions
- You Win: AI crashes into itself or you have a higher score on collision
- AI Wins: You crash into yourself or AI has a higher score on collision
- Tie: Both snakes crash simultaneously with equal scores
๐ ๏ธ Tech Stack
- HTML5 Canvas for rendering
- Vanilla JavaScript (ES6+)
- CSS3 with CSS Variables
- Font Awesome icons
- No external dependencies
๐ Project Structure
snake-vs-ai/
โโโ index.html # Complete game (single file)
โโโ README.md # This file
๐ Getting Started
-
Clone the repository:
git clone https://github.com/yourusername/snake-vs-ai.git
-
Open index.html in your browser
That's it! No build process or dependencies needed.
๐ฑ Mobile Support
The game automatically detects mobile devices and displays touch controls. The canvas scales responsively to fit smaller screens.
๐จ Customization
Colors and styling can be easily modified through CSS variables in the :root selector:
:root {
--player-color: #4ade80; /* Green */
--ai-color: #f59e0b; /* Orange */
--food-color: #ef4444; /* Red */
}
๐ License
MIT License โ feel free to use, modify, and distribute.
๐ค Contributing
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
Built by TheGhostPacket | Part of the ShieldStack Technologies portfolio