Python Battleship (2/6): grid and display
Battleship in Python, part 2: a 10×10 grid, a terminal display with headers, and converting squares A1 through J10 into Python indexes for ship placement.
Getting started
Home Assistant
Buying
Battleship in Python, part 2: a 10×10 grid, a terminal display with headers, and converting squares A1 through J10 into Python indexes for ship placement.


AI-generated illustration
Some links on this page are affiliate links. The button opens Amazon so you can check today’s price.
In short — This lesson adds a 10×10 grid and terminal display to the Battleship project (the track hub). You convert squares A1–J10 into Python indexes 0–9.
You continue after part 1: this time, the smallest “display engine” so you can see the grids in the terminal.

One row = a list of TAILLE characters; the grid = TAILLE rows.
TAILLE = 10
VIDE = "."
def nouvelle_grille_defense():
return [[VIDE for _ in range(TAILLE)] for _ in range(TAILLE)]
def nouvelle_grille_radar():
return [["?" for _ in range(TAILLE)] for _ in range(TAILLE)]
To stay oriented, you often print 1–10 across the top and A–J down the left (or the other way around). A compact example: numbered columns, and rows numbered 0–9 at first (you can switch to letters next).
def afficher_grille(grille, titre="Grid"):
print(titre)
print(" " + " ".join(str(c + 1) for c in range(TAILLE)))
for i, ligne in enumerate(grille):
print(f"{i:2} " + " ".join(cell for cell in ligne))
For a “paper Battleship” look, replace the row index i with a letter:
def lettre_ligne(i):
return chr(ord("A") + i)
def afficher_grille_lettres(grille, titre="Grid"):
print(titre)
print(" " + " ".join(str(c + 1) for c in range(TAILLE)))
for i, ligne in enumerate(grille):
print(f"{lettre_ligne(i):2} " + " ".join(cell for cell in ligne))
B7The user often types one letter (row) and one number (column, usually 1-based).
def parser_case(texte):
texte = texte.strip().upper().replace(" ", "")
if len(texte) < 2:
raise ValueError("Too short (e.g. B7)")
lettre = texte[0]
chiffres = texte[1:]
if not ("A" <= lettre <= "J"):
raise ValueError("Row A–J")
ligne = ord(lettre) - ord("A")
colonne = int(chiffres) - 1 # 1..10 -> 0..9
if not (0 <= colonne < TAILLE):
raise ValueError("Column 1–10")
return ligne, colonne
Watch out: if your grid is not 10×10, adapt the letter range (A + TAILLE - 1).
def case_vers_texte(ligne, colonne):
return f"{lettre_ligne(ligne)}{colonne + 1}"
? with afficher_grille_lettres.input, use parser_case, and print “You aimed at row … column …”.# cells on a defense grid and display it: check the visual alignment.Mixing up column 0 in Python and column 1 for the player: keep one convention (here: internal 0–9, display 1–10).
Part 3 — Place the ships without overlap · Battleship hub · Programming track
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Battleship — Chapter 2/6: display and A1–J10 parser.
Run: python bataille_chapitre_02.py
"""
TAILLE = 10
VIDE = "."
RADAR_INCONNU = "?"
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 lettre_ligne(i):
return chr(ord("A") + i)
def afficher_grille_lettres(grille, titre="Grid"):
print(titre)
print(" " + " ".join(str(c + 1) for c in range(TAILLE)))
for i, ligne in enumerate(grille):
print(f"{lettre_ligne(i):2} " + " ".join(cell for cell in ligne))
def parser_case(texte):
texte = texte.strip().upper().replace(" ", "")
if len(texte) < 2:
raise ValueError("Too short (e.g. B7)")
lettre = texte[0]
chiffres = texte[1:]
derniere = chr(ord("A") + TAILLE - 1)
if not ("A" <= lettre <= derniere):
raise ValueError(f"Row A–{derniere}")
ligne = ord(lettre) - ord("A")
colonne = int(chiffres) - 1
if not (0 <= colonne < TAILLE):
raise ValueError(f"Column 1–{TAILLE}")
return ligne, colonne
def case_vers_texte(ligne, colonne):
return f"{lettre_ligne(ligne)}{colonne + 1}"
def main():
afficher_grille_lettres(nouvelle_grille_radar(), "Radar (all unknown)")
afficher_grille_lettres(nouvelle_grille_defense(), "Defense (water)")
while True:
txt = input("\nSquare (e.g. D5), blank to quit: ").strip()
if not txt:
break
try:
li, co = parser_case(txt)
print("Internal indexes: row", li, "column", co, "=>", case_vers_texte(li, co))
except ValueError as e:
print("Error:", e)
if __name__ == "__main__":
main()
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).