In this assignment you will be Jeff Probst. Your job, Jeff, is to implement a game of Survivor. Our version of survivor gathers several castaways together in a wilderness. At first, they're split into two tribes. After a brief initial challenge, one tribe will be declared the initial winner.
Then there's a second round, where members of the winning tribe compete against one another. Two people go head-to-head in a fierce competition. The loser of each challenge gets voted off the island, and the other one stays. The season is over when there is one single castaway left in the tribe. That person is then declared the overall winners and gets a big fat cash prize.
You're going to make this all work using trees. The heart of your code will be a BinarySearchTree class that inherits from the Binary Tree class provided in the starter code. (Youll need to add more to the Binary Tree class as well.)
In Tree.h, we've defined a BinaryNode struct. It has three attributes: a TreeItem, a left pointer and a right pointer. Whats a TreeItem? Well, in this case it should be a Survivor object. You can use "typedef" so that anytime a TreeItem is referred to in your code its tied to Survivor. (Jays Note: need to understand what typedef is and the proper usage.)
The starter code includes the following member functions. Most of them have corresponding non-member wrapper functions to handle the recursion, as we saw in class.
The Binary Tree class uses a Queue as an auxiliary data structure for some of its operations. This is only in the starter code and not for anything you'll need to write. The BST class doesnt need an auxiliary data structure at all, thanks to its invariant of keeping smaller things to the left and larger things to the right. Put the queues out of your mind entirely while youre working on the project, and well revisit them later in lecture.
You will need to modify the given BinaryTree class to include the following functions. Write wrapper functions to handle recursion whenever necessary. Any member function that does not modify the attributes of the tree object should be declared const.
The starter code includes the following member functions. The starter code includes the following member functions.
You'll finish writing the class for a BinarySearchTree whose elements are TreeItems containing objects of the Survivor class. This will inherit from the BinaryTree class, so you are responsible for overwriting the find and remove functions inherited from the BinaryTree.
You will need to modify the given BinaryTree class to include the following functions. Write wrapper functions to handle recursion whenever necessary.
Any member function that does not modify the attributes of the tree object should be declared const.
This class will keep track of the Survivor contestants and is similar to the Pirate class from HW 1.
The Survivor class should keep track of the following with private member variables:
Here are the functions you'll need to write. You can also add any other functions or attributes you need to make your program work.
(Jay’s Note: below is the generate_next_pirate() function mentioned above)
// in the Pirate.h file:
class Pirate
{
public:
Pirate();
Pirate(string);
void print(ofstream &) const;
void generate_pirate_name(ifstream &, int);
void assign_pirate_id();
// Member function to add pirates in order.
void generate_next_pirate(ifstream &);
friend bool operator == (const Pirate &, const Pirate &);
private:
int pirate_id;
string name;
static int pirate_count;
};
// in the Pirate.cpp file:
void Pirate::generate_next_pirate(ifstream &infile)
{
string pname;
int count = 0;
infile.clear();
infile.seekg(0, ios::beg);
while (getline(infile, pname))
{
if (count == pirate_count - 1)
{
name = pname;
break;
}
count++;
}
}
// in the int main() file:
int all_pirates_smee(PirateMates hookbook[], Pirate smee)
{
int counter = 0;
// Make an ArrayList out of every Pirate in the txt file
ifstream infile;
infile.open(PIRATE_FILE.c_str());
if (!infile.is_open())
{
cerr << "Could not add pirates from filen";
return -1;
}
while (!infile.eof() && counter < ALL_PIRATES)
{
Pirate p;
p.generate_next_pirate(infile);
p.assign_pirate_id();
hookbook[counter].pirate = p;
hookbook[counter].mates.insert_pirate(smee);
counter++;
}
infile.close();
}
You need to create a driver to run the logic of your game, survivor-driver.cpp. You'll have two input files to read, castaways.txt and stowaways.txt. One input file will become a Binary Tree and the other will become a BST (which is which, you ask? Well, thatll be randomly chosen).
This will run your game according to the following structure.
You can add any functions you need to your driver to make this happen.
You must submit a Makefile along with your source code and header files. You should be able to compile with the command "make survivor" and generate an executable called survivor. These names are important, because our grading scripts will use them.
Here's a sample output (please note the spacing, well use a script to check so be careful that it matches, also note that we ran this on a totally different input file than youll see in starter code!). We also arent expecting any kind of precision setting in doubles.
Loser had 9 leaves
ROUND ONE WINNER: BST
Maralyn Hershey
hometown: Wakefield, VA
age: 51
Height: 6
Number of nodes: 18
Number of leaves: 5
Average age this round 34.5
Voted off the island:
Tina Wesson
hometown: Knoxville, TN
age: 39
Average age this round 34.2353
Voted off the island:
Jeff Varner
hometown: NewYork, NY
age: 34
Average age this round 33.6
Voted off the island:
Michael Skupin
hometown: WhiteLake, MI
age: 38
WINNING SURVIVOR:
Rodger Bingham
hometown: Crittenden, KY
age: 53