DEV Community

Cover image for The Ultimate Battleship - A Python Terminal Game
Sonja
Sonja

Posted on • Updated on

The Ultimate Battleship - A Python Terminal Game

For my first Python project I decided to create a Battleship Terminal Game. I love to play it on paper and was intrigued by the challenge to make it digital. This is part of the CS Career Path on www.codecademy.com.

What does the program do?

The programm will randomly hide 10 ships of 4 different sizes.
To play one needs to follow the text instructions and use the keyboard for input. The playing field will be updated after each shot and notifications will be displayed when a ship has been sunk. The player has 75 missiles at their disposal. If they manage to sink all the ships, THEY WIN.

How did I implement the game?

First, I thought about the visual appearance of the game. I definitely wanted to have a playing field. Therefore, I designed a 10x10 grid, each square numbered (A0 through J9). Also, the dialogue between program and player got worked out in the beginning. I guess, I worked along the storyline of the game.

Battleship Playing Field

To determine the position of the ships I worked with the random module. All playing fields, the hit list, the ships etc. are stored and being accessed as lists. I had a vague idea that for some purposes the use of dictionaries might have been more appropriate, but my understanding of lists was way better and so I went down that path.

The first tricky part was to make sure that the ships were distributed correctly (not touching each other, not spilling onto the next row or column). Then, I included some counters, so that the player can see, how many hits they have landed, how many missiles are still available, etc. The updated playing field and counters are being output to the terminal after each move the player makes. This is as visual as I could make it.

Playing field in action

The next challenge was figuring out how to tell the player that a ship or all ships of a kind, respectively, have been sunk. I managed with a whole series of nested if statements.

Finally, it took me a while to find out how to enable the player to go for several rounds of the game consecutively with different random playing field setups.

Playing field solution after lost game

In the end, I had coded all the features I had listed in the beginning as must haves and managed to refrain from working on most of the nice to haves, noting them for future enhancement.

What next?

I am pretty happy how the project turned out and have fun playing it myself. My main focus was to make it work. And that it does. A next step will be to get rid of the some of the lengthy and not very elegant code and make it more concise.

Update - December 2021: Version 2.0 is available

The program now gives a choice of 4 difficulty levels and keeps a running total of games played, games won and games lost.

Please, feel free to play the game and let me know what you think. You can find it on GitHub.

GitHub logo s-mehnert / Battleship

Terminal Game - play the classic Battleship Game against the computer.


The game has been created using Python 3.9.7

Top comments (0)