Battleship game in 150 lines of C++ šŸš¢

Adamay MannAdamay Mann
3 min read

āš“ Why Battleship?

Battleship is the perfect blend of strategy and nostalgia. You donā€™t need fancy graphicsā€”just grids, logic, and a little suspense. Itā€™s also a goldmine for learning programming:

  • Grid management

  • Input validation

  • Game state tracking

  • Turn-based logic

Perfect for sharpening your C++ skills without getting lost in rendering engines or physics simulations.


šŸŒŸ Why C++?

ā€œWhy not Python/JavaScript?ā€ Because C++ forces you to:

  1. Think about memory: No garbage collection here!

  2. Master arrays: The backbone of the 10x10 grid.

  3. Practice OOP: Structs for players/ships keep things clean.

  4. Embrace simplicity: No frameworksā€”just raw terminal I/O.

Itā€™s like building a ship in a bottle: constrained, rewarding, and old-school cool.


šŸ› ļø What We Built

A two-player terminal game where you:

  1. Place 5 ships (Carrier, Battleship, etc.) on a hidden grid.

  2. Take turns firing torpedoes (typing coordinates like ā€œA7ā€).

  3. Track hits (X), misses (O), and sink all enemy ships to win.

Features:

  • ASCII ā€œgraphicsā€ (itā€™s terminal-friendly!)

  • Input validation (no crashing on Z42)

  • Hidden enemy ships (no peeking!)

  • Game-over detection


šŸ” How It Works

1. The Boards

Each player has two 10x10 grids:

  • shipBoard: Where their ships live (S = ship, X = hit).

  • attackBoard: Tracks attacks on the enemy (X/O).

2. Ship Placement

Players place ships horizontally or vertically with commands like:

Place Aircraft Carrier (Length: 5)  
Enter start coordinate: C3  
Horizontal (H) or Vertical (V)? V

The code checks for collisions/out-of-bounds!

3. The Game Loop

WHILE no oneā€™s ships are sunk:  
   Player 1ā€™s turn:  
      - See attack board  
      - Enter coordinate (e.g., "F5")  
      - Hit/miss? Update boards  
   REPEAT for Player 2

šŸ’» The Code Breakdown

I have tried to explain key snippets. You can access the complete code here and try it out on your own

1. Structs for Clarity ā€” No classes, no inheritanceā€”just simple data containers*.*

No classes, no inheritanceā€”just simple data containers.

2. ASCII Grid Magic ā€” Hides ships on the enemyā€™s board with a simple showShips flag.

3. Input Parsing ā€” Converts B7 to row=6, col=1ā€”no regex, no fuss.


šŸ§  Key Lessons Learned

  1. OOP Lite: Structs > classes for small projects.

  2. Validation Matters: Assume users will type šŸ¬ instead of H.

  3. Game Loops Rule: while(true) is your friend.

  4. Terminal = Canvas: You donā€™t need OpenGL to have fun.


šŸš€ How to Run

  1. Compile:
$ g++ battleship.cpp -o battleship
  1. Play:
$ ./battleship

(Tested on Linux/macOS. Windows? Use WSL or Cygwin.)


šŸ”— Why You Should Build This

  • Learn C++ Fundamentals: Arrays, structs, I/O.

  • Practice Debugging: ā€œWhy isnā€™t my ship showing? Oh, showShips is false.ā€

  • Flex Creativity: Add features like save/load, AI, or ANSI colors.


šŸ Final Thoughts

Is this the prettiest Battleship game? Nope.
Is it a fun weekend project that teaches core programming? Absolutely.

Mod it! Break it! Improve it!
The full code is hereā€”150 lines of pure C++ joy.

Now go forth and sink some virtual ships. šŸŽÆ

0
Subscribe to my newsletter

Read articles from Adamay Mann directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Adamay Mann
Adamay Mann