by Josh Blake: L6 Age ~17


This is the first (and we hope not the last) Pascal contribution from Josh. His Calculator is the first Python program on our Heroku site.

There are four ways that you can play battleships with Josh's program as shown by the menu:



In the first option, the computer places the five ships and you guess the cells that the ships occupy on the grid:

First option

First option

In the second option you place the ships and the program responds with the number of guesses that it took to find all the cells occupied by parts of a ship.

The following screenshot shows the end of a victorious game against the computer (Option 3):

Third option

Third option

Technical features

The code is impressively planned with well-chosen units and contains these advanced features:

  • object-oriented code;
  • advanced use of inheritance with abstract and virtual methods;
  • overloaded methods (same method name but different parameters);
  • factory methods (for instantiating different classes);
  • good use of own functions, some with own types as parameters e.g. function ResultToString(GuessResult: TGuessResult): String;
  • enumerated types;
  • default values of parameters in routines;
  • optional debugging outputting to a log file;
  • Appropriate use of inbuilt routines such as Append, Val, SetLength, Random, Low and High.


  • There are some typos in the output. Can you find them and correct the source code?
  • In its current version, the program records and processes the position of the fifth ship but does not always display it. Can you alter the code so that it displays that ship for all players in all game modes?
  • The code compiles in Delphi 2007 (using crt.pas and from an earlier version of Delphi). The usual way of converting it to run in Lazarus would be to (1) remove these files from the program folder so that Lazarus will use its own versions, (2) copy the contents of BattleshipGames.dproj to a new file named BattleshipGames.pas then (3) start a new project based on BattleshipGames.pas and (4) attempt to compile the project. The current compiler error message for Line 33 in Placers.pas is Duplicate identifier "INVALID_STR" with the hint Identifier already defined in Placers.pas at line 18. Can you make the code compile with Lazarus, either by changing the source or by changing the Lazarus settings?


The zip download contains the source code, Delphi 2007 project file and the executable. Unzip all of the files to a convenient folder. If you just want to try playing one or more of the game modes, double click on BattleshipGames.exe.
Programming - a skill for life!

Student programs to inspire you!