logo

UTK Notes


Lab 09: Multi-User Dungeons

Overview

In the late 1990s and early 2000s, text-based multi-user games called MUDs (Multi-User Dungeons) were frequented by gamers. While nothing like the 3 dimensional graphic shooters or builders (e.g., Minecraft), these were a way for computer gamers from all over the world to connect to a central server and socialize with others.

You will be designing a very simple MUD that only supports a single user and several rooms.

Assignment

You will be developing one of these text-based games by reading in a single file that contains a variable number of room descriptions (format of the file follows below).

First, your program will need to read a rooms file, whose name will be given as the first user-supplied command line argument. You must dynamically allocate all memory associated with the rooms file. Furthermore, the rooms file must be completely read into memory, and then the file must be closed before the game starts.

After all the rooms are read, you will place the user in the very first room (index 0) and provide them with a command line where they may enter one of six commands:

1
2
3
4
5
q - Quit (closes the program)
l - Look (looks at the room the player is in)
n, e, s, w - Moves the user in the given cardinal direction (north, east, south, or west).
   *If the room does not have an exit in the given direction, you must not move the player, but rather give them
   an error message. There is only ONE exit per direction.

The “look” command will print the room information. An example of this information might be:

1
2
3
4
Short Room Title
Description of Room

Exits: n e s w  ***(Only display the exits that exist in the room)***

The prompt for the user will be a greater than sign (>). You will then wait for the user to provide input until they quit the game using the command “q”.

Rooms File

The rooms file is a single text file that may contain one or more room descriptions. A room has the following description in the rooms file:

1
2
3
4
5
6
7
8
9
10
Room Title
~
Long room description
and further text
that may span one or more lines, but is still
terminated by a tilde '~'
~
exit_direction room_index
exit_direction room_index
~

Each room will follow this format. Another room may follow, so you must keep reading rooms until you exhaust the file. An example room file may be as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
The Room of DOOM
~
This is the first room the player should be in!
Welcome to COSC102!
~
w 1
~
A Gloomy Room
~
This is the second room, which is west of the first room.
That also means that....
The doom room is EAST of this room!
~
e 0
~

Example Interaction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
./mud myroomfile
> l
Room #0
This is the first room the player should be in!
Welcome to COSC102!

Exits: w
> w
You moved WEST.
> l
Room #1
This is the second room, which is west of the first room.
That also means that....
Room #0 is EAST of this room!

Exits: e
> e
You moved EAST.
> l
Room #0
This is the first room the player should be in!
Welcome to COSC102!

Exits: w
> e
You can't go EAST!
> q

The following is a test room file with 25 rooms: room1.txt

Restrictions and Hints

  1. You must use new and delete to create the memory to store all of the rooms. There are two versions of new: new and new[] and two versions of delete: delete and delete[]. new creates an instance, whereas new dateType[X] creates a X-element array (i.e. new dataType[5] creates a 5-element array), and gives you a pointer to the first element in the array. If you create a Room struct or class (which I recommend), you would then call an array of 5 Rooms as: Room *roomArrayPtr = new Room[5];

new must be used with delete.

new[] must be used with `delete[].

1
2
3
1. You will need to count the number of rooms in the file before you actually read the rooms into memory that you created with new. Hint to counting the number of rooms: use the tildes (~). If a line is a ~ that separates the title, description, and exits. So the number of rooms is the count of tildes divided by 3.
2. The room indices are in the order that they're read from the file, starting with index 0. The player will start in room index 0 at the start of the game.
3. Always delete memory and close your files when they are no longer needed. Do this as soon as you can.
  1. You may use C++ style strings (#include ). You may also use stringstreams (#include ). Stringstreams would be useful in conjunction with getline() for storing the exit_direction and exit_index.
  2. There is no need to give a “help” prompt to the user. Assume all players know the commands.
  3. You may not use any vectors or resizable data types (other than C++ strings) for this lab. Instead, you must use pointers and dynamic memory to store/manipulate your data (e.g., rooms file).
  4. Warnings might be as bad as errors. Always fix errors AND warnings!
  5. Remember that switch statements exist!
  6. When printing the exits for the room while playing the game, the example above doesn’t show multiple exits. You can list the exit direction with spaces between them, commas between them, or printing “Exit: dir” for each one, or any other way you see fit. I find “Exit: n s e w” to be the easiest way to code up, if all directions have an exit.
  7. You must comment your code, including a header, any TA or student who helped you, and inline-code comments.

Compiling

When testing, the TA will build your program using the following command:

1
g++ -g -O0 -std=c++11 -Wall -o mud mud.cpp

Plagiarism

Please remember that labs are an individual effort. Please review the plagiarism policy on the course syllabus.

Submission

Submit your mud.cpp file. Multiple submissions will rename your lab when you submit to Canvas. This is OK.

WARNING: If your .cpp file does not compile using the command line above, it will not be graded, and thus, it will be graded 0. NO EXCEPTIONS!