Battleship game in 150 lines of C++ š¢


ā 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:
Think about memory: No garbage collection here!
Master arrays: The backbone of the 10x10 grid.
Practice OOP: Structs for players/ships keep things clean.
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:
Place 5 ships (Carrier, Battleship, etc.) on a hidden grid.
Take turns firing torpedoes (typing coordinates like āA7ā).
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*.*
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
OOP Lite: Structs > classes for small projects.
Validation Matters: Assume users will type
š¬
instead ofH
.Game Loops Rule:
while(true)
is your friend.Terminal = Canvas: You donāt need OpenGL to have fun.
š How to Run
- Compile:
$ g++ battleship.cpp -o battleship
- 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. šÆ
Subscribe to my newsletter
Read articles from Adamay Mann directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
