The Essence of the Text Game

I was perusing Rosetta Code the other day, looking for more programming ideas to explore. One of the tasks provided, with examples in a variety of programming languages, was to code the old computer game Hunt the Wumpus.

Surprisingly, a version of the game wasn’t available in C on Rosette Code (probably because other programmers hadn’t gotten around to the task). So, I reviewed the rules and determined to code my own version of the game. How hard could it be?

Well, it wasn’t easy, but it wasn’t a monumental task either. I’ve coded text games before. Most of them follow a simple format:

  1. Initialize stuff
  2. Loop to process input
  3. Exit when certain conditions are met

As an example, consider the rudimentary “Guess the Number” game, which most beginning programmers code as an initial assignment:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int guess,number,tries;

    /* intialize stuff */
    srand( (unsigned)time(NULL) );     /* seed the randomizer */
    tries = 6;
    number = rand() % 100 + 1;        /* range is 1 to 100 */

    /* loop to process input */
    while( tries )
    {
        printf("You have %d tries left\n",tries);
        printf("Guess the number, 1 to 100: ");
        scanf("%d",&guess);
        /* check for winning condition */
        if( guess==number )
        {
            printf("%d was the number! You won!\n",number);
            break;
        }
        /* update the user */
        if( guess < number )
            printf("%d is too low\n",guess);
        else
            printf("%d is too high\n",guess);
        /* update the exit condition */
        tries--;
    }
    /* if the user lost, inform them */
    if( tries==0 )
    {
        printf("Too bad! You lost\n");
        printf("The number was %d\n",number);
    }

    return(0);
}

The program starts by initializing variables and the randomizer at Line 9. Variable tries is set equal to 6 because I read somewhere that it’s possible to guess a value between 1 and 100 in six or fewer tries (though I’m probably incorrect).

The while loop at Line 15 processes input, comparing the user’s choice guess (gathered at Line 19) with the random value stored in number.

When the user guesses correctly (Line 21), a success message is output and the loop breaks. Otherwise, a message is output indicating whether input was high or low, variable tries is decremented, and the loop continues. When tries is equal to zero, the while loop halts.

After the loop is completed, a final if test compares variable tries with zero. When true, a consolation message is output and the value of number is revealed.

Here’s a sample run:

You have 6 tries left
Guess the number, 1 to 100: 50
50 is too high. Try again.
You have 5 tries left
Guess the number, 1 to 100: 25
25 is too low. Try again.
You have 4 tries left
Guess the number, 1 to 100: 37
37 is too high. Try again.
You have 3 tries left
Guess the number, 1 to 100: 31
31 is too low. Try again.
You have 2 tries left
Guess the number, 1 to 100: 34
34 is too low. Try again.
You have 1 tries left
Guess the number, 1 to 100: 36
36 was the number! You won!

The game Hunt the Wumpus is coded similarly to this random number guessing game, though Wumpus has more complexities. I cover the details in next week’s Lesson. Still, the code from this Lesson is representative of how most computer text games work.

Leave a Reply