Saint Louis University |
Computer Science A220/P126
|
Dept. of Math & Computer Science |
Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.
For this assignment, you are allowed to work with one other student if you wish (in fact, we suggest that you do so). If any student wishes to have a partner but has not been able to locate one, please let the instructor know so that we can match up partners. You will note that there are two distinct implementation approaches required for this assignment. It may be that the partnership divides the work by having one person do one implementation, and one person another (with consultation as needed). Alternatively both pieces could be developed side-by-side.
Please make sure you adhere to the policies on academic integrity in this regard.
Apparently, you cannot run a network these days without the typical "reality" show (e.g., "Survivor," "Big Brother," "The Bachelor," "The Apprentice," ad nauseum). Each of these invovles the basic premise of starting with a group of people, and one-by-one removing people until only one remains. This, combined with a million dollars and some clever marketing appears to be a formula for success.
In this assignment, we are going to make a program which can be used for modeling these games and deciding whom to remove at each stage. Specifically, we will use a classic children's method for selecting a person out of a group. The people are arranged in a circle, and then someone starts pointing to the people one by one around the circle, while chanting the phrase
"Eeny-meeny-miney-moe, catch a tiger by the toe, if he hollers let him go, eeny-meeny-miney-moe. My mother told me to pick the very best one and you are not it."Whomever is identified when the last word of the phrase is completed is eliminated from the game (and thus never counted again). The phrase is restarted with the neighbor of that person most recently eliminated, and again a second person is removed based on the end of the phrase. A third and fourth player are removed in this way, continuing until there is only one person remaining. That person is the survivor!
The main parameters in modeling the game are the number of original people in the group, and the number of syllables used in the phrase for removing people. In this assignment, we will let N denote the number of players in the game and K denote the number of consectutive steps which are taken around the circle before the elimination of a player. Once a person has been removed from the group, they should never be counted again when counting steps.
For consistency, you should refer to the original players as if they
are numbered from {0, ..., N-1}. Also, please start the game
so that the first step is at Player 0. As a simple example, if we
play the game with N=5 and K=3, players would be removed in the
following order:
Player 2 is removed.
Player 0 is removed.
Player 4 is removed.
Player 1 is removed.
Player 3 has survived!
Please simulate the above example by hand. If you do not get
exactly the same result as shown then you are misinterpreting the
conventions for this assignment.
For this assignment, you will be required to give two different implementations for simulating an abstract class game:
Maintain an array of booleans which will be used to represent whether or not a particular person has been eliminated. After initializing this array, you begin by removing people according to the rules of the game. You will need to take care when counting K steps, as you are not to count any players who had been removed in an earlier round.
Use what is termed a circular singly linked list to represent the circle of players. A standard linked lists has a beginning and an end. In a circularly-linked list there is no beginning or end, rather the next pointers are linked to create a cycle. (of course you will still need to maintain a variable which references some node of the list so that you will be able to find the list!) In this implementation, rather than using a boolean to mark players as removed, you must actually remove each associated node from the linked list. In this way, when you later want to take K steps in the game, you will know that all of the players in the linked list are still part of the game.
The precise details of the required classes are outlined. in the following section.
All such files can be downloaded here.
Game.h
This file formally defines the abstract class Game. In
particular, it includes the following specifications:
Subclasses should include a constructor with two, non-negative integer parameters, for specifying N and K, and should leave the game in its initial state with all players remaining, and player 0 the next to be 'counted' when a round begins.
Returns the number of players who currently remaining in the game.
Plays a single round, eliminating one player. This method can be called repeatedly to simulate several consecutive rounds of the game.
If there is only one player in the game, throws a GameOverException, rather than removing the last player.
The return value is The ID of the removed player.
Completes the entire game, from the current state. (i.e. equivalent to calling playRound repeatedly until size is one).
The return value should be the ID of the sole surviving player.
This method toggles the 'verbose' setting. The return value indicates the new setting.
The default behavior should be with verbose set to false, in which case no output is to be generated by the other methods.
If verbose is set to true, subsequent steps of the game should produce a message, printed to the standard output stream, to document the play of the game.
This method may be called more than once, if setting changes are desired while a game is in progress.
ArrayGame.h,
ArrayGame.cpp
These files provide you with a starting point for implementing
the ArrayGame class, though they are incomplete.
LinkedGame.h,
LinkedGame.cpp
These files provide you with a starting point for implementing
the LinkedGame class, though they are incomplete.
Survivor.cpp
This file provides a main driver to be used in testing your
program. You will not need to modify this code.
makefile
This makefile should allow you to rebuild your project by
simply typing 'make' rather than in invoking the compiler
directly.
When the completeGame command is executed through our driver, it reports the amount of running time which was spent during the call to your routine. We would like you to use this in order to run some experiments regarding the relative efficiency of the two implementations.
For evaluating running time, always test your program with verbose set to false. Output to the screen would signficantly skew the measurements of the underlying efficiency of your methods.
Your readme file for this assignment should include two tables, one for each implementation, reporting the running times for each of the following cases:
Source Code
Please submit all source code files which you have either
modified or added. At minimum, we expect this to include the
four files (ArrayGame.h,
ArrayGame.cpp,
LinkedGame.h,
LinkedGame.cpp).
One good test is to make sure that both implementations actually give the same output when run with identical parameters. Secondly, you should certainly test your programs versus the example given earlier in this assignment. If you would like a few more answers to compare to, here is the survivor for several other cases:
The assignment is worth 10 points.
Chances are, though you may feel that your code is already complete and works properly, it may be that it has an inherent memory leak which is going unnoticed! Since I expect that your ArrayGame and LinkedGame classes will both involve the allocation of memory to represent a game, you must remember the lessons of Chapter 1.5.3 of the text.
For extra credit, redefine both your array and linked classes so that there exists an appropriate destructor, copy constructor, and overloaded assignment operator. Please note that the initial files we provided to get you started are incomplete, and do not contain the necessary definitions for these tasks.
To test the correctness of your extra credit implementations, you should create a new game, play some of the rounds but not all of the rounds, then invoke the "X" option in the menu-driven driver. This command begins a series of steps to clone the current game, presumably making a brand new game which has identical state as the original game. Then it intentionally destroys the original game and uses the clone as the current game from this point on. If you've done your extra credit tasks correctly, you should be able to comlete the current game at this point and it will look just like you are completing the original game.
For example, run the driver with the following input,
L
5
3
V
P
P
X
C
and you should see verbose results similar to the example given
earlier in this assignment.
You can receive 1 point for correctly adding this functionality to your ArrayGame class, and 1 point for the LinkedGame class.