Creating a Higher Lower Game for Android

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.

Image for post
Image for post

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:

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:

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:

Image for post
Image for post

So, we need to enter a number for trying to guess the number picked randomly by the computer:

Image for post
Image for post

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.

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store