Battleship.... in the Terminal!
Published: 2024-05-15 18:53:41

I have been slowly chipping away at Codecademy's Computer science course over the last few weeks and tackled my first project of the course in the shape of a python script that lets a person battle against the computer component in the classic board-game 'Battleship'.

It was a rewarding challenge to brush up on my Python skills and implement modularization to keep this project manageable during the two weeks of development.

Programming a game in the terminal had its own unique set of challenges, the primary being a visual representation of the state of the game so that the player could easily follow along without losing track of their progress.

I'm particularly proud of the board representation I created using a matrix data structure. It effectively represents the rows and columns of the board, along with alphabetical characters to denote ships and actions that have occurred on each coordinate.

When I initially started the project, I didn't do any coding until I had vision of how the board would be rendered in a read-able manner. Thanks to the library termcolor - [termcolor](https://pypi.org/project/termcolor/). Making my script render the board with color representations made my vision truly pop.


![Color functions from the termcolor library making the game board come to life](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9x94wykjcfxmyzkbl38m.png)

The second aspect that I am happy with is the function I wrote within the gameFunctions.py, titled boat_spawn() which upon the start of the game randomly places the boats within the matrix following the standard battleship rules of no overlapping boats and no diagonal placements. This functions removed the struggle of the player having to manually enter coordinate by coordinate instructions of where they wanted their boats placed and instead gets the player immediately set into the heat of the game.

Lastly, I needed to design the computer opponent with an aggressive tendency on their turns when they had recently hit a target instead of consistently randomly generating attack coordinates. This aggression would simulate a second real player that would follow up with investigative shots toward successful attacks. This logic can be found in player.py and in the method titled, computer_turn_initiate(self). This method takes the last four attack calls the computer has made and checks it against the array of existing successful hits. The computer then randomly selects from the four possible adjacent indexes within the matrix data structure. While not not perfect in design, this simple solution adds enough depth to the computer opponent and keeps the game interesting.

![Computer attack function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnrlrsx8zf3z75kmy92c.png)

I'm very happy to be done with this project and continue my Codecademy course but I do plan on re-visiting and flushing out this project some more with a true multiplayer feature one day allowing two people over the internet to battle it out!

If you have any questions feel free to reach out and check out the repository over @ [github](https://github.com/FIV95/Battleship/tree/main)