CSC322: Homework 1


This is your C lab. It consists of programming a small text-based game / virtual world simulation purely in C, loosely inspired by Dan Schlegel's CSC241 project, which in turn was loosely inspired by mine.

The world consists of a series of rooms, joined by doors. A room can have multiple doors to other rooms. Every door can be locked or not. A room can also contain keys and creatures.

The player acts as a person (the player character, or PC) trying to escape the maze of rooms. The PC has a carrying capacity and a leadership parameter. The carrying capacity, which is an integer, indicates how many keys the PC can carry; the leadership parameter, which is another integer, indicates how many escapees can follow the PC at the same time.

Every key can be used to unlock precisely one specific door.

There are two kinds of other creatures in the maze: escapees and guards. Every guard has a target escapee he must ensure does not escape (this can be the PC). Whenever one or more escapees encounter one or more guards in the same room, the following happens:
- if there are equal or more guards than escapees, all creatures (including the PC, escapees, and guards) are returned to their original locations. Carried items are not confiscated. The PC can be returned to the original location no more than 5 times; if the PC is caught for the fifth time, the game is lost.
- if there are more escapees than guards, any guards whose targets are in the group of escapees start following the escapees in the hope that they'll encounter more guards. Other guards do not follow.

The goal of the game is for the PC to reach a specific door, which is the exit.

Input

When the program starts, it takes from stdin (so no files are opened/closed/used in the project code) input in the format described below.

There are two blocks of input: a description of the world, followed by the game starting and the human player entering commands.

The first line of input will contain four integers, separated by a space, indicating the number of rooms, the number of doors, the number of keys and the number of creatures (excluding the PC):
r d k c

The next d lines contain three integers each, which fully describe the door with the respective ID (from 0 to d-1). The first integer indicates the ID of the room on one side of the door; the second integer indicates the ID of the room on the other side of the door; and the third integer can only be 1 if the door is unlocked, and 0 if the door is locked.

The next k lines contain two integers each, which fully describe the key with the respective ID (from 0 to k-1). The first integer indicates the ID of the room the key is in, and the second integer indicates the ID of the door the key opens.

The next c lines contain three integers each, which fully describe the creature with the respective ID (from 0 to c-1). The first integer indicates the ID of the room the creature is in; the second integer indicates the type of creature: 0 for the PC, 1 for an escapee, and 2 for a guard; and the third integer signifies the ID of the target of the guard (you can ignore the third integer for other types of creatures).

The next line contains two integers for the PC: the first one is its carrying capacity, and the second one is its leadership.

Finally, the last line of the world description contains a single integer: the ID of the door that the PC must reach to escape.

Simulation

Once the world description is input and processed, the game starts. Your program should enter a loop that takes a command from the player and processes it. Possible commands:

Make sure you describe what is going on after each command (which includes whether the command was successful and any results from it), and that you are checking for the victory condition (the PC goes through the escape door) and the defeat condition (the PC is caught for the fifth time).

Implementation

You must not use global memory, except you are allowed to use up to three global pointers. Also, all arrays, structs, and other large entities should be placed on the heap. Remember to do your own memory management for anything placed on the heap. You may use the stack only for loop counters, pointers, and the like. The one exception is a potential buffer you would need for reading string commands -- that can be on the stack.

Submit your source file(s) to the dropbox. Your code should compile with the gcc on pi. If it requires any command-line parameters passed to gcc when compiling, specify them in the comment box when you make your submission.

Do not underestimate the project. It is sufficiently large to require all the time you have. Start early in order to finish on time.

Once you submit, you will have to demonstrate the working project to me in person. We'll schedule presentation slots for that purpose as necessary..