Assignment #7 Answers

Intermediate C Programming

UW Experimental College


Assignment #7 ANSWERS

Exercise 2. Rewrite the lockcmd and unlockcmd functions as object functions.

I stated this problem slightly incorrectly. You only need to write one function, which will recognize and implement both the verbs ``lock'' and ``unlock''. You can write the function in two ways, one way so that it can be attached to objects that can be locked and unlocked, and one way so that it can be attached to objects like keys that do the locking and unlocking. Here is the function written so as to be attachable to lockable objects:

int lockfunc(struct actor *player, struct object *objp,
					struct sentence *cmd)
{
	if(strcmp(cmd->verb, "lock") != 0 && strcmp(cmd->verb, "unlock") != 0)
		return CONTINUE;
	if(objp != cmd->object)
		return CONTINUE;
	if(cmd->preposition == NULL || strcmp(cmd->preposition, "with") != 0 ||
			cmd->xobject == NULL)
		{
		printf("You must tell me what to %s with.\n", cmd->verb);
		return FAILURE;
		}
	if(!hasattr(cmd->object, "lock"))
		{
		printf("You can't lock the %s.\n", cmd->object->name);
		return FAILURE;
		}
	if(Isopen(cmd->object))
		{
		printf("The %s is open.\n", cmd->object->name);
		return FAILURE;
		}
	if(!contains(player->contents, cmd->xobject))
		{
		printf("You don't have the %s.\n", cmd->xobject->name);
		return FAILURE;
		}
	if(!hasattr(cmd->xobject, "key"))
		{
		printf("The %s won't %s the %s.\n",
				cmd->xobject->name, cmd->verb, objp->name);
		return FAILURE;
		}

	if(strcmp(cmd->verb, "lock") == 0)
		{
		if(hasattr(cmd->object, "locked"))
			{
			printf("The %s is already locked.\n", cmd->object->name);
			return FAILURE;
			}

		setattr(cmd->object, "locked");
		printf("The %s is now locked.\n", cmd->object->name);
		return SUCCESS;
		}
	else if(strcmp(cmd->verb, "unlock") == 0)
		{
		if(!hasattr(cmd->object, "locked"))
			{
			printf("The %s is already unlocked.\n", cmd->object->name);
			return FAILURE;
			}

		unsetattr(cmd->object, "locked");
		printf("The %s is now unlocked.\n", cmd->object->name);

		return SUCCESS;
		}

	/* shouldn't get here */
	return CONTINUE;
}
Here it is written so as to be attachable to keylike objects:
int keyfunc(struct actor *player, struct object *objp,
					struct sentence *cmd)
{
	if(strcmp(cmd->verb, "lock") != 0 && strcmp(cmd->verb, "unlock") != 0)
		return CONTINUE;
	if(objp != cmd->xobject)
		return CONTINUE;
	if(cmd->object == NULL)
		{
		printf("You must tell me what to %s.\n", cmd->verb);
		return FAILURE;
		}
	if(!hasattr(cmd->object, "lock"))
		{
		printf("You can't %s the %s.\n", cmd->verb, cmd->object->name);
		return FAILURE;
		}
	if(Isopen(cmd->object))
		{
		printf("The %s is open.\n", cmd->object->name);
		return FAILURE;
		}
	if(!contains(player->contents, cmd->xobject))
		{
		printf("You don't have the %s.\n", cmd->xobject->name);
		return FAILURE;
		}

	if(strcmp(cmd->verb, "lock") == 0)
		{
		if(hasattr(cmd->object, "locked"))
			{
			printf("The %s is already locked.\n", cmd->object->name);
			return FAILURE;
			}

		setattr(cmd->object, "locked");
		printf("The %s is now locked.\n", cmd->object->name);
		return SUCCESS;
		}
	else if(strcmp(cmd->verb, "unlock") == 0)
		{
		if(!hasattr(cmd->object, "locked"))
			{
			printf("The %s is already unlocked.\n", cmd->object->name);
			return FAILURE;
			}

		unsetattr(cmd->object, "locked");
		printf("The %s is now unlocked.\n", cmd->object->name);

		return SUCCESS;
		}

	/* shouldn't get here */
	return CONTINUE;
}

Exercise 4. Invent a (silly) shower object.

int showerfunc(struct actor *player, struct object *objp,
					struct sentence *cmd)
{
	if(strcmp(cmd->verb, "take") != 0)
		return CONTINUE;
	if(objp != cmd->object)
		return CONTINUE;

	printf("After spending 20 luxurious minutes in the shower,\n");
	printf("singing three full-length show tunes, and using up\n");
	printf("all the hot water, you emerge clean and refreshed.\n");

	return SUCCESS;
}

Exercise 5. Implement a magic sword object.

int swordfunc(struct actor *player, struct object *objp,
					struct sentence *cmd)
{
	if(strcmp(cmd->verb, "take") != 0)
		return CONTINUE;
	if(objp != cmd->object)
		return CONTINUE;
	printf("As you pick it up, the sword briefly glows blue.\n");
	return CONTINUE;
}

Exercise 6. Implement a container of radioactive waste.

Here are sample descriptions for dungeon.dat:

object barrel
desc It is a large yellow barrel with a funny symbol and
     the word "HAZARDOUS" on it.
func wastebarrelfunc
object suit
desc It is a hooded suit made out of some strange plasticlike yellow fabric.
func hazmatsuitfunc
Here is the waste barrel function:
int wastebarrelfunc(struct actor *player, struct object *objp,
					struct sentence *cmd)
{
	struct object *hazmatsuit;

	if(strcmp(cmd->verb, "take") != 0)
		return CONTINUE;
	if(objp != cmd->object)
		return CONTINUE;

	hazmatsuit = findobject(player, "suit");
	if(hazmatsuit == NULL || !contains(player->contents, hazmatsuit))
		{
		printf("The container is far too dangerous to pick up.\n");
		return FAILURE;
		}

	return CONTINUE;
}
Here is a function so that you can wear the Hazmat suit:
int hazmatsuitfunc(struct actor *player, struct object *objp,
					struct sentence *cmd)
{
	if(strcmp(cmd->verb, "wear") != 0)
		return CONTINUE;
	if(objp != cmd->object)
		return CONTINUE;
	/* implicitly pick up, if necessary */
	if(!contains(player->contents, cmd->object))
		{
		if(!takeobject(player, cmd->object))
			return FAILURE;
		}
	printf("You feel much safer wearing the bright yellow Hazmat suit.\n");
	return SUCCESS;
}


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