Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.cpp
- *
- * Created on 19 January 2013, 17:48
- */
- //#include <cstdlib>
- #include <iostream>
- #include <string>
- #include <time.h>
- #include <vector>
- enum colour{
- white = 0,
- brown,
- black,
- spotted
- };
- class Bunny{
- private:
- bool isMale;
- colour appearance;
- unsigned int age;
- std::string name;
- bool radioactive_mutant_vampire_bunny;
- public:
- Bunny();
- Bunny(std::string c);
- std::string GetIsMale();
- std::string GetColour();
- int GetAge();
- std::string GetName();
- std::string GetVampire();
- void ModifyAge(int a);
- void ModifyVamp(bool v);
- friend bool operator== (Bunny &b1, Bunny &b2);
- friend bool operator!= (Bunny &b1, Bunny &b2);
- };
- bool operator== (Bunny &b1, Bunny &b2){
- return (b1.age == b2.age && b1.appearance == b2.appearance && b1.isMale == b2.isMale && b1.name == b2.name && b1.radioactive_mutant_vampire_bunny == b2.radioactive_mutant_vampire_bunny);
- }
- bool operator!= (Bunny &b1, Bunny &b2){
- return !(b1 == b2);
- }
- class List{
- private:
- typedef struct node{
- Bunny data;
- node *next;
- }* nodePtr;
- nodePtr head;
- nodePtr curr;
- nodePtr temp;
- public:
- List();
- void AddNode(Bunny addData);
- void DeleteNode(Bunny delData);
- void PrintList();
- void AddAge(int a);
- int Reaper(int maxAge);
- int VReaper(int maxAge);
- int Reproduction(int age);
- int Vampire();
- bool CheckHead();
- };
- List::List(){
- head = NULL;
- curr = NULL;
- temp = NULL;
- }
- void List::AddNode(Bunny addData){
- nodePtr n = new node;
- n->next = NULL;
- n->data = addData;
- if (head != NULL){
- curr = head;
- while (curr->next != NULL){
- curr = curr->next;
- }
- curr->next = n;
- }
- else{
- head = n;
- }
- }
- /*not currently used
- void List::DeleteNode(Bunny delData){
- nodePtr delPtr = NULL;
- temp = head;
- curr = head;
- while(curr != NULL && curr->data != delData){
- temp = curr;
- curr = curr->next;
- }
- if (curr == NULL){
- std::cout<<"Could not delete Bunny check DeleteNode.\n";
- delete delPtr;
- }
- else{
- delPtr = curr;
- curr = curr->next;
- temp->next = curr;
- if (delPtr == head){
- if (head->next != NULL){
- head = head->next;
- temp = NULL;
- }
- else{
- head = NULL;
- temp = NULL;
- }
- }
- std::cout<<"Bunny "<< delData.GetName()<<" died :(\n";
- delete delPtr;
- }
- }
- */
- void List::PrintList(){
- curr = head;
- std::cout<<"Bunny List:\n";
- while(curr != NULL){
- std::cout<<"------------------------------\n";
- std::cout<<"Name: "<<curr->data.GetName()<<"\n";
- std::cout<<"Age: "<<curr->data.GetAge()<<"\n";
- std::cout<<"Colour: "<<curr->data.GetColour()<<"\n";
- std::cout<<"Gender: "<<curr->data.GetIsMale()<<"\n";
- std::cout<<"Abnormalities: "<<curr->data.GetVampire()<<"\n";
- std::cout<<"------------------------------\n";
- curr = curr->next;
- }
- }
- void List::AddAge(int a){
- if (head != NULL){
- curr = head;
- while (curr->next != NULL){
- curr->data.ModifyAge(a);
- curr = curr->next;
- }
- curr->data.ModifyAge(a);
- }
- else
- curr->data.ModifyAge(a);
- }
- int List::Reaper(int maxAge)
- {
- if( !head ) return 0;// list is empty
- int deadCount = 0;// return value
- // start at head
- while(head->data.GetAge() >= maxAge && head->data.GetVampire() == "No abnormalities")
- {
- temp = head;
- std::cout<<"Bunny "<<head->data.GetName()<<" died\n";
- head = head->next;
- delete temp;
- ++deadCount;
- if( !head )
- return deadCount;//all bunnies dead
- }
- // continue to end
- nodePtr iter = head;
- while(iter->next)
- {
- if( iter->next->data.GetAge() >= maxAge && iter->next->data.GetVampire() == "No abnormalities")
- {
- temp = iter->next;// iter->next will be deleted
- iter->next = iter->next->next;// linking across node to be deleted
- delete temp;
- ++deadCount;
- }
- else iter = iter->next;
- }
- return deadCount;
- }
- int List::VReaper(int maxAge)
- {
- if( !head ) return 0;// list is empty
- int deadCount = 0;// return value
- // start at head
- while(head->data.GetAge() >= maxAge && head->data.GetVampire() != "No abnormalities")
- {
- temp = head;
- std::cout<<"Vampire Bunny "<<head->data.GetName()<<" died\n";
- head = head->next;
- delete temp;
- ++deadCount;
- if( !head ) return deadCount;// all bunnies dead
- }
- // continue to end
- nodePtr iter = head;
- while(iter->next)
- {
- if( iter->next->data.GetAge() >= maxAge && iter->next->data.GetVampire() != "No abnormalities")
- {
- temp = iter->next;// iter->next will be deleted
- iter->next = iter->next->next;// linking across node to be deleted
- delete temp;
- ++deadCount;
- }
- else iter = iter->next;
- }
- return deadCount;
- }
- int List::Reproduction(int age){
- if(!head)
- return 0;
- int babies = 0;
- int males = 0;
- curr = head;
- while(curr->next != NULL)
- {
- if (curr->data.GetAge() >= age && curr->data.GetIsMale()== "Male" && curr->data.GetVampire()=="No abnormalities")
- males++;
- curr = curr->next;
- if (!curr)
- break;
- }
- curr = head;
- while(curr->next != NULL)
- {
- if (curr->data.GetAge() >= age && curr->data.GetIsMale()== "Female" && curr->data.GetVampire()=="No abnormalities"){
- for(int i = 0; i<males;i++){
- Bunny b = Bunny(curr->data.GetColour());
- AddNode(b);
- std::cout<<b.GetName()<<" was born\n";
- babies++;
- }
- }
- curr = curr->next;
- if (!curr)
- break;
- }
- return babies;
- }
- int List::Vampire(){
- int vrabbits = 0;
- int converted = 0;
- curr = head;
- while(curr->next != NULL){
- if (curr->data.GetVampire() != "No abnormalities")
- vrabbits++;
- curr = curr->next;
- if (!curr)
- break;
- }
- curr = head;
- while(curr->next != NULL){
- if (converted == vrabbits)
- break;
- if(curr->data.GetVampire() == "No abnormalities"){
- curr->data.ModifyVamp(true);
- ++converted;
- }
- curr = curr->next;
- if (!curr)
- break;
- }
- return converted+vrabbits;
- }
- bool List::CheckHead(){
- if (head)
- return true;
- else
- return false;
- }
- int main(int argc, char** argv) {
- int year = 0;
- srand(time(NULL));
- std::cout<<"Bunny Simulator\n\n";
- std::cout<<"5 Bunnies move in!\n";
- List population;
- for (int i = 0; i< 5; i++)
- population.AddNode(Bunny());
- while(population.CheckHead()){
- population.PrintList();
- std::cout<<population.Reaper(11)<<" bunnies have died\n";
- std::cout<<population.VReaper(50)<<" vampire bunnies have died\n";
- std::cout<<population.Vampire()<<" vampire rabbits exist\n";
- std::cout<<population.Reproduction(2)<<" bunnies are born\n";
- population.AddAge(1);
- std::cout<<"Year "<<year<<"\n";
- ++year;
- }
- std::cout<<"Submit any key to quit\n";
- char x = 0;
- std::cin>>x;
- return 0;
- }
- Bunny::Bunny(){
- age = 0;
- int gRandom = rand() % 100+1;
- int aRandom = rand() % 3;
- int vRandom = rand() %100 + 1;
- int nRandom = rand() %4;
- std::string names[5] = {"Tyler","Quinn","Riley","Ryan","Casey"};
- if (gRandom>49)
- isMale = true;
- else
- isMale = false;
- switch(aRandom){
- case 0:
- appearance = white;
- break;
- case 1:
- appearance = brown;
- break;
- case 2:
- appearance = black;
- break;
- case 3:
- appearance = spotted;
- break;
- default:
- std::cout<<"A bunny was created without a colour "<<aRandom<<"\n";
- break;
- }
- if (vRandom>2)
- radioactive_mutant_vampire_bunny = false;
- else
- radioactive_mutant_vampire_bunny = true;
- switch(nRandom){
- case 0:
- name = names[0];
- break;
- case 1:
- name = names[1];
- break;
- case 2:
- name = names[2];
- break;
- case 3:
- name = names[3];
- break;
- case 4:
- name = names[4];
- break;
- default:
- std::cout<<"A nameless bunny was born :(\n";
- }
- }
- Bunny::Bunny(std::string c){
- age = 0;
- int gRandom = rand() % 100+1;
- int vRandom = rand() %100 + 1;
- int nRandom = rand() %4;
- std::string names[5] = {"Tyler","Quinn","Riley","Ryan","Casey"};
- if (gRandom>49)
- isMale = true;
- else
- isMale = false;
- if (c == "white")
- appearance = white;
- else if (c == "brown")
- appearance = brown;
- else if (c == "black")
- appearance = black;
- else if (c == "spotted")
- appearance = spotted;
- else
- std::cout<<"Colourless bunny was born\n";
- if (vRandom>2)
- radioactive_mutant_vampire_bunny = false;
- else
- radioactive_mutant_vampire_bunny = true;
- switch(nRandom){
- case 0:
- name = names[0];
- break;
- case 1:
- name = names[1];
- break;
- case 2:
- name = names[2];
- break;
- case 3:
- name = names[3];
- break;
- case 4:
- name = names[4];
- break;
- default:
- std::cout<<"A nameless bunny was born :(\n";
- }
- }
- std::string Bunny::GetIsMale(){
- if (isMale){
- return "Male";
- }
- else{
- return "Female";
- }
- }
- std::string Bunny::GetColour(){
- switch(appearance){
- case 0:
- return "white";
- break;
- case 1:
- return "brown";
- break;
- case 2:
- return "black";
- break;
- case 3:
- return "spotted";
- break;
- default:
- std::cout<<"No colour\n";
- break;
- }
- return 0;
- }
- int Bunny::GetAge(){
- return age;
- }
- std::string Bunny::GetName(){
- return name;
- }
- std::string Bunny::GetVampire(){
- if (radioactive_mutant_vampire_bunny)
- return "Radioactive Mutant Vampire Bunny";
- else
- return "No abnormalities";
- }
- void Bunny::ModifyAge(int a){
- age += a;
- }
- void Bunny::ModifyVamp(bool v){
- radioactive_mutant_vampire_bunny = v;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement