In the game of 'Hangman' two people play as follows:
one player writes down a word or phrase as a series of asterisks and spaces
the second player then guesses letter that might be in the phrase; as he does this he writes down each letter so as not to guess it again
if the second player makes a correct guess the asterisks for that letter are replaced with the letter itself so part of the hidden word is revealed
if the second player makes a wrong guess then the first player draws a gallows piece by piece
if the second player finds all the hidden letters before the gallows are built then he will have won the game
if the second player guesses the word before all the letters are revealed then he will also have won the game
if the gallows are completed before the word is guessed then the second player will have lost the game.
We will write a program that allows a human player to play against the computer.
This will be written as a Windows program rather than in console mode. Windows applications are written by placing controls on a form and writing the code that will be executed when an event is detected on the form such as clicking a button or changing the contents of a text box. The programmer writes event handlers for each control and any other possible events such as mouse actions.
*** These two sections will be written as procedures in a separate unit of code that does not reference form controls but is concerned with algorithmic operations such as opening files and converting characters. There is much to be said for using modules of code to break a program into manageable chunks and especially if these code blocks are used more than once in different parts of the code - as are the file open and string conversion routines.
Creating code modules requires a structured approach. First the procedure headings and any parameters should be defined inside the second unit:
procedure
procedure
These will be placed in the interface part of the Delphi code unit and later in the implementation section as well - Delphi units have these two sections. Note the use of var parameters (call by reference) to return data from the modules, along with normal (non-var) parameters (call by value) to pass data into the modules. The details of the operations in these modules can then be added.
The list of modules used by unit 1 must be extended to include the new code unit (unit 2).
Where code includes reference to controls on the form it may be better to leave the code in the same unit as the object handlers. It is always a benefit to keep code modules as simple as possible so an object event handler might, ideally, consist of a series of calls to modules rather than a lot of complex code that is hard to read and debug. Remember, breaking down complex tasks into a series of simpler ones is a foundational principle pf computational thinking.
As an alternative to the second unit of code for processing and logic operations that do not directly involve form controls the modules could be written inside unit 1. There are two possibilities here:
Define a module inside the object handler; the new module will be local to the handler because it is defined inside it but this may be acceptable if the code is not used wlsewhere in the program
Define a module as a procedure inside the form class; this requires that a module header (procedure or function) be written both inside the form class definition and in the implementation section as well. By making the new module a part of the form class it is available to all parts of the unit.