-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhtpataic13.html
926 lines (910 loc) · 32.5 KB
/
htpataic13.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
<!DOCTYPE html>
<html>
<head>
<title>13. The parser</title>
<link rel="stylesheet" href="htpataic.css" type="text/css" />
</head>
<body>
<table class="contents"><tr><td>
<h4>Contents</h4>
<div><a href="htpataic01.html">1. Introduction</a></div>
<div><a href="htpataic02.html">2. The main loop</a></div>
<div><a href="htpataic03.html">3. Locations</a></div>
<div><a href="htpataic04.html">4. Objects</a></div>
<div><a href="htpataic05.html">5. Inventory</a></div>
<div><a href="htpataic06.html">6. Passages</a></div>
<div><a href="htpataic07.html">7. Distance</a></div>
<div><a href="htpataic08.html">8. North, east, south, west</a></div>
<div><a href="htpataic09.html">9. Code generation</a></div>
<div><a href="htpataic10.html">10. More attributes</a></div>
<div><a href="htpataic11.html">11. Conditions</a></div>
<div><a href="htpataic12.html">12. Open and close</a></div>
<div><b>13. The parser</b></div>
<div><a href="htpataic14.html">14. Multiple nouns</a></div>
<div><a href="htpataic15.html">15. Light and dark</a></div>
<div><a href="htpataic16.html">16. Savegame</a></div>
<div><a href="htpataic17.html">17. Test automation</a></div>
<div><a href="htpataic18.html">18. Abbreviations</a></div>
<div><a href="htpataic19.html">19. Conversations</a></div>
<div><a href="htpataic20.html">20. Combat</a></div>
<div><a href="htpataic21.html">21. Multi-player</a></div>
<div><a href="htpataic22.html">22. Client-server</a></div>
<div><a href="htpataic23.html">23. Database</a></div>
<div><a href="htpataic24.html">24. Speech</a></div>
<div><a href="htpataic25.html">25. JavaScript</a></div>
</td></tr></table>
<h1>How to program a text adventure in C</h1>
<p>
by Ruud Helderman
<<a href="mailto:r.helderman@hccnet.nl">r.helderman@hccnet.nl</a>>
</p>
<p>
Licensed under
<a href="https://github.com/helderman/htpataic/blob/master/LICENSE">MIT License</a>
</p>
<h2>13. The parser</h2>
<p class="intro">
Every text adventure has a
<a href="http://en.wikipedia.org/wiki/Parsing">parser</a>.
But there are parsers and parsers.
A simple ‘verb-noun’ parser
(like the one we have been using ever since chapter 2)
could be sufficient for a carefully designed adventure game.
However,
<a href="http://en.wikipedia.org/wiki/Infocom">Infocom</a>
has proven that a more advanced parser
really helps to make an enjoyable game.
It does not have to pass the
<a href="http://en.wikipedia.org/wiki/Turing_test">Turing test</a>;
remember, it’s only a game.
But the parser should enable the player
to express his intentions in a more or less natural way.
</p>
<p>
Our current parser mainly consists of two lines of code,
tucked away in <i>parsexec.c</i>:
</p>
<table><tr>
<td class="snippet">char *verb = strtok(input, " \n");
char *noun = strtok(NULL, "\n");
</td>
</tr></table>
<p>
Alright, plus the sequence of <i>strcmp</i> calls to map verbs to commands,
and the functions in <i>noun.c</i> to map nouns to objects.
But that’s it.
This system has served us well for the past 11 chapters,
but it has its flaws.
</p>
<ul>
<li>It only accepts simple commands of the form ‘verb noun’;
it does not understand sentences
with both a direct object and an indirect object, like <i>put coin in box</i>.
</li>
<li>It does accept multi-word objects (like <i>silver coin</i>),
but the
<a href="http://en.wikipedia.org/wiki/Whitespace_character">whitespace</a>
between the words must be spot-on.
Our game rejects a double space between <i>silver</i> and <i>coin</i>.
</li>
<li>It is
<a href="http://en.wikipedia.org/wiki/Case_sensitivity">case-sensitive</a>;
the command “Go north” is not recognized
because of the capital ‘G’.
</li>
</ul>
<p>
Writing a good parser is no trivial task,
but here I will give you a relatively simple approach.
We will define a
<a href="http://en.wikipedia.org/wiki/Formal_grammar">grammar</a>
that consists of a list of <b>patterns</b>,
similar to (but much simpler than)
<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expressions</a>.
Examples of patterns:
</p>
<table cellpadding="8"><tr>
<td class="snippet">look around</td>
<td class="comment">Matches just that;
double spaces, leading spaces, trailing spaces and case differences
are ignored</td>
</tr><tr>
<td class="snippet">go A</td>
<td class="comment">Matches the word <i>go</i> followed by the tag of an object
(see chapters 4 and 8 for an explanation of ‘tags’)</td>
</tr><tr>
<td class="snippet">put A in B</td>
<td class="comment">Matches the word <i>put</i> followed by
a tag, the word <i>in</i>, and another tag</td>
</tr></table>
</p>
<p>
To parse the user’s input,
we will traverse the list of patterns from top to bottom,
trying to match the user’s input with each pattern in turn.
We will stop at the first match found.
For the sake of simplicity, we will not make use of
<a href="http://en.wikipedia.org/wiki/Backtracking">backtracking</a>,
though this could be added later.
</p>
<p>
Uppercase letters are the
<a href="http://en.wikipedia.org/wiki/Nonterminal_symbol">nonterminal symbols</a>
in our grammar;
they match <b>any tag</b> (of any object).
Matching is
<a href="https://en.wikipedia.org/wiki/Greedy_algorithm">greedy</a>;
when the parser has a choice between two different tags
(for example ‘silver coin’ and ‘silver’),
the longer tag will take precedence.
</p>
<p>
The matching tag can then be passed as parameter <i>noun</i>
to one of the <i>execute</i> functions.
For commands with more than one noun (introduced in the next chapter),
parameter passing becomes a bit unpractical.
For simplicity, I will use a
<a href="http://en.wikipedia.org/wiki/Global_variable">global variable</a>
instead of parameters (despite the bad reputation of global variables).
The variable will be an array of string pointers.
</p>
<table><tr>
<td class="snippet">const char *params[26];
</td>
</tr></table>
<p>
The array has 26 elements; one for each (uppercase) letter in the alphabet.
That is sufficient for upto 26 different nonterminals in a single pattern.
For every nonterminal in a (matching) pattern,
a matching tag will be ‘captured’ by filling
the nonterminal’s array element with a pointer to that particular tag.
params[0] (the first array element) captures nonterminal ‘A’,
params[1] captures ‘B’, and so forth.
A simple macro can be used
to find the array element that belongs to a given nonterminal.
</p>
<table><tr>
<td class="snippet">#define paramByLetter(letter) (params + (letter) - 'A')
</td>
</tr></table>
<p>
Note: you may find the array length of 26 a bit over the top,
but it does save me the trouble of writing some
<a href="http://en.wikipedia.org/wiki/Bounds_checking">bounds checking</a>
code to prevent a
<a href="http://en.wikipedia.org/wiki/Buffer_overflow">buffer overflow</a>
in case of a malformed pattern.
</p>
<p>
Now we have to think of a way to handle missing or unrecognized nouns.
Suppose the user makes a typo and enters “go kave”.
Question is, should this command match the pattern “go A” or not?
If we don’t, then the command will fail to match <i>any</i> pattern
and end up in a generic error handler, which will probably reply with
something like “I don’t know how to <i>go kave</i>”.
This takes away every opportunity
to improve these ‘negative responses’;
a reply “I don’t understand where you want to go”
already feels a lot more natural.
It would be best to maintain all replies concerning command <i>go</i>
inside function <i>executeGo</i>.
</p>
<p>
There are a few ways to achieve this, but the easiest one is
to allow nonterminals to match <i>anything</i>;
so not just a valid tag, but also total gibberish, blanks or just nothing.
This ‘invalid’ input will be captured as an empty string
(“”).
</p>
<p>
Having such a ‘loose’ nonterminal in the middle of a pattern
does complicate the pattern matching process; it would require
<a href="http://en.wikipedia.org/wiki/Backtracking">backtracking</a>
to properly align the word ‘in’
when matching input “put foo in in box”
with pattern “put A in B”.
</p>
<p>
To keep things simple, we will <i>only</i> enable this loose matching
for nonterminals that occur at the very end of a pattern.
To be able to properly match the example above, we need two separate patterns:
</p>
<ul>
<li>Pattern “put A in B” will match
the valid command <i>put coin in box</i>,
as well as the invalid commands
<i>put coin in booox</i> and <i>put coin in</i>.
Note that the nonterminal A only matches valid tags
(in this case <i>coin</i>).
</li>
<li>Pattern “put A” matches
all remaining invalid commands:
<i>put coin</i>,
<i>put koin</i>,
<i>put koin in box</i>,
<i>put koin in booox</i> and a bare <i>put</i>.
And also the initial example (<i>put foo in in box</i>).
</li>
</ul>
<p>
The order of the patterns is vital here:
if “put A” would be on top (meaning it will be tried first),
then it would consume every <i>put</i> command;
a valid “put coin in box” would be considered invalid
because there is no tag “coin in box.”
</p>
<p>
The same goes for commands that appear in multiple forms, such as <i>look</i>.
</p>
<ul>
<li>“look around”</li>
<li>“look” (as an abbreviation of <i>look around</i>)</li>
<li>“look A”</li>
</ul>
<p>
The first two patterns can be in any order,
but the third one <i>must</i> come last.
</p>
<p>
Time to put this into action.
We will discard the existing contents of module <i>parsexec.c</i>,
and replace it by a new implementation of function <i>parseAndExecute</i>
using a list of patterns
that should be able to match every command we have implemented so far.
Each pattern is tied to a function that executes the appropriate command.
</p>
<table class="code"><tr>
<th>parsexec.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern bool parseAndExecute(const char *input);</li>
</ol>
</td>
</tr><tr>
<th>parsexec.c</th>
</tr><tr>
<td>
<ol>
<li class="new">#include <ctype.h></li>
<li class="new">#include <stdbool.h></li>
<li class="new">#include <stdio.h></li>
<li class="new">#include "object.h"</li>
<li class="new">#include "misc.h"</li>
<li class="new">#include "match.h"</li>
<li class="new">#include "location.h"</li>
<li class="new">#include "inventory.h"</li>
<li class="new">#include "openclose.h"</li>
<li class="new"></li>
<li class="new">typedef struct</li>
<li class="new">{</li>
<li class="new"> const char *pattern;</li>
<li class="new"> bool (*function)(void);</li>
<li class="new">} COMMAND;</li>
<li class="new"></li>
<li class="new">static bool executeQuit(void)</li>
<li class="new">{</li>
<li class="new"> return false;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">static bool executeNoMatch(void)</li>
<li class="new">{</li>
<li class="new"> const char *src = *params;</li>
<li class="new"> int len;</li>
<li class="new"> for (len = 0; src[len] != '\0' && !isspace(src[len]); len++);</li>
<li class="new"> if (len > 0) printf("I don't know how to '%.*s'.\n", len, src);</li>
<li class="new"> return true;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">bool parseAndExecute(const char *input)</li>
<li class="new">{</li>
<li class="new"> static const COMMAND commands[] =</li>
<li class="new"> {</li>
<li class="new"> { "quit" , executeQuit },</li>
<li class="new"> { "look" , executeLookAround },</li>
<li class="new"> { "look around" , executeLookAround },</li>
<li class="new"> { "look at A" , executeLook },</li>
<li class="new"> { "look A" , executeLook },</li>
<li class="new"> { "examine A" , executeLook },</li>
<li class="new"> { "go to A" , executeGo },</li>
<li class="new"> { "go A" , executeGo },</li>
<li class="new"> { "get A" , executeGet },</li>
<li class="new"> { "drop A" , executeDrop },</li>
<li class="new"> { "ask A" , executeAsk },</li>
<li class="new"> { "give A" , executeGive },</li>
<li class="new"> { "inventory" , executeInventory },</li>
<li class="new"> { "open A" , executeOpen },</li>
<li class="new"> { "close A" , executeClose },</li>
<li class="new"> { "lock A" , executeLock },</li>
<li class="new"> { "unlock A" , executeUnlock },</li>
<li class="new"> { "A" , executeNoMatch }</li>
<li class="new"> };</li>
<li class="new"> const COMMAND *cmd;</li>
<li class="new"> for (cmd = commands; !matchCommand(input, cmd->pattern); cmd++);</li>
<li class="new"> return (*cmd->function)();</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 1:
from <a href="http://en.wikipedia.org/wiki/Ctype.h">ctype.h</a>,
we will be using <i>isspace</i> (line 26),
which provides a more complete
<a href="http://en.wikipedia.org/wiki/Whitespace_(computer_science)">whitespace</a>
test than a simple comparison with
<a href="http://en.wikipedia.org/wiki/Space_character">space</a>
(it also matches
<a href="http://en.wikipedia.org/wiki/Tab_character">tab</a>,
among others).
</li>
<li>Line 11-15:
type <i>COMMAND</i> is a structure that combines a pattern with a function.
</li>
<li>Line 17-29:
a few generic handlers.
</li>
<li>Line 26:
this loop calculates the length of the first word entered by the player;
we assume this is supposed to be the (unrecognized) verb.
</li>
<li>Line 27:
we use a special format specifier <code>%.*s</code>
to print only the first <i>len</i> characters of <i>src</i>.
</li>
<li>Line 33-53:
the array of patterns.
</li>
<li>Line 38-40:
<a href="https://en.wikipedia.org/wiki/Synonym">synonyms</a>
are simple: just map different patterns to the same function.
</li>
<li>Line 52:
the last pattern should always be “A”;
this pattern matches <i>anything</i> and so it will pick up
everything that hasn’t been matched by preceding patterns.
</li>
<li>Line 55:
this loop scans though all patterns from top to bottom;
it stops at the first pattern that matches.
The ‘sure match’ on line 52 ensures
the loop will never pass the end of the array.
</li>
<li>Line 56:
the function that belongs to the matching pattern is called.
</li>
</ul>
</div>
<p>
The hardest part is the implementation of function <i>matchCommand</i>.
But as you can see below, that too can be done in less than 100 lines of code.
</p>
<table class="code"><tr>
<th>match.h</th>
</tr><tr>
<td>
<ol>
<li class="new">#define MAX_PARAMS 26</li>
<li class="new"></li>
<li class="new">extern const char *params[];</li>
<li class="new"></li>
<li class="new">#define paramByLetter(letter) (params + (letter) - 'A')</li>
<li class="new"></li>
<li class="new">extern bool matchCommand(const char *src, const char *pattern);</li>
</ol>
</td>
</tr><tr>
<th>match.c</th>
</tr><tr>
<td>
<ol>
<li class="new">#include <ctype.h></li>
<li class="new">#include <stdbool.h></li>
<li class="new">#include <string.h></li>
<li class="new">#include "object.h"</li>
<li class="new">#include "misc.h"</li>
<li class="new">#include "match.h"</li>
<li class="new"></li>
<li class="new">const char *params[MAX_PARAMS];</li>
<li class="new"></li>
<li class="new">static const char *skipSpaces(const char *src)</li>
<li class="new">{</li>
<li class="new"> while (isspace(*src)) src++;</li>
<li class="new"> return src;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">static const char *matchSpaces(const char *src)</li>
<li class="new">{</li>
<li class="new"> return *src == '\0' || isspace(*src) ? skipSpaces(src) : NULL;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">static const char *matchTerminal(const char *src, char terminal)</li>
<li class="new">{</li>
<li class="new"> return terminal == ' ' ? matchSpaces(src) :</li>
<li class="new"> tolower(*src) == tolower(terminal) ? src + 1 : NULL;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">static const char *matchTag(const char *src, const char *tag, bool atEnd)</li>
<li class="new">{</li>
<li class="new"> while (src != NULL && *tag != '\0')</li>
<li class="new"> {</li>
<li class="new"> src = matchTerminal(src, *tag++);</li>
<li class="new"> }</li>
<li class="new"> return atEnd && src != NULL && *skipSpaces(src) != '\0' ? NULL : src;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">static const char *matchParam(const char *src, const char **par, bool loose)</li>
<li class="new">{</li>
<li class="new"> const char *restOfSrc = loose ? src + strlen(src) : NULL;</li>
<li class="new"> OBJECT *obj;</li>
<li class="new"> for (obj = objs; obj < endOfObjs; obj++)</li>
<li class="new"> {</li>
<li class="new"> const char **tag;</li>
<li class="new"> for (tag = obj->tags; *tag != NULL; tag++)</li>
<li class="new"> {</li>
<li class="new"> const char *behindMatch = matchTag(src, *tag, loose);</li>
<li class="new"> if (behindMatch != NULL && strlen(*tag) > strlen(*par))</li>
<li class="new"> {</li>
<li class="new"> *par = *tag;</li>
<li class="new"> restOfSrc = behindMatch;</li>
<li class="new"> }</li>
<li class="new"> }</li>
<li class="new"> }</li>
<li class="new"> if (**par == '\0')</li>
<li class="new"> {</li>
<li class="new"> *par = src;</li>
<li class="new"> }</li>
<li class="new"> return restOfSrc;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">bool matchCommand(const char *src, const char *pattern)</li>
<li class="new">{</li>
<li class="new"> const char **par;</li>
<li class="new"> for (par = params; par < params + MAX_PARAMS; par++)</li>
<li class="new"> {</li>
<li class="new"> *par = "";</li>
<li class="new"> }</li>
<li class="new"> for (src = skipSpaces(src); src != NULL && *pattern != '\0'; pattern++)</li>
<li class="new"> {</li>
<li class="new"> src = isupper(*pattern)</li>
<li class="new"> ? matchParam(src, paramByLetter(*pattern), pattern[1] == '\0')</li>
<li class="new"> : matchTerminal(src, *pattern);</li>
<li class="new"> }</li>
<li class="new"> return src != NULL && *skipSpaces(src) == '\0';</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 16-19:
function <i>matchSpaces</i>
tries to match a stretch of whitespace, either between two words,
or (possibly zero-width) at the end of the input string.
As with most of the functions in this module,
parsing starts at the character pointed to by parameter <i>src</i>;
the return value is either a pointer to the position in the input string
that immediately follows the match
(in this case, to the first non-whitespace character encountered;
this <i>may</i> be the NUL character denoting end-of-string),
or NULL, meaning no match was found
(in this case, if there was a non-whitespace character at the start position).
</li>
<li>Line 21-25:
function <i>matchTerminal</i> matches a particular character,
case-insensitively.
If the required character (parameter <i>terminal</i>) is a space,
the match is a stretch of (whitespace) characters, as explained above.
</li>
<li>Line 27-34:
function <i>matchTag</i> matches a particular word, case-insensitively.
If parameter <i>atEnd</i> is true,
then matching will only succeed if the word is last in the input string.
</li>
<li>Line 36-58:
function <i>matchParam</i> matches and captures a nonterminal.
All tags of all objects will be scanned to find the longest matching tag.
The array element pointed to by <i>par</i>
will be filled with a pointer to the matching tag (if any).
Set parameter <i>loose</i> to match a ‘loose’ nonterminal;
this will always consume the entire input string
(i.e. return a pointer to the end-of-string character),
regardless of whether there is a tag to satisfy this match.
</li>
<li>Line 53-56:
if no matching tag is found, then the remaining input is captured.
This can be used to included the non-matching input in an error message;
we do this in function <i>executeNoMatch</i>, defined in <i>parsexec.c</i>.
</li>
<li>Line 60-74:
function <i>matchCommand</i> matches an entire pattern,
capturing nonterminals into array <i>params</i>.
</li>
</ul>
</div>
<p>
We adjust the implementations of the various commands
to make use of the new array <i>params</i>.
</p>
<table class="demo">
<tr><th>Sample output</th></tr>
<tr><td>
Welcome to Little Cave Adventure.<br />
You are in an open field.<br />
You see:<br />
a silver coin<br />
a burly guard<br />
a cave entrance to the east<br />
dense forest all around<br />
<br />
--> get coin<br />
You pick up a silver coin.<br />
<br />
--> give coin<br />
You give a silver coin to a burly guard.<br />
<br />
--> go cave<br />
You walk into the cave.<br />
<br />
You are in a little cave.<br />
You see:<br />
an exit to the west<br />
solid rock all around<br />
a closed door to the south<br />
a tiny key<br />
<br />
--> get key<br />
You pick up a tiny key.<br />
<br />
--> go south<br />
The door is closed.<br />
<br />
--> open door<br />
You open a closed door to the south.<br />
<br />
--> go south<br />
You walk through the door into a backroom.<br />
<br />
You are in a backroom.<br />
You see:<br />
solid rock all around<br />
an open door to the north<br />
a wooden box<br />
<br />
--> unlock box<br />
You unlock a wooden box.<br />
<br />
--> open box<br />
You open a wooden box.<br />
<br />
--> examine box<br />
The box is open.<br />
You see:<br />
a gold coin<br />
<br />
--> get gold<br />
You get a gold coin from a wooden box.<br />
<br />
--> quit<br />
<br />
Bye!<br />
</td></tr>
</table>
<table class="code"><tr>
<th>inventory.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern bool executeGet(void);</li>
<li class="new">extern bool executeDrop(void);</li>
<li class="new">extern bool executeAsk(void);</li>
<li class="new">extern bool executeGive(void);</li>
<li class="new">extern bool executeInventory(void);</li>
</ol>
</td>
</tr><tr>
<th>inventory.c</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdbool.h></li>
<li class="old">#include <stdio.h></li>
<li class="old">#include "object.h"</li>
<li class="old">#include "misc.h"</li>
<li class="new"><span class="old">#include </span>"match.h"</li>
<li class="new">#include<span class="old"> "noun.h"</span></li>
<li class="old">#include "move.h"</li>
<li class="old"></li>
<li class="new">bool executeGet(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> OBJECT *obj = getVisible("what you want to get", </span>params[0]);</li>
<li class="old"> switch (getDistance(player, obj))</li>
<li class="old"> {</li>
<li class="old"> case distSelf:</li>
<li class="old"> printf("You should not be doing that to yourself.\n");</li>
<li class="old"> break;</li>
<li class="old"> case distHeld:</li>
<li class="old"> printf("You already have %s.\n", obj->description);</li>
<li class="old"> break;</li>
<li class="old"> case distOverthere:</li>
<li class="old"> printf("Too far away, move closer please.\n");</li>
<li class="old"> break;</li>
<li class="old"> case distUnknownObject:</li>
<li class="old"> // already handled by getVisible</li>
<li class="old"> break;</li>
<li class="old"> default:</li>
<li class="old"> if (obj->location != NULL && obj->location->health > 0)</li>
<li class="old"> {</li>
<li class="old"> printf("You should ask %s nicely.\n", obj->location->description);</li>
<li class="old"> }</li>
<li class="old"> else</li>
<li class="old"> {</li>
<li class="old"> moveObject(obj, player);</li>
<li class="old"> }</li>
<li class="old"> }</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeDrop(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> moveObject(getPossession(player, "drop", </span>params[0]),<span class="old"> player->location);</span></li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeAsk(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> moveObject(getPossession(actorHere(), "ask", </span>params[0]),<span class="old"> player);</span></li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeGive(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> moveObject(getPossession(player, "give", </span>params[0]),<span class="old"> actorHere());</span></li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool<span class="old"> executeInventory(void)</span></li>
<li class="old">{</li>
<li class="old"> if (listObjectsAtLocation(player) == 0)</li>
<li class="old"> {</li>
<li class="old"> printf("You are empty-handed.\n");</li>
<li class="old"> }</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
</ol>
</td>
</tr></table>
<p>
Same thing for the module we added in the previous chapter.
</p>
<table class="code"><tr>
<th>openclose.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern bool executeOpen(void);</li>
<li class="new">extern bool executeClose(void);</li>
<li class="new">extern bool executeLock(void);</li>
<li class="new">extern bool executeUnlock(void);</li>
</ol>
</td>
</tr><tr>
<th>openclose.c</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdbool.h></li>
<li class="old">#include <stdio.h></li>
<li class="old">#include "object.h"</li>
<li class="new"><span class="old">#include </span>"match.h"</li>
<li class="new">#include<span class="old"> "reach.h"</span></li>
<li class="old"></li>
<li class="new">bool executeOpen(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> OBJECT *obj = reachableObject("what you want to open", </span>params[0]);</li>
<li class="old"> if (obj != NULL) (*obj->open)();</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeClose(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> OBJECT *obj = reachableObject("what you want to close", </span>params[0]);</li>
<li class="old"> if (obj != NULL) (*obj->close)();</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeLock(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> OBJECT *obj = reachableObject("what you want to lock", </span>params[0]);</li>
<li class="old"> if (obj != NULL) (*obj->lock)();</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeUnlock(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> OBJECT *obj = reachableObject("what you want to unlock", </span>params[0]);</li>
<li class="old"> if (obj != NULL) (*obj->unlock)();</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
</ol>
</td>
</tr></table>
<p>
In <i>location.c</i>, the command <i>look around</i> is given its own function,
separate from the <i>look</i> command to inspect specific objects
(see lines 7-12).
</p>
<table class="code"><tr>
<th>location.h</th>
</tr><tr>
<td>
<ol>
<li class="new"><span class="old">extern </span>bool executeLookAround(void);</li>
<li class="new"><span class="old">extern </span>bool executeLook(void);</li>
<li class="new">extern bool executeGo(void);</li>
</ol>
</td>
</tr><tr>
<th>location.c</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdbool.h></li>
<li class="old">#include <stdio.h></li>
<li class="old">#include "object.h"</li>
<li class="old">#include "misc.h"</li>
<li class="new"><span class="old">#include </span>"match.h"</li>
<li class="new">#include<span class="old"> "noun.h"</span></li>
<li class="old"></li>
<li class="new">bool executeLookAround(void)</li>
<li class="old">{</li>
<li class="old"> printf("You are in %s.\n", player->location->description);</li>
<li class="old"> listObjectsAtLocation(player->location);</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeLook(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> OBJECT *obj = getVisible("what you want to look at", </span>params[0]);</li>
<li class="old"> switch (getDistance(player, obj))</li>
<li class="old"> {</li>
<li class="old"> case distHereContained:</li>
<li class="old"> printf("Hard to see, try to get it first.\n");</li>
<li class="old"> break;</li>
<li class="old"> case distOverthere:</li>
<li class="old"> printf("Too far away, move closer please.\n");</li>
<li class="old"> break;</li>
<li class="old"> case distNotHere:</li>
<li class="new"><span class="old"> printf("You don't see any %s here.\n", </span>params[0]);</li>
<li class="old"> break;</li>
<li class="old"> case distUnknownObject:</li>
<li class="old"> // already handled by getVisible</li>
<li class="old"> break;</li>
<li class="old"> default:</li>
<li class="old"> printf("%s\n", obj->details);</li>
<li class="old"> listObjectsAtLocation(obj);</li>
<li class="old"> }</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">static void movePlayer(OBJECT *passage)</li>
<li class="old">{</li>
<li class="old"> printf("%s\n", passage->textGo);</li>
<li class="old"> if (passage->destination != NULL)</li>
<li class="old"> {</li>
<li class="old"> player->location = passage->destination;</li>
<li class="old"> printf("\n");</li>
<li class="new"><span class="old"> </span>executeLookAround();</li>
<li class="old"> }</li>
<li class="old">}</li>
<li class="old"></li>
<li class="new">bool executeGo(void)</li>
<li class="old">{</li>
<li class="new"><span class="old"> OBJECT *obj = getVisible("where you want to go", </span>params[0]);</li>
<li class="old"> switch (getDistance(player, obj))</li>
<li class="old"> {</li>
<li class="old"> case distOverthere:</li>
<li class="old"> movePlayer(getPassage(player->location, obj));</li>
<li class="old"> break;</li>
<li class="old"> case distNotHere:</li>
<li class="new"><span class="old"> printf("You don't see any %s here.\n", </span>params[0]);</li>
<li class="old"> break;</li>
<li class="old"> case distUnknownObject:</li>
<li class="old"> // already handled by getVisible</li>
<li class="old"> break;</li>
<li class="old"> default:</li>
<li class="old"> movePlayer(obj);</li>
<li class="old"> }</li>
<li class="new"><span class="old"> </span>return true;</li>
<li class="old">}</li>
</ol>
</td>
</tr></table>
<p>
Our game still accepts simple verb-noun commands only,
but the new parser does have the potential to accept
commands with more than one noun like ‘put coin in box’;
this will be demonstrated in the next chapter.
</p>
<p>
The new parser is a huge improvement over the original one,
but by today’s standards, it is still far from perfect.
For example, there is no structural way to
manipulate multiple objects with a single command
(‘get coin, key and lamp’)
or execute two or more commands in a row
(‘get key then go east’).
</p>
<p>
In the true sense of the word, my parser is not even a parser.
It is just a simple pattern matcher.
IMHO, it does its job sufficiently well for a simple adventure game.
Some of its flaws can be mitigated by adding more patterns.
But eventually, you will run into its limitations,
and you may want to move on to something more mature.
In that case, I would recommend a decent parser generator
(e.g. <a href="http://en.wikipedia.org/wiki/Yacc">Yacc</a>).
Please be aware that this is not an easy thing to do.
For now, this is outside the scope of this tutorial.
</p>
<hr />
<table class="download"><tr><td>
<a class="button" href="code13/src.zip">⭳ Download source code</a>
<a class="button" href="https://repl.it/github/helderman/htpataic">🌀 Run on Repl.it</a>
</td></tr></table>
<p>
Next chapter: <a href="htpataic14.html">14. Multiple nouns</a>
</p>
</body>
</html>