Assignment #3

Intermediate C Programming

UW Experimental College

Assignment #3


Assignment #3
Assignment #2 Answers
Class Notes, Chapter 18
Class Notes, Chapter 19


  1. Appended to this assignment are listings of new versions of commands.c and parser.c. (I've only printed the first page of commands.c, because the rest doesn't change.) These versions implement a slightly more sophisticated scheme for parsing and representing commands; they will handle sentences like
    	hit nail with hammer
    The 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.)

    Replace your copies of parser.c and commands.c with these new ones, which can be found in the week3 subdirectory. Merge any of the commands you added (or other changes you made) from your old commands.c into the new one. In main.c, replace the declarations of the variables verb and object with the new variable
    	struct sentence cmd;
    which can contain the whole command, and change the calls to parseline and docommand to
    	if(!parseline(&player, line, &cmd))
    	docommand(&player, &cmd);
    You'll also need to change the prototype declarations for parseline and docommand in game.h.

    Although we can now parse these more-complicated commands, we need a bit more machinery before we can implement any of them. For now, here is a little ``hit'' command (to be added to commands.c) which will begin to give you a feel for what we'll be able to do:
    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.
  2. Add some other commands, such as ``break'' or ``cut'' or ``read''. (They won't really do anything yet, other than print little messages along the lines of the ``hit'' command in exercise 1.)
  3. Write a function plural with the prototype
    	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);
    	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