Turbo Pascal Text Adventure Primer cover: BEACON, the adventure the primer builds in Turbo Pascal 2.0, mid-game on the Tatung Einstein
← Back to Modules
module
7

Recognising Words

Introduction

You have the words. Now the program has to know them. That means a table of the words the game understands, and a way of finding a typed word in it. The simplest way is to compare whole words. The other way, older and very common in the games of the period, is to compare only the first few letters. Each has a price.

The Table

The words go in a typed constant, as the Turbo course kept its levels:

const
  Vocab: array[1..Count] of Str12 =
    ('NORTH', 'SOUTH', 'TAKE', 'DROP', ...);

A word's number in the table is how the program will know it. Whole reads down the table and returns the number of the entry that equals the typed word exactly, or 0.

First Letters

First compares only the first Sig letters of each:

if Copy(Vocab[I], 1, Sig) = Copy(W, 1, Sig) then

Copy past the end of a short word simply gives what there is, so KEY is compared as KEY whatever Sig is.

With Sig at 4, the player can type LANT for LANTERN, or MATCH for MATCHES, and misspell the end of a long word as much as they like. LANTERNS is understood too, though the game has no such word. And two words that share their first letters become one: with Sig at 2, LANTERN and LAMP are both LA, and the program cannot tell them apart.

Two Real Answers

Zork chose six letters. Its dictionary, read off its disc, holds every word the game knows cut to six letters, and in play only those six count:

Typed in Zork IZork said
OPEN MAILBOXXYZOpening the small mailbox reveals a leaflet.
OPEN MAILBOZunderstood as the mailbox
OPEN MAILBIXI don't know the word "mailbix".

Six letters is enough to keep the game's 697 words apart, and short enough to be stored compactly (S16).

Alice and the March Hare chose whole words. Its table holds INVENTORY and NORTH in full, and in play INVENTORY worked while INVENTOR, INVENTORYX and NORT all got No such command!. To save the player typing, it simply has extra entries: I for INVENTORY, N for NORTH, L for LOOK.

Both work. First letters forgive typing mistakes and can save space; whole words never confuse two words, at the price of a longer table.

The Code

program Know;
const
  Count = 12;
  Sig = 4;
type
  Str12 = string[12];
const
  Vocab: array[1..Count] of Str12 =
    ('NORTH', 'SOUTH', 'TAKE', 'DROP',
     'LOOK', 'LANTERN', 'LAMP',
     'LOGBOOK', 'MATCHES', 'KEY',
     'CROWBAR', 'OIL');
var
  W: Str12;

function Whole(W: Str12): Integer;
var
  I, Found: Integer;
begin
  Found := 0;
  for I := 1 to Count do
    if Vocab[I] = W then
      Found := I;
  Whole := Found;
end;

function First(W: Str12): Integer;
var
  I, Found: Integer;
begin
  Found := 0;
  for I := 1 to Count do
    if Copy(Vocab[I], 1, Sig) =
       Copy(W, 1, Sig) then
      Found := I;
  First := Found;
end;

begin
  repeat
    Write('Word? ');
    Readln(W);
    Writeln('Whole ', Whole(W),
            '  First ', First(W));
  until W = '';
end.

Starting from

A new work file KNOW. Type in capitals (CAPS LOCK as Turbo starts). ENTER on its own ends the program.

What you should see

Word? LANTERN
Whole 6  First 6
Word? LANT
Whole 0  First 6
Word? LANTERNS
Whole 0  First 6
Word? LAMP
Whole 7  First 7
Word? KEYS
Whole 0  First 0
Word? MATCH
Whole 0  First 9
Word? LOGS
Whole 0  First 0

Change One Thing

  • Change Sig = 4 to Sig = 2. Type the same words. Which answers are now wrong, and why?
  • Change it to Sig = 6. Which shortened words stop working?
  • In Whole, add else Found := 0; after Found := I (and take the semicolon off Found := I). Which words can Whole still find?

Exercises

7.1 With Sig = 2, make First count how many entries match, and return -1 when more than one does, so the program can say the word is ambiguous.

7.2 Work the way Zork does: at the start, cut every word in the table to six letters, and cut each typed word to six letters before looking it up. Show that LANTERNXYZ is understood and LANTEX is not.

7.3 Instead of numbers, print You mean LANTERN for a word that First recognises, and I don't know and the word for one it does not.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
A word is taken for a different wordToo few letters compared: two words share them.
A word is never foundSmall letters typed, or a space on the end. Tidy first (S5).
Only the last word in the table is ever foundThe loop clears Found again after finding the word.

Summary

A table of words and a loop down it recognise a word. Comparing whole words never confuses two words; comparing the first few letters forgives mistakes and long endings but can merge words that start alike. Zork kept six letters of every word; Alice and the March Hare kept whole words and added short ones as extra entries.

Next

S8, Words Into Numbers: once a word is known, stop comparing strings.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.