Prepare to embark on a coding adventure across the digital seas in this epic 5-day mission: Building Battleship in Python! Whether you're a programming private or a Python pirate, you’ll be tasked with creating your very own version of the classic board game — right in the terminal.
This project is your first step toward designing a fully interactive version in Pygame. But for now, it’s all about mastering the fundamentals of Python: loops, lists, strings, and a splash of strategy. So grab your keyboards, steady your aim, and get ready to sink some ships — code-style.
Welcome to Day 1 of your Battleship build, where we dive headfirst into the thrilling waters of tactical code warfare! 🫡
In today’s mission, you’re not just writing code—you’re charting the open sea. Your job? Create the battlefield: a 10×10 grid where the fate of tiny invisible ships will be decided by well-placed guesses and glorious Python logic.
But here’s the catch — the ships are sneaky. They’re lurking beneath the waves (and by “waves,” we mean squiggly tildes ~) and you won’t be placing them yourself. The computer will randomly hide them, and it’s up to you to uncover them using input, logic, and a splash of luck.
🧱 Build and display a 10x10 game board with proper labels (A–J, 0–9). <-- TODAY'S TASK
⚙️ Randomly place 3 hidden ships on the board (1 cell each) and then display the board <-- TODAY'S TASK
💥 Mark hits with an X, misses with an O, and show the updated board after every guess.
Welcome to the Terminal Battlefield.
🖥️ No GUI. No mercy. Just grit, grids, and glorious code.
You need to create the game grid. This grid should have:
10 rows, numbered 0 to 9.
10 columns, labeled A through J.
Symbols like below to represent:
Water (~)
A hit (X)
A miss (O)
Possibly a ship (hidden or revealed)
🎯 Objective:
We need to create a game board in Python that’s shaped like a grid — 10 rows and 10 columns — just like the classic Battleship game. Each square in the grid will start with a water symbol ~.
We’ll store this board in Python as a 2D list, which means a list that contains other lists — one for each row.
This is essentially what it looks like, but DON'T HARD CODE THIS! USE LOOPS!!!
Create a list called board. Start by making a blank list.
Then, use a loop to add 10 rows.
Each row should also be a list, and each of those rows should hold 10 tildes (~).
HINT: NESTED FOR LOOP!!!!
Start With An Empty List
Add 10 Rows
To Each Row, Add 10 Columns
Once the board is created, we need to print it out
To label the columns from A to J, we use chr() — a Python function that turns numbers into letters using ASCII codes. (See the super hint below)
You could also Pre-Generate These and Just print them!! Whatever works for you!!!
COLUMN HEADER SUPER HINT!!!
This code snippit will print A through J!!!
Now, go through each row and:
Print the row number (0 to 9)
Print each cell value from that row
LINING THESE UP PROPERLY IS THE HARDEST PART!!! Don't FORGET ljust, rjust or center!!!
In the Battleship game, the enemy doesn’t tell you where their ships are — that would be cheating! So today, we’ll simulate the hidden ships using Python. Your job is to randomly generate 3 secret ship locations on the board. They won’t be shown to the player — but they’ll be tracked by the computer, and checked later when the player guesses.
These ships will each take up only one square (for now), and no two ships can be placed in the same location.
Use Python’s random module to generate random coordinates.
Store each ship’s position as a (row, column) pair.
Use a list to keep track of all ship positions.
Make sure no duplicates are added (each ship must be on a unique square).
Create an Empty Ship List
Use a loop to place 3 ships:
Pick a Random Row/Column for each ship
Create a Tuple for the ship's location
Check for Duplicates
Add the ship location to the ship list
Update the board by placing an 'S' where the ship is located
BONUS: Only have this show up if the user chooses DEVELOPER MODE!!
This gives you a chance to actually see the ships placed.