https://github.com/arsh-jafri/dijkstra-snake-game
A modern take on the classic Snake game featuring an AI opponent that uses Dijkstra's algorithm for pathfinding. Customize snake colors and game speed while competing against an intelligent AI snake.
https://github.com/arsh-jafri/dijkstra-snake-game
dijkstra-algorithm java java-awt java-swing
Last synced: over 1 year ago
JSON representation
A modern take on the classic Snake game featuring an AI opponent that uses Dijkstra's algorithm for pathfinding. Customize snake colors and game speed while competing against an intelligent AI snake.
- Host: GitHub
- URL: https://github.com/arsh-jafri/dijkstra-snake-game
- Owner: Arsh-Jafri
- Created: 2025-02-27T17:20:23.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-27T18:25:15.000Z (over 1 year ago)
- Last Synced: 2025-02-28T01:59:58.856Z (over 1 year ago)
- Topics: dijkstra-algorithm, java, java-awt, java-swing
- Language: Java
- Homepage:
- Size: 38.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dijkstra's Snake Game
A Java implementation of the classic Snake game with an AI opponent that uses Dijkstra's algorithm for pathfinding.
## Features
- Player-controlled snake (blue by default) using arrow keys
- AI snake (red by default) that automatically finds the shortest path to food using Dijkstra's algorithm
- Customizable snake colors through color picker
- Adjustable game speed (Slow, Normal, Fast, Very Fast)
- 3-second countdown before game starts
- Grid-based game board with randomly spawning food
- Collision detection for walls, self, and other snake
- Game over screen with restart option
- Pause/Resume functionality
- Both snakes start with 4 blocks length
## Controls
- Arrow keys: Control the player snake's direction
- Space: Restart the game when game over
- P: Pause/Resume the game
- Restart button: Start a new game at any time
- Pause/Resume button: Toggle game pause state
## How to Run
1. Make sure you have Java Development Kit (JDK) installed on your system
2. Compile the Java files:
```bash
javac *.java
```
3. Run the game:
```bash
java Game
```
## Game Rules
- Control your snake (blue by default) to eat the food (red squares)
- Avoid colliding with:
- Walls
- Your own body
- The AI snake (red by default)
- Each time either snake eats food, it grows longer
- The AI snake will automatically find the shortest path to the food using Dijkstra's algorithm
- Game ends if the player snake collides with walls, itself, or the AI snake
## Game Flow
1. Start Screen:
- Choose your snake's color (default: blue)
- Choose AI snake's color (default: red)
- Select game speed (default: Normal)
- Click "Start Game" to begin
2. Countdown Phase:
- 3-second countdown before game starts
- Snakes are visible but don't move during countdown
- Both snakes start with 4 blocks length
3. Gameplay:
- Move your snake with arrow keys
- Collect food to grow longer
- Avoid collisions
- Pause anytime with 'P' key or Pause button
- Restart anytime with Restart button
## Implementation Details
- Built using Java Swing for graphics rendering
- Uses AWT for keyboard input handling
- Implements Dijkstra's algorithm with a priority queue for efficient pathfinding
- The AI snake uses an adjacency list representation for the game grid
- Custom countdown timer for game start
- Separate timers for game logic and countdown
- Responsive controls and smooth animation