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ΓΌ