Python Battleship (1/6): brief and data
Set the rules for a terminal Battleship game: a 10×10 grid, five ships, defense and radar symbols, and the Python constants you will reuse later.
Getting started
Home Assistant
Buying
Set the rules for a terminal Battleship game: a 10×10 grid, five ships, defense and radar symbols, and the Python constants you will reuse later.


AI-generated illustration
Some links on this page are affiliate links. The button opens Amazon so you can check today’s price.
This project runs across six articles. You will code Battleship you can play in the terminal, reusing what you saw in the Python track: lists, loops, input, functions, and conditions. Once the game runs, you can move it onto a Makeblock mBot2 or a Raspberry Pi 5 to show the grid on a screen or drive LEDs.
Amazon.co.uk (Makeblock mBot2)
Amazon.co.uk (Raspberry Pi 5)
Suggested level: comfortable through the articles on lists and, ideally, files (optional, for saving a game later).
Give yourself clear rules (you can change them later):
0 to 9, columns 0 to 9).In practice, each player works with:
For learning, you represent both as lists of lists (matrices), the same size as the sea.
| Symbol | Defense grid (my ships) |
|---|---|
. |
empty water |
# |
part of a ship not yet hit |
X |
ship hit |
o |
opponent’s shot in the water (miss) |
| Symbol | Radar grid (what I know about the enemy) |
|---|---|
? |
square not yet targeted |
O |
miss (confirmed) |
X |
hit (at least part of a ship) |
The radar does not show where the hidden # cells are: only your own shots.
Besides the grid, it is very practical to store each ship as the list of its coordinates (row, column). Example for a vertical destroyer at (2, 3), (3, 3):
torpilleur = [(2, 3), (3, 3)]
flotte = [
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4)], # length 5
# ... other ships
]
When the opponent fires at (2, 3), you mark that square as hit on the grid and you can check whether all the cells of torpilleur are hit → sunk.
You will implement that logic in parts 4 and 5.
You can already create a file constantes.py, or a header in your future bataille.py:
TAILLE = 10
LONGUEURS_NAVIRES = [5, 4, 3, 3, 2]
VIDE = "."
NAVIRE = "#"
TOUCHE = "X"
MANQUE_DEFENSE = "o"
RADAR_INCONNU = "?"
RADAR_MANQUE = "O"
RADAR_TOUCHE = "X"
Before you display the grid neatly:
10 × 10 list of lists filled with VIDE.print of one line (or two) that len(grille) == 10 and len(grille[0]) == 10.Part 2 — Grid, display, and “A1-style” coordinates: functions to create the sea, print it in the terminal, and turn B7 into indexes.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Battleship — Chapter 1/6: constants and empty grids (check).
Run: python bataille_chapitre_01.py
"""
TAILLE = 10
LONGUEURS_NAVIRES = [5, 4, 3, 3, 2]
VIDE = "."
NAVIRE = "#"
TOUCHE = "X"
MANQUE_DEFENSE = "o"
RADAR_INCONNU = "?"
RADAR_MANQUE = "O"
RADAR_TOUCHE = "X"
def nouvelle_grille_defense():
return [[VIDE for _ in range(TAILLE)] for _ in range(TAILLE)]
def nouvelle_grille_radar():
return [[RADAR_INCONNU for _ in range(TAILLE)] for _ in range(TAILLE)]
def main():
g = nouvelle_grille_defense()
assert len(g) == TAILLE and len(g[0]) == TAILLE
r = nouvelle_grille_radar()
nb_inconnues = sum(row.count(RADAR_INCONNU) for row in r)
assert nb_inconnues == TAILLE * TAILLE
print("OK — defense grid", TAILLE, "x", TAILLE, "filled with", repr(VIDE))
print("OK — radar:", nb_inconnues, "squares", repr(RADAR_INCONNU))
print("Ships to place (lengths):", LONGUEURS_NAVIRES)
if __name__ == "__main__":
main()
bataille_chapitre_01.py — the same content as the block above.
Check today’s price on Amazon
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).