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 (between 1 and 500), the number of doors (between 1 and 500), the number of keys (between 0 and 500) and the number of creatures (between 1 and 500):
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..


Here is a very small input example, together with appropriate output (input is bolded to make a visual distinction below):
3 2 1 1
0 1 1
0 2 0
1 1
0 0 0
1 0
1
Please enter a command:
look
You are in room 0. There are no items here. There are no creatures except you here. There is an unlocked door 0 going to room 1, and a locked door 1 going to room 2. Door 1 leads to the exit!
Please enter a command:
pass 1
Door 1 is locked. You can't go through it.
Please enter a command:
unlock 1
You don't have the key to door 1.
Please enter a command:
inv
You are not carrying anything. Your carrying capacity is 1.
Please enter a command:
pass 0
You go through door 0 and enter room 1.
Please enter a command:
look
You are in room 1. It contains the following items: key 0 to door 1. There are no creatures except you here. There is an unlocked door 0 going to room 0.
Please enter a command:
pickup 0
You pick up key 0 to door 1.
Please enter a command:
inv
You are carrying key 0 to door 1. Your carrying capacity is 1.
Please enter a command:
unlock 1
There is no door 1 here.
Please enter a command:
pass 0
You go through door 0 and enter room 0.
Please enter a command:
unlock 1
You use key 0 to unlock door 1.
Please enter a command:
pass 1
You go through door 1, and you are outside. Congratulations, you won!


Here is another small example that tests another part of the homework functionality.
4 3 0 3
0 1 1
0 2 1
2 3 1
0 0 0
1 1 0
2 2 1
0 1
2
Please enter a command:
look
You are in room 0. There are no items here. There are no creatures except you here. There is an unlocked door 0 going to room 1, and an unlocked door 1 going to room 2.
Please enter a command:
pass 1
You go through door 1 and enter room 2.
Room 2 has at least as many guards as escapees in it now. It has the PC in it and also guard 2 in it, who targets creature 1!
You were caught! Back to your starting location.
You were caught 1 times already. Careful!
Please enter a command:
look
You are in room 0. There are no items here. There are no creatures except you here. There is an unlocked door 0 going to room 1, and an unlocked door 1 going to room 2.
pass 0
You go through door 0 and enter room 1.
Please enter a command:
look
You are in room 1. There are no items here. In addition to you, there is escapee 1 here. There is an unlocked door 0 going to room 0.
Please enter a command:
lead 1
Creature 1 starts following you.
Please enter a command:
pass 0
You go through door 0 and enter room 1.
Please enter a command:
pass 1
You go through door 1 and enter room 2.
Room 2 has more escapees (including you) than guards. The guards are waiting for you to make a mistake.
Guard 2 starts following escapee 1, who is following you.
look
You are in room 2. There are no items here. In addition to you, there is escapee 1, and guard 2 who targets escapee 1 here. There is an unlocked door 1 going to room 0, and an unlocked door 2 going to room 3. Door 2 leads to the exit!
Escapee 1 is following you. Guard 2 is following escapee 1.
Please enter a command:
pass 2
You go through door 2, and you are outside. Congratulations, you won!


Here is a third example with a very small world that people thought was interesting.
300 1 0 1
200 30 1
229 0 0
10 10
0
Please enter a command:
look
You are in room 229. There are no items here. There are no creatures except you here. There are no doors here.
Please enter a command: