Skip to content
/ snake Public

🐍 A simple console-based Snake Game written in Java, supporting Maven and Docker. Play in the terminal with adjustable board size!

Notifications You must be signed in to change notification settings

unvercan/snake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game

Description

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.

Features

  • 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

Technologies Used

  • Java (17 or later)
  • Maven (if building the project manually)
  • Docker (if running inside a container)

How to Play

  1. Run the application.
  2. Control the snake using the following keys:
    • W β†’ Move up
    • A β†’ Move left
    • S β†’ Move down
    • D β†’ Move right
  3. Eat food (*) to grow longer.
  4. Avoid crashing into walls (#) or yourself.
  5. The game ends when:
    • The snake collides with a wall or itself (lose condition).
    • The snake reaches the maximum possible length (win condition).

Installation and Setup

Prerequisites

  • Java 17 or later
  • Maven (if building the project manually)
  • Docker (if running inside a container)

Running the Game (Java and Maven)

  1. Clone the repository:
    git clone https://github.com/unvercan/snake.git
    cd snake
  2. Build the project using Maven:
    mvn clean package
    • This command will clean any previous builds and package the project into a JAR file.
  3. 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.

Running the Game (Docker)

  1. Build the Docker Image:
    docker build -t snake .
  2. 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!

Project Structure

β”œβ”€β”€ 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

License

This project is open-source and available under the MIT License.

Author

Ünver Can Ünlü

About

🐍 A simple console-based Snake Game written in Java, supporting Maven and Docker. Play in the terminal with adjustable board size!

Topics

Resources

Stars

Watchers

Forks