Coding for Kids: Guess the number

Coding for Kids: Guess the number

Guess the number was the first program that I have developed with my son. As a kind of appetizer to kindle the desire for programming. Here is what we have done…

Game idea

The idea of the game is simple. The program chooses a random number and the user has to guess it. After each input, you get some help from the computer: is the random number greater or less than the input you have entered.

What you have to know

The basics of a programming language must be known. These are for example the basic data types (integer, string) or commands such as IF or FOR.

The major functions of the IDE Integrated Development Environment of Lazarus must be know also. For example how you put components on a form and how to set properties in the Object Inspector.

Starting point should always be a new project by selecting PROJECT and saving project and units in a new empty folder.

Form design

The first thing we thought about together was how the surface has to look like. And to find the right corresponding components in Lazarus.

  • TLabel’s for the text elements
  • TSpinEdit’s for the input fields
  • TButton’s for all actions: Start Game, Slove and Exit

Then we have placed the components on the form and set the properties in the Object Inspector.

Guess the number Lazarus form

Until our project looked like this, it took a while. But it was a good practice to work with the Form Designer and do the settings via the Object Inspector. Our first prototype looked like this:

first release of guess the number
First design

The code


Next, we considered what variables we need. One for the random number. Another to count the attempts. The right place for that declaration is the private sector in the code.

    { private declarations }
    RandomNumber : integer;   //the random number we have to guess
    Attempts : integer;       //count of attempts

New game

Then the event routine for the first function of the game followed. Double-click the button „Start Game“ to create a new procedure.

procedure TForm1.Button3Click(Sender: TObject);

After that, we can fill this empty procedure with instructions. Random numbers are in Lazarus generated with the function Random.

  //find random number, starts with 0 >> +1

After that, the variables are set to the initial values.

  //don't show the RandomNumber

  //set Attempts count to 0

First program start

When our program starts the above procedure has to bee executed. This is done in Lazarus with an event. To do it double-click the OnCreate event of the main form.


Lazarus again generates a procedure hull FormCreate, which can be filled.

procedure TForm1.FormCreate(Sender: TObject);
  //Initialize random number generator

  //When starting, start direct new game

The procedure randomize is important. If it is not called Lazarus generates the same „random“ numbers when the program starts.

Validating input

Most of the work takes place in the event routine of the GUESS button. First attempts are counted and displayed. For this, we have access to the Caption property of the label.

procedure TForm1.Button1Click (Sender: TObject);
var input: integer;
  //increase Attempts

  //and show them
  Label3.Caption:=IntToStr(Attempts)+'. attempt:';

Then we have to check if the user guessed the right number or give some advice if the secret number is greater or smaller.

  //Evaluate the input of the number we are looking for
  if SpinEdit1.Value>RandomNumber then
    Label2.Caption:='Number is less than '+SpinEdit1.Text;

  if SpinEdit1.Value<RandomNumber then
    Label2.Caption:='Number is greater than '+SpinEdit1.Text;;

  if SpinEdit1.Value=RandomNumber then
      //You found the right number

      //Show a little picture

      //Show RandomNumber

Then everything will be prepared for the next attempt.

  //Reset the input edit

  //set the focus back to input edit


If the user got no idea about the right number, it’s possible to show it with a click on SOLVE.

procedure TForm1.Button2Click (Sender: TObject);
  //show the correct number
  Label2.Caption:='The number is '+IntToStr(RandomNumber); 

And that’s it. Now you can start compiling and executing the program!


Our first improvement was a better keyboard support. Instead of just a mouse click on GUESS, the Enter key should do the same.

That can be done with another event routine: KeyPress of the TSpinEdit.

procedure TForm1.SpinEdit1KeyPress(Sender: TObject; var Key: char);
  //if return key is pressed, then do the same as a click
  if Key=#13 then Button1Click(Sender);

And now have fun reprogramming the game!

guess the number
The final program



Download the Lazarus project als zip-file

Download Lazarus

Installing Lazarus (only German)

In the download folder you will also find an executable file of the software. If you are concerned about security, please read the page EXE files and the Internet (only German).

Klicken Sie hier, um diesen Artikel in Deutsch zu lesen.

Dieser Beitrag hat einen Kommentar

Kommentar verfassen