Develop A Hangman Game For Android

Hangman is a classical paper and pencil game for two or more players which everyone played during its childhood. The principle is quite easy. A player thinks to a word and the other player tries to guess it by suggesting letters. The player has limited number of errors he can make when suggesting a letter. In this tutorial, you are going to create a Hangman Game for Android with the computer choosing a word and the user who must find this word.

Image for post
Image for post

Creating The User Interface

First step is to create the User Interface of our Hangman Game for android. Our UI will have the following views:

  • An ImageView for representing the state of the game

By clicking on a Button, the user will enter a letter for the word to find. So, we add a callback method for each button.

It gives us the following code for the activity_main.xml layout file:

Last part of the User Interface is to put in the /res/drawable directory of our Android Application, the images that will be used for representing the state of the Hangman Game. For that, we use 7 images:

Image for post
Image for post

When the Hangman Game will start, we will display no image. The user will have the right to make 7 errors. Beyond this limit, the game will be over.

Defining Strings For The Hangman Game

Next step is to define some Strings for the Hangman Game. For example, we define a specific message when the user won the game or when the game is ended.

Nothing extraordinary, but these messages must be well defined. Here is the content of the strings.xml file:

Defining The Properties Of The Hangman Game

Now, we need to implement the core of the Hangman Game inside the MainActivity class. First, we define the words we will use for our Hangman Game. For fun, these words are the keywords of the Java Programming Language:

Our images to represent the state of the Hangman Game are indexed starting at 0. We use a nbErrors integer property to store the number of errors made by the user when entering a letter. We initialize nbErrors to -1. Like that, at the beginning of the Hangman Game, no image will be displayed.

Given this initial value for nbErrors, the MAX_ERRORS integer constant is set to 6. It represents the fact that the user can make until 7 errors when trying to find the word.

Besides, we define the other following properties in the MainActivity class for defining the core of the Hangman Game:

  • A wordToFind String representing the word to find

It gives us the following declaration for the MainActivity class:

Choosing The Word To Guess

Now, we need to define a method letting the computer to choose the next word the user must find. This choice will be made randomly by picking an element in the WORDS array:

Starting A New Game

Next step is to define a method to start a new Hangman Game. When we start a new game, we need to perform the following actions:

  • Setting the number of errors to -1

For that, we need to define an updateImg method taking as input the nbErrors integer. In this method, we get dynamically the identifier of the drawable to display. Then, we call the setImageResource method of the image for displaying this image:

After that, we write a wordFoundContent() method returning the state of the word found by the user until now:

Finally, it gives us the following code for the newGame() method of the MainActivity class:

Managing A Letter Entered By The User

We need to write a method for managing a letter entered by the user. In our enter method, we start by checking if the user has not already entered this letter. If no, we continue the process. We check the word to find contains the letter entered. If no, we increment the number of errors made by the user.

If the letter is in the word to find, we need to replace the ‘_’ by the occurrences of the letter in the wordFound array. For that, we uses the indexOf method of the String class and a while loop to find all the occurrences at the correct position. During the iteration, we just have to replace the ‘_’ by the letter entered by the user. It gives us the following code for the enter method:

You must note that we display some toast messages to the user during this process. For example, we display a “Try an other letter” message when the user enters a letter that is not in the word to find.

Implementing Core Logic Of The Hangman Game

Now, we have all we need to play a Hangman Game. We just need to assemble all the pieces of the puzzle. This assembling is made inside the touchLetter method which is the callback method called by each Button when a user will click on it.

In that method, we implement the following core logic for the Hangman Game:

  • First, we check if number of errors is lower than the maximum allowed and if game is not ended.

It gives us the following code for the MainActivity class:

Our Hangman Game For Android in Action

Best part of the tutorial is there since we are going to put our Hangman Game For Android in action! Once the game is started, we have the following display:

Image for post
Image for post

Then, we enter a first letter that is an error. So, the first image is displayed:

Image for post
Image for post

Finally, we continue to enter some letters and we lose. So, the word that was to find is displayed:

Image for post
Image for post

The user can start a new game by clicking on the “New Game” entry in the Action Bar of the Application.

That’s all for that tutorial. Feel free to use comments if you have some questions concerning this implementation of the Hangman 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