Assignment #3 Answers

Intermediate C Programming

UW Experimental College


Assignment #3 ANSWERS

Exercise 2. Add some other commands, such as ``break'' or ``cut'' or ``read''.

Here is the code I added (to commands.c, of course) to implement all three commands. They are all straightforward but quite boring, because they don't really do anything yet. (Soon we'll be able to make them more interesting.)

else if(strcmp(verb, "break") == 0)
	{
	if(objp == NULL)
		{
		printf("You must tell me what to break.\n");
		return FALSE;
		}
	if(cmd->preposition == NULL || strcmp(cmd->preposition, "with") != 0 ||
			cmd->xobject == NULL)
		{
		printf("You must tell me what to break with.\n");
		return FALSE;
		}
	if(!contains(player->contents, cmd->xobject))
		{
		printf("You have no %s.\n", cmd->xobject->name);
		return FALSE;
		}
	printf("Oh, dear.  Now the %s is broken.\n", objp->name);
	}

else if(strcmp(verb, "cut") == 0)
	{
	if(objp == NULL)
		{
		printf("You must tell me what to cut.\n");
		return FALSE;
		}
	if(cmd->preposition == NULL || strcmp(cmd->preposition, "with") != 0 ||
			cmd->xobject == NULL)
		{
		printf("You must tell me what to cut with.\n");
		return FALSE;
		}
	if(!contains(player->contents, cmd->xobject))
		{
		printf("You have no %s.\n", cmd->xobject->name);
		return FALSE;
		}
	printf("The %s is now cut in two.\n", objp->name);
	}

else if(strcmp(verb, "read") == 0)
	{
	if(objp == NULL)
		{
		printf("You must tell me what to read.\n");
		return FALSE;
		}

	printf("There isn't much to read on the %s.\n", objp->name);

	}

Exercise 3. Write a function plural so that the game can print slightly more interesting messages.

Here is my first plural function:

#include <string.h>
#include "game.h"

#define MAXRETBUF 20

char *
plural(char *word)
{
static char retbuf[MAXRETBUF];
strcpy(retbuf, word);
strcat(retbuf, "s");
return retbuf;
}
Notice that the retbuf array is declared static, so that it will persist after plural returns, so that the pointer that plural returns will remain valid.

Here is the change to (the newer version of) parser.c:

if(ac < 2)
	cmd->object = NULL;
else if((cmd->object = findobject(actor, av[1])) == NULL)
	{
	printf("I don't see any %s.\n", plural(av[1]));
	return FALSE;
	}
(The only change is to the printf call. You could also make a similar change to the code just below which checks for the presence of the object of the preposition, if any.)

To keep my compiler happy, I also added the prototype

	extern char *plural(char *);
to game.h.

Finally, here is an improved version of the plural function. If the last character of the word being pluralized is s, it tacks on ``es''. (Unfortunately, if the word is already plural, this means that it ends up changing ``marbles'' to ``marbleses,'' and sounding like Gollum.) Since this version has to compute the length of the original word anyway, it's easy to make sure that it won't overflow the return buffer. (In this case, since plural's function is relatively unimportant, I chose to have it return the original word, unpluralized, rather than printing error messages or aborting or anything.)

#include <string.h>
#include "game.h"

#define MAXRETBUF 20

char *
plural(char *word)
{
static char retbuf[MAXRETBUF];
int len = strlen(word);
if(len + 3 >= MAXRETBUF)
	return word;
strcpy(retbuf, word);
if(word[len-1] != 's')
	strcat(retbuf, "s");
else	{
	/* It ends in s. */
	/* maybe we should add "es" */
	strcat(retbuf, "es");
	/* (or maybe it'a already plural and we should add nothing...) */
	}
return retbuf;
}


This page by Steve Summit // Copyright 1995-9 // mail feedback