next up previous contents index
Next: Excercise Up: Hangman Previous: Hangman   Contents   Index

ASCII Art

Now we will put it all together to make a computer game of hangman. First, we need the ascii art7.1 to display the hanged man's body parts when the game player guesses a letter that isn't in the word. Our picture can be simple or elaborate. An example could be:

    0000000000000
    0           0
    0           1
    0          1 1
    0           1
    0          324
    0         3 2 4
    0        3  2  4
    0          5 6
    0         5   6
    0        5     6
    0       5       6
    0
    0
    0

or

 
    _________
    |         |
    |         0
    |        /|\
    |        / \
    |
    |

The possibilities are endless. Our program can print the current state of the game by putting each line of the drawing in a print function.

    print " _________     \n";
    print "|         |    \n";
    print "|         0    \n";
    print "|        /|\\  \n";
    print "|        / \\  \n";
    print "|              \n";
    print "|              \n";

Notice we need to double the ``\''. In perl, ``\'' is an escape character.7.2

We'll put our drawing code in a subroutine. In a program, a subroutine is something that will be run over and over. Rather than writing it out over and over, we use the name with any values that it needs in parenthesis. In this case our subroutine is called gallows and the argument it needs is the number of wrong guesses so far in the game. In perl, it will look something like

    gallows(3)

after the third wrong guess.

Define the gallows subroutine with ``sub'' followed by the name, ``gallows'' followed by a block of code encased in curly brackets, ``{ }''.

    sub gallows {
        ...
    }

There's a while loop to get input from the keyboard like in section 5.1, Terminal Input. Once again, the ``< >'' puts the line from the keyboard into the default scalar ``$_''. When the subroutine is run with ``gallows($_)'', ``$_'' is the first argument of the gallows subroutine. the arguments of a subroutine go into the default array ``@_''. The first item in the @_ array has the value $_[0] because perl starts numbering from 0.

    while (<>) { # <> puts the keyboard entry into $_
        gallows($_);# inside gallows $_[0] is the 1st argument 
                    # which is our keyboard entry $_
    }
    sub gallows { 
        # First the crossbar of the gallows 
            print "_____________   \n"; 
            print "|           |   \n"; 
        if ($_[0] == 0){ # Now the head 
            print "|               \n"; 
            print "|               \n"; 
            print "|               \n"; 
            print "|               \n"; 
        } else { 
            print "|           o   \n"; 
            print "|          o o  \n"; 
            print "|          o o  \n"; 
            print "|           o   \n"; 
        } 
        if ($_[0] <= 1) { # The body and arms
            print "|               \n"; 
            print "|               \n"; 
            print "|               \n"; 
        } elsif ($_[0] == 2) { 
            print "|           |    \n"; 
            print "|           |    \n"; 
            print "|           |    \n"; 
        } elsif ($_[0] == 3) { 
            print "|           |    \n"; 
            print "|          /|    \n"; 
            print "|         / |    \n"; 
        } elsif ($_[0] >3) { 
            print "|           |    \n"; 
            print "|          /|\\  \n"; 
            print "|         / | \\ \n"; 
        } 
        # Excercise: Write the code to print the legs here.   
    }



Subsections
next up previous contents index
Next: Excercise Up: Hangman Previous: Hangman   Contents   Index
Tom Hunt 2002-06-09