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.
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:
The code
Variables
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 { 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); begin end;
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 RandomNumber:=Random(SpinEdit2.Value+1);
After that, the variables are set to the initial values.
//don't show the RandomNumber Label5.Visible:=false; Label5.Caption:=IntToStr(RandomNumber); //set Attempts count to 0 Attempts:=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); begin //Initialize random number generator Randomize; //When starting, start direct new game Button3Click(nil); end;
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; begin //increase Attempts Attempts:=Attempts+1; //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 begin //You found the right number Label2.Caption:='Right!'; //Show a little picture Image1.Visible:=true; //Show RandomNumber Label5.Visible:=true; end;
Then everything will be prepared for the next attempt.
//Reset the input edit SpinEdit1.Value:=0; //set the focus back to input edit SpinEdit1.SetFocus; end;
Solve
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); begin //show the correct number Label2.Caption:='The number is '+IntToStr(RandomNumber); end;
And that’s it. Now you can start compiling and executing the program!
Improvement
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); begin //if return key is pressed, then do the same as a click if Key=#13 then Button1Click(Sender); end;
And now have fun reprogramming the game!
Links
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).
Pingback: Coding für Kids: Zahlen raten – Tech+Code