Welcome to the Battleship Game! 🛳️ This is a console-based game written in Java. The goal is to sink all of your opponent's ships before they sink yours. Guess the locations of the hidden ships, and may the best player win! 🎯
- The game is played on a 7x12 grid 📊.
- Each player gets 3 ships 🚢 randomly placed on the grid.
- Take turns guessing the locations of your opponent’s ships using coordinates.
- The first player to sink all the ships is the winner! 🏆
- Ship Placement: Ships are automatically placed on the grid for both players 🗺️.
- Turns: Players alternate turns guessing ship locations by entering coordinates (e.g.,
5,4
) 📍. - Hit/Miss Feedback:
- A hit is marked with
"="
💥. - A miss is marked with
"♒︎"
🌊.
- A hit is marked with
- Winning: Sink all your opponent's ships to win! 🏁
🎮 Game Rules
Objective: Be the first player to score 3 points by discovering your opponent's ships 💥 and orbs 💖.
- Gameplay:
- Players take turns guessing grid positions by entering row and column numbers.
- Hit a Ship: Gain 2 points.
- Hit a Bomb: Lose 1 point.
- Collect an Orb: Gain 1 point.
- Miss: Opponent gets 1 point.
- Winning: The first player to reach 3 points wins the round.
✨ Features
- Random Ship Placement: Each player’s grid is populated with hidden ships 🛳️ of random lengths.
- Special Elements:
- Orbs 💖: Bonus items that give extra points if found.
- Bombs 💥: Hazardous items that decrease your score.
- Interactive Grids: The game is played on a 9x14 grid, providing plenty of space for strategic gameplay.
- Dynamic Scoring: Points are calculated based on the outcomes of the player’s guesses.
- 🎲 Random Ship Placement: Ships are randomly positioned horizontally, so no two games are the same!
- 👨🚀 Turn-based Play: Players take turns guessing ship locations.
- 💥 Hit and Miss Indicators: Successful hits are shown as
"="
, while misses are marked as"♒︎"
. - 🧹 Clear Terminal: The screen is cleared between each turn to maintain suspense (works on most systems).
- ✅ Input Validation: Ensures player inputs are valid within the grid dimensions.
- Java 8+ is required to run the game ☕.
- Works on Windows, macOS, and Linux.
First, clone the repository to your local machine:
git clone https://github.com/yourusername/battleship-game-java.git
Navigate to the project directory and compile the source files:
javac Project/Battleship.java
Run the game with the following command:
java Project.Battleship
-
Game Start:
- Ships are automatically placed for both players 🎲.
-
Input Format:
- Enter the row and column in the format
row,column
(e.g.,5,4
) 📝. - Rows range from 1 to 7, and columns range from 1 to 12.
- Enter the row and column in the format
-
Turn-based Gameplay:
- Players take turns guessing, and the board updates after each guess 🎯.
- You’ll see hits (
=
) and misses (♒︎
) after each turn.
-
Winning:
- The game ends when one player sinks all of their opponent’s ships 💥.
To change the grid size, modify the array dimensions in the code. The current size is 7x12 🗺️.
You can adjust the number of ships by changing the value in the generateArray()
method (default is 3 ships) 🚢.
The current version only supports horizontal ship placements. You can modify the ship generation logic to support vertical or diagonal placements as well 🛠️.
=============================================
♒︎ ♒︎ ♒︎ ♒︎ 1 1 1 1 1 ♒︎ ♒︎ ♒︎
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
♒︎ ♒︎ ♒︎ 1 1 1 1 1 1 1 1 ♒︎
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ 1 1 1 1 1 1
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
=============================================
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
♒︎ ♒︎ ♒︎ ♒︎ 2 2 2 2 2 2 2 2
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ 2 2 2
♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
2 2 2 2 2 2 2 ♒︎ ♒︎ ♒︎ ♒︎ ♒︎
=============================================
Player 1: Enter row and column number: 1,7
Player 2 found a ship!
Player 2: Enter row and column number: 1,5
No ship found...
ℹ️ Ship numbers are shown for demonstration purposes.
- AI Opponent: Add a single-player mode where you can play against the computer 🤖. Gameplay features: Adding multiple obstacles, home screen etc 🕹️ ...
- Vertical and Diagonal Ships: Implement vertical and diagonal ship placements 🚢.
- GUI Version: Use JavaFX or Swing to build a graphical interface 🖼️.
- Save and Load: Implement save/load functionality so players can pause and resume games 💾.
Feel free to fork this project, add features, or fix bugs! Contributions are welcome 🛠️.
This project is licensed under the MIT License. You are free to use, modify, and distribute the game 🔓.
Have fun and enjoy playing Battleship! 🎮