In this blog post, we are going to create a hangman game powered by the Google App Engine. Hangman is a game where you are trying determine a secret word by guessing letters that might be in the word.
Hangman Cloud Backend
The first thing to do is to create a cloud service that returns back a random word that we can use as our secret word. To do this, we first need a list of possible words to choose from. Fortunately, there is such a list of all 109582 words in the English language.
After downloading that list, we can write a quick little python cloud application that chooses a random word and returns it to the caller:
This code segment is very much based on the Google App Engine Hello World example. The code is actually very similar, but instead of returning “Hello World”, we return a random word to be used as the secret word in a game of hangman.
The biggest difference is the first section where we read in the text file (wordsEn.txt), and filter out any word that does not have at least four letters in it. We filter out short words because they are not very fun to play a game of hangman with. After we have the word list, it is just a matter of picking a random index into the array and returning that word.
Running the application locally and publishing it to the cloud are both covered very well in the Hello World tutorial, so I won’t go into much detail here. It requires another file to describe the application:
And then you can use the Google App Engine Launcher to run the application locally or publish (deploy) it to the cloud.
I published mine to http://hangman-riot.appspot.com/, so if you navigate there, you should see a random word in your browser:
Hangman Client Application
Fortunately, there’s already a great implementation of hangman in python from InventWithPython.com. So, we’ll use that as a starting point and add in some logic to use the cloud as the source of the secret word. The code can be downloaded from: http://inventwithpython.com/hangman.py
I suggest running this a few times before you modify it just to get a feel of how it works.
Now, there are two places that we need to modify. First, we need to remove the list of built in words that the application comes with, and replace the getRandomWord function with an implementation that grabs the word from our hangman cloud backend:
And then we need to add in a reference to the library that we are using the fetch the random word:
That’s it! The final version of the code can be found here.
For a detailed description of how the code works, check out the InventWithPython website.
Right now the code is set up to hit the google app engine instance that I created. If you want to hit a locally running application, just change the URL to be:
http://localhost:8080
Running Hangman on the Raspberry Pi
The hangman client python program requires python 3 in order to run. So, if you don’t already have that installed, install it with this command:
sudo apt-get install python3
Once that completes you can run the hangman program:
python3.2 hangman.py
That should do it. Good luck, the words can be pretty tricky!