1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include "Bunny.h"
#include "Game.h"
#include "../../custom_files/rangedRandom.h"
#include "../../custom_files/itos.h"
//------------------- STATIC VARS
int Game::bunniesBorn = 0;
int Game::bunniesDied = 0;
int Game::numMassKills = 0;
int Game::numStarters = 10;
int Game::populationLim = 1000;
//-------------------------------
void Game::Play() // this calls a single round
{
turn++;
Update();
cout << "Turn: " << turn << ", "
<< "Bunnies Remaning: " << bunnyList.size() << endl;
}
bool Game::GameOver()
{
if (bunnyList.empty())
return true;
else
return false;
}
void Game::SaveData()
{
ofstream save ("datalog.txt", ios::app);
if (save.is_open())
{
save << "--------------------------------------------" << endl;
save << "TURN NUMBER " << turn << endl;
save << "List of bunnies:" << endl;
for (vector<Bunny>::iterator iter = bunnyList.begin(); iter != bunnyList.end(); iter++)
save << iter->title << endl;
save << "\nTotal Bunnies: " << bunnyList.size() << endl;
save << "Total Died: " << bunniesDied << endl;
save << "Total Born: " << bunniesBorn << endl;
save << "--------------------------------------------" << endl;
save.close();
}
else
cout << "Failed to save data." << endl;
}
void Game::Update()
{
vector<Bunny>::iterator iter = bunnyList.begin();
Bunny sampleRMVB(NULL); // this isn't an actual bunny being created, it's just a placeholder.
int numRMVB = 0;
bool dadExists = false;
// first, see if there is a father.
while (!dadExists && iter != bunnyList.end()) // as long as we haven't found a father AND we havent reached the end of the list.
{
if (iter->age >= 2 && iter->gender == "male")
dadExists = true;
iter++;
}
// now, check for mutants.
for (iter = bunnyList.begin(); iter != bunnyList.end();
iter++)
{
if (iter->isRMVB)
{
sampleRMVB = *iter;
numRMVB++;
}
}
// now, age all bunnies, kill ones that are too old, mutate if necessary, and give birth to new ones.
for (iter = bunnyList.begin(); iter != bunnyList.end();
iter++)
{
iter->age++; // age all bunnies.
iter->title = "Bunny " + iter->name + " (" + itos(iter->age) + ", " + iter->gender + ", " + iter->color + ")"; // update the title of each bunny.
if (iter->age >= 2 && iter->gender == "female" && !iter->isRMVB) // if the bunny is a girl over 2 and is NOT radioactive
{
if (dadExists) // if there is a father present
{
bunniesBorn++;
bunnyList.push_back(iter->BirthBunny(*iter)); // add a new bunny to the list!
}
}
if (numRMVB > 0)
{
if (!iter->isRMVB) // if the current is not an RMVB.
{
sampleRMVB.ConverttoRMVB(*iter);
numRMVB--;
}
}
if (iter->tooOld())
{
bunniesDied++;
cout << iter->name << " died!" << endl;
system("PAUSE");
if (!bunnyList.empty())
iter = bunnyList.erase(iter); // kill the bunny
}
if (static_cast<int>(bunnyList.size()) >= randomInRange(populationLim, bunnyList.size())) // if there are too many bunnies in the population....
{
int amount;
if (bunnyList.size()-2 > populationLim)
amount = randomInRange(populationLim, bunnyList.size()-2); // kill anywhere from populationLim to n-2 rabbits
else
amount = populationLim;
bunniesDied += amount;
random_shuffle(bunnyList.begin(), bunnyList.end());
bunnyList.erase(bunnyList.begin(), bunnyList.begin()+amount);
numMassKills++;
cout << "MASS BUNNY DEATH! " << amount
<< " BUNNIES KILLED." << endl;
}
}
}
|