Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <cstdlib>
- #include <iomanip>
- #include <locale>
- #include <sstream>
- #define MAXB 1000
- using namespace std;
- string maleNames[1000];
- string femaleNames[1000];
- ofstream infoOutp("statistics.txt");
- ofstream eventOutp("events.txt");
- void readNames()
- {
- ifstream male("male.txt");
- for(int i = 0; i < 1000; i++)
- male >> maleNames[i];
- male.close();
- ifstream female("female.txt");
- for(int i = 0; i < 1000; i++)
- female >> femaleNames[i];
- female.close();
- }
- int getRandomNumber(int min, int max)
- {
- static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0); // static used for efficiency, so we only calculate this value once
- // evenly distribute the random number across our range
- return static_cast<int>(rand() * fraction * (max - min + 1) + min);
- }
- class Bunny {
- public:
- enum sex_type {
- SEX_MALE,
- SEX_FEMALE
- };
- enum color {
- COLOR_WHITE,
- COLOR_BROWN,
- COLOR_BLACK,
- COLOR_SPOTTED,
- ANY_COLOR
- };
- private: //properties
- sex_type m_sex;
- color m_color;
- int m_age = 0;
- string m_name = "";
- bool m_radioactive;
- public:
- Bunny(sex_type sex, color col, string name, bool ractive) //Constructors
- : m_sex(sex), m_color(col), m_name(name), m_radioactive(ractive)
- {
- if(ractive == true)
- eventOutp << "Radioactive bunny ";
- else
- eventOutp << "Bunny ";
- eventOutp << name << " was born" << endl;
- }
- Bunny() {
- m_name = "";
- }
- static Bunny createRandomBunny(Bunny bunnies[], color motherColor = ANY_COLOR)
- {
- cout << "CreateRandomBunny" << endl;
- sex_type sex = getRandomNumber(0, 1) > 0 ? SEX_MALE : SEX_FEMALE; //sex
- string name = sex == SEX_MALE ? maleNames[getRandomNumber(0, 999)] : femaleNames[getRandomNumber(0, 999)]; //name
- bool radioactive = getRandomNumber(1, 50) == 1 ? true : false; //radioactiveness
- color randCol;
- if(motherColor == ANY_COLOR)
- randCol = static_cast<Bunny::color>(getRandomNumber(0, 3)); //color
- else
- randCol = motherColor;
- return Bunny(sex, randCol, name, radioactive);
- }
- void reset(int &alive)
- {
- alive--;
- eventOutp << "Bunny " << this->m_name << " died" << endl;
- this->m_name = ""; //set the chosen bunnie's name to blank (since everywhere we check the name to see if a spot in the list is filled)
- return;
- }
- static void sortBunnies(Bunny bunnies[])
- {
- cout << "SortBunnies" << endl;
- for(int i = 0; i < MAXB; i++)
- for(int j = 0; j< MAXB; j++)
- if(bunnies[i].m_age > bunnies[j].m_age && bunnies[i].m_sex > bunnies[j].m_sex)
- swap(bunnies[i], bunnies[j]); //Sort bunnies by age (oldest first)
- return;
- }
- static void printBunnies(Bunny bunnies[], int &turnNr)
- {
- cout << "PrintBunnies" << endl;
- string strTurn = "\nTurn nr. " + static_cast<ostringstream*>( &(ostringstream() << turnNr) )->str() + " ----------------------------------------------------\n";
- cout << strTurn << endl;
- infoOutp << strTurn << endl;
- int totalBunnies = 0;
- int totalMales = 0;
- int totalFemales = 0;
- int totalRadioactive = 0;
- sortBunnies(bunnies);
- for(int i = 0; i < MAXB; i++) {
- if(bunnies[i].m_name != "") {
- totalBunnies++;
- string info = "";
- if(bunnies[i].m_radioactive == true) {
- info = "Radioactive bunny " + bunnies[i].m_name;
- totalRadioactive++;
- }
- else
- info = "Bunny " + bunnies[i].m_name;
- if(bunnies[i].m_sex == SEX_MALE && bunnies[i].m_radioactive == false) {
- info += " (male)";
- totalMales++;
- }
- else if (bunnies[i].m_sex == SEX_FEMALE && bunnies[i].m_radioactive == false){
- info += " (female)";
- totalFemales++;
- }
- info += ",\t " + static_cast<ostringstream*>( &(ostringstream() << bunnies[i].m_age) )->str() + " years old, color: ";
- if(bunnies[i].m_color == 0)
- info += "white";
- else if(bunnies[i].m_color == 1)
- info += "brown";
- else if(bunnies[i].m_color == 2)
- info += "black";
- else if(bunnies[i].m_color == 3)
- info += "spotted";
- cout << info << endl;
- infoOutp << info << endl;
- }
- }
- double malePercent = (static_cast<double>(totalMales) / static_cast<double>(totalBunnies)) *100;
- double femalePercent = (static_cast<double>(totalFemales) / static_cast<double>(totalBunnies)) *100;
- double radioactivePercent = (static_cast<double>(totalRadioactive) / static_cast<double>(totalBunnies)) *100;
- string statistics = "\nTotal bunnies: " + static_cast<ostringstream*>( &(ostringstream() << totalBunnies) )->str()
- + ", male: " + static_cast<ostringstream*>( &(ostringstream() << totalMales) )->str()
- + " (" + static_cast<ostringstream*>( &(ostringstream() << malePercent) )->str()
- + "%), female: " + static_cast<ostringstream*>( &(ostringstream() << totalFemales) )->str()
- + " (" + static_cast<ostringstream*>( &(ostringstream() << femalePercent) )->str()
- + "%), radioactive: " + static_cast<ostringstream*>( &(ostringstream() << totalRadioactive) )->str()
- + " (" + static_cast<ostringstream*>( &(ostringstream() << radioactivePercent) )->str() + ")\n";
- cout << statistics << endl;
- infoOutp << statistics << endl;
- return;
- }
- static void killHalf(Bunny bunnies[], int &alive)
- {
- for(int i = 0; i < MAXB/2; i++) {
- int deathNr = getRandomNumber(0, 999);
- while(bunnies[i].m_name == "")
- deathNr = getRandomNumber(0, 999);
- bunnies[deathNr].reset(alive);
- }
- return;
- }
- static void turn(Bunny bunnies[], int &turnNr, int &alive)
- {
- eventOutp << "\nTurn nr. " << static_cast<ostringstream*>( &(ostringstream() << turnNr) )->str() << " ----------------------------------------------------\n" << endl;
- bool areMales = false;
- for(int y = 0; y < MAXB; y++)
- if(bunnies[y].m_age >= 2 && bunnies[y].m_sex == SEX_MALE) { //check if there are age 2 or older males
- areMales = true;
- break;
- }
- for(int i = 0; i < MAXB; i++) { //FOR EACH BUNNY
- if(bunnies[i].m_name != "") { //if bunny exists (m_name not empty)
- if(bunnies[i].m_radioactive == false) { //if bunny is not radioactive
- if(areMales && bunnies[i].m_sex == SEX_FEMALE) {//if there are age 2 or older males and bunny is a female
- int emptyID = 9999;
- for(int j = 0; j < MAXB; j++)
- if(bunnies[j].m_name == "") { //search for a free spot in the bunny list
- emptyID = j;
- break;
- }
- if(emptyID != 9999) {//if there is a free spot (emptyID changed)
- bunnies[emptyID] = createRandomBunny(bunnies, bunnies[i].m_color); //create a new bunny with the color of his mother's
- alive++;
- }
- else {//if there aren't any free spots in the bunny list (emptyID unchanged)
- killHalf(bunnies, alive); //kill half of the bunnies
- alive = 500; //there should be 1000/2 = 500 bunnies left
- return; //finish the turn early
- }
- }
- if (bunnies[i].m_age < 10) //if bunny is younger than 10 yrs.
- (bunnies[i].m_age += 1); //it grows older
- else
- bunnies[i].reset(alive); //or it dies
- }
- else {//if bunny is radioactive
- int nonRadioactive = 0; //check if there are any non-radioactive bunnies
- for(int y = 0; y < MAXB; y++)
- if(bunnies[y].m_radioactive == false)
- nonRadioactive++;
- if(nonRadioactive != 0) { //if there are any non-radioactive bunnies
- int deathNr = getRandomNumber(0, MAXB); //generate a random bunny id in the list
- while(bunnies[deathNr].m_name == "" || bunnies[deathNr].m_radioactive == true)
- deathNr = getRandomNumber(0, MAXB); //change the ID until we find a non-empty (name not blank) and non-radioactive buny
- bunnies[deathNr].m_radioactive = true; //turn the chosen bunny into radioactive
- eventOutp << "Bunny " << bunnies[deathNr].m_name << " was turned radioactive" << endl;
- }
- if(bunnies[i].m_age < 50) //if radioactive bunny is younger than 50 yrs.
- bunnies[i].m_age += 1; //it grows older
- else
- bunnies[i].reset(alive); //or it dies
- }
- }
- }
- if(alive > 0) {
- printBunnies(bunnies, turnNr);
- turnNr++;
- }
- return;
- }
- };
- int main()
- {
- srand(static_cast<unsigned int>(time(0))); // set initial seed value to system clock
- rand();
- readNames();
- Bunny bunnies [MAXB];
- int turnNr = 1;
- for(int i = 0; i < 5; i++) {
- bunnies[i] = Bunny::createRandomBunny(bunnies); //create the 5 starting bunnies
- }
- int alive = 5;
- while(alive != 0) //while there is at least one bunny alive
- Bunny::turn(bunnies, turnNr, alive);
- infoOutp.close();
- eventOutp.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement