At this point in the tutorial series, we can start putting programs together from the previous tutorials.
We will use the classic game Guess My Number. It is a very boring game to play, by today’s standards, but programing the game can be interesting.
These are the requirements of the game:
The game will prompt the user to select one of three games. The games are 1-10, 1-100, 1-1000. If the user does not enter the right selection, the program will display the selection again. Once the user selects the game, the program will randomize the number and prompt the user for a guess. The guess will be checked with the computer’s number. If the guess was not correct, the computer will prompt the user to guess higher or lower. If the guess was correct, the computer will display the number of times the user guessed, and the game will be over. The computer will prompt the user to play again. If the user does want to play again, the program will return to the game menu. If the user does not want to play again, the computer will quit the program.
Before we get to the code, there is are built-in functions called clrscr() and UpperCase(). Clrscr() clears the screen and needs the crt unit added to the unit section to work. UpperCase() converts the text to uppercase and needs the sysutils unit loaded into the unit section to work. UpperCase() is used to prevent the program from being case sensitive. The computer sees “A” differently than “a”. By converting the case, the user can enter both lower and uppercase answers and the computer will respond to the input the same way.
The Flow-Chart for Guess My Number:

The flow-chart was created using Diagram Designer
Program Code:
program FPTutorial7;
{$mode objfpc}{$H+}
uses
crt, sysutils,
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
answer, guess, cnt:integer;
game:string;
begin
repeat
repeat
clrscr(); { Clears the Screen, needs crt}
writeln('Please Select A Game: ');
writeln;
writeln('A) 10 ');
writeln('B) 100 ');
writeln('C) 1000 ');
writeln;
write('Selection : ');
readln(game);
game := UpperCase(game); {converts to uppercase, needs sysutils}
randomize; {Needed to get random numbers}
until (game = 'A') or (game = 'B') or (game = 'C'); {makes sure that
a correct response is given}
clrscr();
if game = 'A' then
begin
answer := random(10) + 1; {random (10) gets 0 - 9, so add one for 1 - 10}
writeln('Game 1 - 10');
end
else if game = 'B' then
begin
answer := random(100) + 1;
writeln('Game 1 - 100');
end
else if game = 'C' then
begin
answer := random(1000) + 1;
writeln('Game 1 - 1000');
end; {end if game statement}
cnt := 0; {set the counter to zero}
repeat
write('Please Enter A Guess: ');
readln(guess);
cnt := cnt + 1;
if guess < answer then
writeln('Too Low! ')
else if guess > answer then
writeln('Too High! ')
else
writeln('You Guessed My Number In ', cnt, ' Guesses! ');
until guess = answer; {repeat until the computer's number is guessed}
repeat
writeln;
write('Play Again Y/N : ');
readln(game);
game := UpperCase(game); {convert to uppercase}
until (game = 'Y') or (game = 'N'); {repeat until the response is correct}
until game = 'N'; {repeat until the user selects N to quit the game}
end.
Video Tutorial: Free Pascal Tutorial 7 - Guess My Number - Lazarus (15:25)