This is a simple console-based Snake game written in Java. The player controls a snake, moving it around the board to consume food and grow in length. The objective is to achieve the maximum snake length without colliding with the walls or itself.
- Classic Snake game mechanics
- Player-controlled movement using
W
,A
,S
,D
keys - Food generation at random locations
- Collision detection (walls and self)
- Win condition when the snake reaches the maximum possible length
- Console-based interface with real-time updates
- Adjustable board size
- Java (17 or later)
- Maven (if building the project manually)
- Docker (if running inside a container)
- Run the application.
- Control the snake using the following keys:
W
→ Move upA
→ Move leftS
→ Move downD
→ Move right
- Eat food (
*
) to grow longer. - Avoid crashing into walls (
#
) or yourself. - The game ends when:
- The snake collides with a wall or itself (lose condition).
- The snake reaches the maximum possible length (win condition).
- Java 17 or later
- Maven (if building the project manually)
- Docker (if running inside a container)
- Clone the repository:
git clone https://github.com/unvercan/snake.git cd snake
- Build the project using Maven:
mvn clean package
- This command will clean any previous builds and package the project into a JAR file.
- Run the game using the JAR file:
java -jar target/snake-1.0.jar boardWidth=15 boardHeight=8
boardWidth
: Defines the board width.boardHeight
: Defines the board height.
- Build the Docker Image:
docker build -t snake .
- Run the Docker Container:
docker run --rm -it -e BOARD_WIDTH=15 -e BOARD_HEIGHT=8 snake
-t
adds the name of the image while building the Docker file.--rm
ensures the container is removed after it stops.-it
keeps the container interactive for user input.-e BOARD_WIDTH=15
: Sets the board width inside the container.-e BOARD_HEIGHT=8
: Sets the board height inside the container.
Now you can play the Snake game directly in the terminal inside the Docker container!
├── src
│ ├── tr.unvercanunlu.snake # Main package
│ ├── App.java # Main Application entry point
│ ├── Game.java # Game logic
│ ├── util # Package for Utility classes
│ ├── ConsoleUtil.java # Console related Utility class
│ ├── InputUtil.java # Input related Utility class
│ ├── NumberUtil.java # Number related Utility class
│ ├── constant # Package for Enums
│ ├── Tile.java # Enum for board tiles
│ ├── Status.java # Enum for game statuses
│ ├── Move.java # Enum for movement directions
│ ├── Input.java # Enum for player inputs
│ ├── Action.java # Enum for possible actions
├── pom.xml # Maven configuration file
├── Dockerfile # Docker setup
This project is open-source and available under the MIT License.
Ünver Can Ünlü