Gaming

Hangman

A simple hangman game.
Back to BLOCKS

About Hangman

This is an example of a simple game (hangman) hosted on PubNub. When creating a game server, it is important to hide some logic from the client. In this example, we have a hidden word which is only revealed to the client when correct letters are guessed. In addition, the server keeps track of the number of guesses and lives remaining. This prevents the client from being able to hack or modify the game logic.

Try it Now
Categories:
Gaming Gaming
Gaming
Gaming
Last Updated:
20160729 at 17:31:09
Author:
Ian Jennings

Walkthrough

In this example, we have a hidden word on the server that the client must guess. The client will be publishing a single letter like "a" over the network, and the server will respond with the new game state.

To accomplish this, we first store the secret word under the word variable. We also store the previous guesses, and remaining lives on the server. In this example the secret word is realtime.

The server starts by censoring the word variable into entirely dashes (-). As the client guesses correctly, the word is uncensored (--a-----) until either the word is revealed or the client runs out of lives.

Because the word is stored on the server and only exposed to the client in parts, it is impossible for the client to expose the word without playing the game. In an non obscured client environment (like JavaScript) the client would be able to discover the word by looking at the source.

As the client guesses letters, the guesses are stored in the database to calculate game progress over sequential guesses. If the word is discovered or the lives run out, the game is marked as won or lost and begins again.

Guess a letter

The client publishes a letter over the network. Input Channel: hangman-game-after-publish
"a"

The server responds.

The server responds with a game state object on an output channel. Output Channel: game-result
{
    "won: false,
    "lost": false,
    "word": ["-", "-", "a", "-", "-", "-", "-", "-"],
    "guesses: 1,
    "lives": 5,
    "successGuess": true
}