top of page

Battleships

Developed in my first year at university for my introduction to C++, I created a fully fledged battleships game. This challenging experience allowed to to explore a different language and helped me better understand coding standards.

Made for: ARU 1nd Year – trimester 2

Duration: 4 months

Language: C++

Position: individual

Brief (Excerpts):

The students are required to build a text-based variation of the classic battleship game focusing on Object Oriented Principles and procedural generation. The game must provide an interactive and enjoyable experience with understandable user controls and cohesive gameplay. The entire game:

  • is to be played only using a console window

  • is visualised using ascii characters

  • is a single-player game that plays again an AI enemy

    • The AI enemy should come with a set of strategies rather than random attacks

  • Is added with features designed by the student, e.g.,

    • Game mechanic design

    • Difficulty mode

    • Acquisition and use of tricks

    • etc.

This module was a introduction to C++, at the time, I was already learning C# which gave me the base understanding of OOP practice. This had helped me a lot when learning the language as it gave me a foundation to work on when trying to learn this very challenging language. This task was focused on simply learning how to code in C++, so it was not done in a game engine and was instead played in a console window. To show the visuals in a console window, I had to use characters to represent the game.

Placing Ships

The game required two sources of inputs for placing the ships and guessing locations. These sources were the player, and an AI that the player would compete against. With this in mind, I split the game into two sections, placing the ships and playing the game.

Click Image to view code

Capture.PNG

This was a function that I had created in that is run for both the player and the AI when placing ships, the function is designed to check the ships to make sure the placements do not collide with any other ships on the board.

Player moving ships:

Click Image to view code

Capture2.PNG

Click Image to view code

AI moving ships:

Capture3.PNG
ezgif.com-video-to-gif.gif
AI Thing.gif

Playing the game

Because battleships is a turn based game, I had to create a core loop which would make the player and the AI take turns in choosing which coordinate they wanted to strike. I wanted to make the AI smart too, making it so it would be intelligent in some of its placement choices.

Click Image to view code

Capture.PNG

This is a main while loop for the game, the way that I had made this game inside a console window was by refreshing the visuals when something had been inputted by clearing the console and then printing the board again. This is essentially the equivalent to a new frame in a application. The function first asks the user for input, then checks to see if they have won. Then the same is repeated for the AI as well.

Click Image to view code

player ex gif.gif

I wanted to make sure the AI would play similarly to how a person would play Battleships, fortunately, the game is mostly dependant on random guessing which made the AI's base logic simple. The complex part of the AI is when it's hit a ship, as there is a logical next step to take which consists of taking shots at the coordinates next to the original hit.

Click Image to view code

AI.gif

Overall, I'm satisfied with how the game turned out. This was a year one project, so it's reasonably understandable why some areas of code are quite messy and not as efficient as they could be. I would like to come back to this project to tidy up some of the code. Aside from this, I'm really proud of the logic behind the AI that I created, as this was the first time creating AI with intelligence.

bottom of page