Higher Lower Game is a famous game played by children in the playgrounds. In this two-player game, the first player thinks of a number between 1 and 1000. The second player must guess this number in a minimum of attempts. Each time the player who must guess the number proposes a number, the first player who has chosen a number answers with “Too High” or “Too Low”. Thus, it gives the indication to the one who has to guess for his next proposal. The game stops when the second player has found the number. We then note the number of tries he needed and in the next round, the other player will have to do better. In this tutorial, you are going to create your own Higher Lower Game for Android.
You can also watch this tutorial in video on YouTube:
Creating the UI of the Higher Lower Game
First, we start by creating the User Interface for our Higher Lower Game. You can note that with our implementation of the game, the computer will play the the role of the player who must chooses a number between 1 and 1000.
So, the UI of our game will be quite simple with the following views:
- A TextView letting us to display a message to the user after each attempt. The message displayed will be either “Too High” either “Too Low”.
- An EditText allowing the player to enter the number he thinks he has guessed.
- A Button allowing the player to validate the number he proposes to the computer.
It gives us the following code for the activity_main.xml layout file:
Writing the logic code of the Higher Lower Game
Now, we can write the logic code of the Higher Lower Game. For that, we use Java programming language. In the MainActivity class, we need to define the following properties for the Higher Lower Game:
- A MAX_NUMBER constant setted to 1000. Indeed, the computer will choose randomly a number between 1 and 1000.
- A numberToFind integer which is the the number choosen by the computer.
- A numberTries interger which is the number of attempts made by the player during the game.
For starting the game, we write a newGame() method in which we use the nextInt static method of the Random Java standard object to pick a number between 1 and 1000. Then, we display a start message in the TextView. We clear the EditText which will be used by the player to enter the number to guess. Finally, we set the numberTries property to zero.
It gives us the following code for the newGame() method:
Then, we need a method for validating the number entered by the player. Validating the number means that we check if the number is equal to the numberToFind integer. If so, we display a message indicating to the player he found the number with the number of attempts.
If no, we check if the number entered is lower or greather than the numberToFind integer. It lets us to display either the “Too High” either the “Too Low” message to the user in the TextView of our UI. Obviously, we don’t forget to increase by one the number of attempts each time the validate() method is called.
It gives us the following code for the validate() method:
Assembling all the pieces of the Puzzle
Next step is to assemble all the pieces of puzzle in the MainActivity class. In the onCreate method, we get references for the UI Views : the TextView, the EditText and the Button. Then, we set an OnClickListener instance on the Button. Like that, when a user click on this Button, we have just to call our validate() method.
And finally, we call the newGame() method at the end of the onCreate method to start a new Higher Lower Game each time the Android Application is launched.
It gives us the complete following code for the MainActivity class:
Our Higher Lower Game for Android in Action
Best part of the tutorial is there since we are going to play to our Higher Lower Game for Android. Once the Higher Lower Game is launched, we have the following display:
So, we need to enter a number for trying to guess the number picked randomly by the computer:
The computer indicates us the number is too low. So, we enter a higher number. Like that, we find the number and the computer displays the final message with the number of attempts for guessing the number.
That’s all for that tutorial. Feel free to use comments if you have some questions concerning the implementation of our Higher Lower Game for Android.