Example Program: Guess

Example Program: Guess

Top  Previous  Next

A good way to learn a language is to simply jump in and get your feet wet. So let's write a small program in XPL0. We begin by describing the task in plain English.

 

This program is a guessing game where the computer thinks of a number between 1 and 100, and we try to guess it. After each guess, the program tells us whether we are high or low.The program goes through these steps:

 

1. Think of a number between 1 and 100.

2. Get a guess from the keyboard.

3. Test the guess against the number.

4. Repeat steps 2 and 3 until the guess is the number.

 

Here are the same steps translated into XPL0:

 

        begin

        MakeNumber;

        repeat  InputGuess;

                TestGuess

        until Guess = Number

        end

 

Note that the program is almost word for word the same as the description of the task. First we "make a number" then we repeatedly "input a guess" and "test the guess" until it is the number.

 

There needs to be more to this program since it does not tell how to make a number, input a guess, or test the guess. Each of these operations is a subroutine to the main program. In XPL0, these subroutines are called procedures. We are now going to write each of these procedures.

 

        procedure MakeNumber;

        begin

        Number:= Ran(100) + 1

        end

 

This procedure generates a random number between 1 and 100 and puts that number into the variable called "Number".

 

        procedure InputGuess;

        begin

        Text(0, "Input guess: ");

        Guess:= IntIn(0)

        end

 

This procedure displays the message: "Input guess: " on the monitor (output device 0) and gets a number (INTeger IN) from the keyboard (input device 0). In XPL0 nine different input and output devices can be called from the program. This enables direct access to the monitor, keyboard, printer, disk files, and so forth.

 

        procedure TestGuess;

        begin

        if Guess = Number then Text(0, "Correct!")

        else

                if Guess > Number then Text(0, "Too high")

                else Text(0, "Too low");

        CrLf(0)

        end

 

This procedure is more complicated but still easy to understand. If the computer's number is equal to the guess then we execute one statement; if it is not equal then we execute another statement. If the numbers are equal, we tell the user that the guess is correct; if they are not equal, we test if the guess is high or low and tell the user. CrLf(0) starts a new line on the monitor (Carriage Return and Line Feed).

 

Here is the complete program:

 

        code Ran=1, CrLf=9, IntIn=10, Text=12;

        integer Guess, Number;

 

        procedure MakeNumber;

        begin

        Number:= Ran(100) + 1

        end;

 

        procedure InputGuess;

        begin

        Text(0, "Input guess: ");

        Guess:= IntIn(0)

        end;

 

        procedure TestGuess;

        begin

        if Guess = Number then Text(0, "Correct!")

        else

                if Guess > Number then Text(0, "Too high")

                else Text(0, "Too low");

        CrLf(0)

        end;

 

        begin

        MakeNumber;

        repeat  InputGuess;

                TestGuess

        until Guess = Number

        end

 

Two new items are shown here. The command word "code" is used to give names to intrinsics. Intrinsics are built-in subroutines that do common operations. For example, "Ran" is the name of the random-number intrinsic, and "Ran" is used to call this random-number generator as a subroutine. The second item is the command word "integer". This declares a name and allocates memory space for each variable that follows it.

 

Note that the main procedure is the last block in the program. An XPL0 program is read starting at the bottom to get the main flow and working upward to get the details in the procedures.

 

Here is an example of what this program does when it runs:

 

        Input guess: 50

        Too high

        Input guess: 25

        Too high

        Input guess: 9

        Too low

        Input guess: 18

        Correct!