DEV Community

Cover image for How I made a Battleship Terminal Game using python 3 - Final Project
Skelly92
Skelly92

Posted on

How I made a Battleship Terminal Game using python 3 - Final Project

As part of my final project for the computer science path in codecademy, I made a single player battleship game in the terminal. I thought it would be interesting to make and would be a challenge as I was entirely sure of how I would accomplish this. The program is written in python 3.

It works by having the 10x10 grid as a dictionary in python 3 with the key being the grid letter and number configuration and the value being "A" by default. I changed the value when I set the battleship in the grid to their own letter which allows me to know if they have been attacked and when it is attacked and it has a battleship on it the value changes to "X" but if it doesn't the value changes to "O".

By using a for loop I went through the dictionaries keys and checked to see if the input the player set was the same as the key and that the value was equal to letter I had assigned to each battleship and if so it would output "KaBoom" if it was successful.

One of the issues I had while programming the game was how I was going to know if a player had successfully sunk a battleship. What I came up with was that when I set up the battleships on the main grid, I would copy in the key:value pairs to a separate dictionary which would allow me to remove them when successfully hit and then if the dictionary was empty the battleship has been sunk by the player.

I feel having been through the process of making this game that I have gained a lot of confidence in coding using python 3 and has helped me to get a better understanding of how to code and I am looking forward to coding a different project in the future.

You can view the game in action here as GIF: https://imgur.com/a/H8L1gdF

If you are interested in trying the game you can find it here:

GitHub logo Skelly92 / Battleship_Terminal

Single Player Battleship on the Terminal

Top comments (0)