|
Due: 5pm, Friday, March 4th Worth: 100 points |
|
Goals
- to build and use circular doubly-linked lists
- to build a multi-file program
Overview
Write a program in C++ that uses a doubly-linked circular linked list
to simulate a game of musical chairs.
You will build the doubly-linked node class as well as the simulation.
Be sure to read the input correctly and move through the list as specified.
There is no audible music in this simulation.
Specification
The first two inputs will be positive integers. If they are not at least 1,
print an error message and stop.
The first input is a seed for the random number generator.
The second input is the number of seconds in the song for the simulation.
The rest of the input is one word names of players for the simulation.
It is terminated by end of file.
The sample input below would create the initial list shown in the picture:
42 13
Mathew Helen Claire Andrew Mark Nicole Micah Patrick
Use the provided Random class to create the random number generator with the seed provided by the user. Use that random number generator to choose a value between 1 and the length of the song. In the example, the song length is 13 seconds; therefore, the generated numbers would be between 1 and 13. In this example, the series of numbers generated starts with 8, 4, 6, and 9.
Use that number to determine which player will not have a chair when the song is stopped. That player is eliminated from this round of the game and is added to the end of the eliminated list. Start counting at 1 for the player currently at the front of the list. The player that was after the eliminated player becomes the new front of the list.
In the example, in round 1, Mathew is at front and we remove the 8th player - Patrick. In round 2, Mathew is still at the front, and we remove the 4th player - Andrew. In round 3, Mark is now at the front, and we remove the 6th player - Claire. The picture at right shows the list after round 3. In the next round, the 9th player - Mathew - is removed which leaves Helen at the front.
Each time a player is eliminated, they are added to the end of an eliminated list. In the example at the end of round 3, that list would contain Patrick, Andrew, and Claire. The final player eliminated is the winner and should be added to the end of the eliminated list. That way, all players are in one list ready for the next round if the players shout, "Again!"
Printed output for the sample is at the end of this handout.
Your output must include:
- the initial seed value and the song length value with labels
- a space separated list of the players in order of input
for each round until there is only one player playing ...
- the ordinal number of the player eliminated and their name (1th, 2th, and 3th are okay)
- a space separated list of the players still playing with the current front player first
- the name of who won (only person in list when others have been eliminated)
- a space separated list of players in the order in
which they were eliminated with winner at the end
Your output does not need to match the sample's form exactly. However, the order of elimination should match. I ran my solution on a Linux Lab machine to obtain this sample. It matched the answers at home on my Mac.
If your solution is in a.out, you can run it with
the sample data by typing
./a.out < cousins.txt
The sample is only one example. This should work with larger and smaller groups, different seeds, and different length songs. Try to break your program.
When a player is eliminated, the player that was after them becomes the front player. Note that this does not involve copying data or creating more nodes. You have a pointer into the list, and that's where the current front is. In the next round, if the random value indicating which player to eliminate were one or less, that front player would be eliminated.
Other Requirements
You must use a doubly-linked circular linked list to store the lists.
Creating those nodes and keeping the list connected is the major purpose of
this assignment.
You are creating this on your own using resources you already know about. The Random class is is in the class examples directory with both a .h and .cpp file. You have the Node class from the previous assignment that you should modify into what I called a NodeD class (D for doubly). You can download the cousins.txt file by clicking here.
The node class you create needs to have three instance variables to hold the data, pointer to the previous node, and pointer to the next node. Those must be private with get and set operations defined for each of them in the class. There should be const and non-const versions of the getting link functions. The class must remain templated. Update the comments as well. This part is careful detail work that doesn't use problem solving skills.
You should not need to delete any nodes. Your program creates a node for each player as the names are read. That node's pointers are changed as it moves from the playing list to the eliminated list and connects to different nodes, but the node continues to exist. Its data doesn't change.
You must use functions throughout this program. Pass the parameters appropriately. If a parameter shouldn't change, make sure it doesn't. Each function should do one thing well (have high cohesion).
You must use the provided random number generator class and create the instance with the seed from the user. If you don't, you will not get the same answers as I do for a given seed.
Ask questions if you get stuck. Don't put this off. My node class file is 80 lines and my chairs.cpp file is 170 lines. There's thinking and problem-solving here, but it's fun getting the parts to hang together.
You'll submit a whole directory where I'll take in files ending in .h and .cpp. Be inside the directory with your code when you submit.
Complete Sample Output (./a.out < cousins.txt)
Updated output - March 1, 2011
Simulate a game of musical chairs. First two values must be integers greater than zero. Random number seed? Longest song length in seconds? Enter one-word names of players until end-of-file. The seed is 42 and the song length is 13 These are the players: Mathew Helen Claire Andrew Mark Nicole Micah Patrick Mathew at front planning to remove 1th player Mathew was eliminated. These are still playing: Helen Claire Andrew Mark Nicole Micah Patrick Helen at front planning to remove 8th player Helen was eliminated. These are still playing: Claire Andrew Mark Nicole Micah Patrick Claire at front planning to remove 4th player Nicole was eliminated. These are still playing: Micah Patrick Claire Andrew Mark Micah at front planning to remove 6th player Micah was eliminated. These are still playing: Patrick Claire Andrew Mark Patrick at front planning to remove 9th player Patrick was eliminated. These are still playing: Claire Andrew Mark Claire at front planning to remove 13th player Claire was eliminated. These are still playing: Andrew Mark Andrew at front planning to remove 9th player Andrew was eliminated. These are still playing: Mark Mark won!!! The players were eliminated in this order: Mathew Helen Nicole Micah Patrick Claire Andrew Mark