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.

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

A first TextView used to display the word to find with the letters already found by the user

A second TextView used to display a message to the user like: “You win” or “You lose”.

A set of Button views for representing the letters the user can enter. There will be 26 buttons: one for each letter of the alphabet.

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:

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

A wordFound char array to show the progression of the user during the Hangman Game

A list of letters already entered by the user

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

Clearing the list of letters already entered by the user

Generating a new word to find by calling the nextWordToFind() method

Initializing the word found array letting us to show the progression of the user during the game. Usually, the letter to guess are represented with the ‘_’ character

Updating the image displayed to the user for representing the state of the Hangman Game

Setting the content of the word found by the user until now in the first TextView of our UI

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.

If so, we get the text associated to the Button passed in parameter of the touchLetter callback. This text is simply the letter associated to the Button. It represents the letter entered by the user.

Otherwise, we display a toast indicating the user the game is ended.

Otherwise, we display a toast indicating the user the game is ended. So, we call the enter method with this letter in parameter. It lets us to manage the letter entered by the user.

After this call, we have to update the state of the word found by the user until now by calling the wordFoundCountent() method and by setting the return to the first TextView.

We have also to update the image displayed to the user by calling the updateImg method with the nbErrors as input.

Then, we check if the word is Found by calling the eponymous wordIsFound() method.

If so, we display a win message to the user.

Otherwise, we check if nbErrors is greater or equal to MAX_ERRORS. If so, game is ended and we display the word to find to the user.

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:

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

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

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.