Handouts:
Assignment #3
Assignment #2 Answers
Class Notes, Chapter 18
Class Notes, Chapter 19
Exercises:
hit nail with hammerThe new scheme involves this structure, in game.h:
struct sentence { char *verb; struct object *object; char *preposition; struct object *xobject; /* object of preposition */ };This structure can contain the verb, object, and prepositional phrase of one of these sentences. (Also, the object of the verb and the object of the preposition are represented by object pointers, filled in by the command parser, rather than simple object names.)
struct sentence cmd;which can contain the whole command, and change the calls to parseline and docommand to
if(!parseline(&player, line, &cmd)) continue; docommand(&player, &cmd);You'll also need to change the prototype declarations for parseline and docommand in game.h.
else if(strcmp(verb, "hit") == 0) { if(objp == NULL) { printf("You must tell me what to hit.\n"); return FALSE; } if(cmd->preposition == NULL || strcmp(cmd->preposition, "with") != 0 || cmd->xobject == NULL) { printf("You must tell me what to hit with.\n"); return FALSE; } if(!contains(player->contents, cmd->xobject)) { printf("You don't have the %s.\n", cmd->xobject->name); return FALSE; } printf("The %s says, \"Ouch!\"\n", objp->name); }Add this command, too.
char *plural(char *);which, given a string, returns a new string with an ``s'' tacked on to the end. Use this new function to rewrite some of the messages printed by the game from
printf("I see no %s here.\n", word);to
printf("I don't see any %s.\n", plural(word));Make an appropriate choice from among the techniques discussed in class for the allocation of the string returned by plural. (Extra credit: try to make plural a little smarter, by adding ``es'' where appropriate.)
This page by Steve Summit // Copyright 1995-9 // mail feedback