Advertisement
Guest User

Bunnies

a guest
Jan 21st, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. /*
  2. * File: main.cpp
  3. *
  4. * Created on 19 January 2013, 17:48
  5. */
  6.  
  7. //#include <cstdlib>
  8. #include <iostream>
  9. #include <string>
  10. #include <time.h>
  11. #include <vector>
  12.  
  13. enum colour{
  14. white = 0,
  15. brown,
  16. black,
  17. spotted
  18. };
  19.  
  20. class Bunny{
  21. private:
  22. bool isMale;
  23. colour appearance;
  24. unsigned int age;
  25. std::string name;
  26. bool radioactive_mutant_vampire_bunny;
  27. public:
  28. Bunny();
  29. Bunny(std::string c);
  30. std::string GetIsMale();
  31. std::string GetColour();
  32. int GetAge();
  33. std::string GetName();
  34. std::string GetVampire();
  35. void ModifyAge(int a);
  36. void ModifyVamp(bool v);
  37. friend bool operator== (Bunny &b1, Bunny &b2);
  38. friend bool operator!= (Bunny &b1, Bunny &b2);
  39. };
  40.  
  41. bool operator== (Bunny &b1, Bunny &b2){
  42. 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);
  43. }
  44.  
  45. bool operator!= (Bunny &b1, Bunny &b2){
  46. return !(b1 == b2);
  47. }
  48.  
  49.  
  50. class List{
  51. private:
  52. typedef struct node{
  53. Bunny data;
  54. node *next;
  55. }* nodePtr;
  56.  
  57. nodePtr head;
  58. nodePtr curr;
  59. nodePtr temp;
  60. public:
  61. List();
  62. void AddNode(Bunny addData);
  63. void DeleteNode(Bunny delData);
  64. void PrintList();
  65. void AddAge(int a);
  66. int Reaper(int maxAge);
  67. int VReaper(int maxAge);
  68. int Reproduction(int age);
  69. int Vampire();
  70. bool CheckHead();
  71. };
  72.  
  73. List::List(){
  74. head = NULL;
  75. curr = NULL;
  76. temp = NULL;
  77. }
  78.  
  79. void List::AddNode(Bunny addData){
  80. nodePtr n = new node;
  81. n->next = NULL;
  82. n->data = addData;
  83.  
  84. if (head != NULL){
  85. curr = head;
  86. while (curr->next != NULL){
  87. curr = curr->next;
  88. }
  89. curr->next = n;
  90. }
  91. else{
  92. head = n;
  93. }
  94. }
  95.  
  96. /*not currently used
  97. void List::DeleteNode(Bunny delData){
  98. nodePtr delPtr = NULL;
  99. temp = head;
  100. curr = head;
  101. while(curr != NULL && curr->data != delData){
  102. temp = curr;
  103. curr = curr->next;
  104. }
  105. if (curr == NULL){
  106. std::cout<<"Could not delete Bunny check DeleteNode.\n";
  107. delete delPtr;
  108. }
  109. else{
  110. delPtr = curr;
  111. curr = curr->next;
  112. temp->next = curr;
  113. if (delPtr == head){
  114. if (head->next != NULL){
  115. head = head->next;
  116. temp = NULL;
  117. }
  118. else{
  119. head = NULL;
  120. temp = NULL;
  121. }
  122. }
  123. std::cout<<"Bunny "<< delData.GetName()<<" died :(\n";
  124. delete delPtr;
  125. }
  126. }
  127. */
  128.  
  129. void List::PrintList(){
  130. curr = head;
  131. std::cout<<"Bunny List:\n";
  132. while(curr != NULL){
  133. std::cout<<"------------------------------\n";
  134. std::cout<<"Name: "<<curr->data.GetName()<<"\n";
  135. std::cout<<"Age: "<<curr->data.GetAge()<<"\n";
  136. std::cout<<"Colour: "<<curr->data.GetColour()<<"\n";
  137. std::cout<<"Gender: "<<curr->data.GetIsMale()<<"\n";
  138. std::cout<<"Abnormalities: "<<curr->data.GetVampire()<<"\n";
  139. std::cout<<"------------------------------\n";
  140. curr = curr->next;
  141. }
  142. }
  143.  
  144. void List::AddAge(int a){
  145. if (head != NULL){
  146. curr = head;
  147. while (curr->next != NULL){
  148. curr->data.ModifyAge(a);
  149. curr = curr->next;
  150. }
  151. curr->data.ModifyAge(a);
  152. }
  153. else
  154. curr->data.ModifyAge(a);
  155.  
  156. }
  157.  
  158. int List::Reaper(int maxAge)
  159. {
  160. if( !head ) return 0;// list is empty
  161.  
  162. int deadCount = 0;// return value
  163.  
  164. // start at head
  165. while(head->data.GetAge() >= maxAge && head->data.GetVampire() == "No abnormalities")
  166. {
  167. temp = head;
  168. std::cout<<"Bunny "<<head->data.GetName()<<" died\n";
  169. head = head->next;
  170. delete temp;
  171. ++deadCount;
  172. if( !head )
  173. return deadCount;//all bunnies dead
  174. }
  175.  
  176. // continue to end
  177. nodePtr iter = head;
  178. while(iter->next)
  179. {
  180. if( iter->next->data.GetAge() >= maxAge && iter->next->data.GetVampire() == "No abnormalities")
  181. {
  182. temp = iter->next;// iter->next will be deleted
  183. iter->next = iter->next->next;// linking across node to be deleted
  184. delete temp;
  185. ++deadCount;
  186. }
  187. else iter = iter->next;
  188. }
  189.  
  190. return deadCount;
  191. }
  192.  
  193. int List::VReaper(int maxAge)
  194. {
  195. if( !head ) return 0;// list is empty
  196.  
  197. int deadCount = 0;// return value
  198.  
  199. // start at head
  200. while(head->data.GetAge() >= maxAge && head->data.GetVampire() != "No abnormalities")
  201. {
  202. temp = head;
  203. std::cout<<"Vampire Bunny "<<head->data.GetName()<<" died\n";
  204. head = head->next;
  205. delete temp;
  206. ++deadCount;
  207. if( !head ) return deadCount;// all bunnies dead
  208. }
  209.  
  210. // continue to end
  211. nodePtr iter = head;
  212. while(iter->next)
  213. {
  214. if( iter->next->data.GetAge() >= maxAge && iter->next->data.GetVampire() != "No abnormalities")
  215. {
  216. temp = iter->next;// iter->next will be deleted
  217. iter->next = iter->next->next;// linking across node to be deleted
  218. delete temp;
  219. ++deadCount;
  220. }
  221. else iter = iter->next;
  222. }
  223.  
  224. return deadCount;
  225. }
  226.  
  227. int List::Reproduction(int age){
  228.  
  229. if(!head)
  230. return 0;
  231.  
  232. int babies = 0;
  233. int males = 0;
  234. curr = head;
  235. while(curr->next != NULL)
  236. {
  237. if (curr->data.GetAge() >= age && curr->data.GetIsMale()== "Male" && curr->data.GetVampire()=="No abnormalities")
  238. males++;
  239. curr = curr->next;
  240. if (!curr)
  241. break;
  242. }
  243.  
  244. curr = head;
  245. while(curr->next != NULL)
  246. {
  247. if (curr->data.GetAge() >= age && curr->data.GetIsMale()== "Female" && curr->data.GetVampire()=="No abnormalities"){
  248. for(int i = 0; i<males;i++){
  249. Bunny b = Bunny(curr->data.GetColour());
  250. AddNode(b);
  251. std::cout<<b.GetName()<<" was born\n";
  252. babies++;
  253. }
  254. }
  255.  
  256. curr = curr->next;
  257. if (!curr)
  258. break;
  259. }
  260.  
  261.  
  262. return babies;
  263. }
  264.  
  265. int List::Vampire(){
  266. int vrabbits = 0;
  267. int converted = 0;
  268.  
  269. curr = head;
  270. while(curr->next != NULL){
  271. if (curr->data.GetVampire() != "No abnormalities")
  272. vrabbits++;
  273. curr = curr->next;
  274. if (!curr)
  275. break;
  276. }
  277. curr = head;
  278. while(curr->next != NULL){
  279. if (converted == vrabbits)
  280. break;
  281. if(curr->data.GetVampire() == "No abnormalities"){
  282. curr->data.ModifyVamp(true);
  283. ++converted;
  284. }
  285. curr = curr->next;
  286.  
  287. if (!curr)
  288. break;
  289. }
  290. return converted+vrabbits;
  291. }
  292.  
  293. bool List::CheckHead(){
  294. if (head)
  295. return true;
  296. else
  297. return false;
  298. }
  299.  
  300.  
  301.  
  302.  
  303.  
  304. int main(int argc, char** argv) {
  305. int year = 0;
  306. srand(time(NULL));
  307. std::cout<<"Bunny Simulator\n\n";
  308. std::cout<<"5 Bunnies move in!\n";
  309. List population;
  310. for (int i = 0; i< 5; i++)
  311. population.AddNode(Bunny());
  312.  
  313. while(population.CheckHead()){
  314. population.PrintList();
  315. std::cout<<population.Reaper(11)<<" bunnies have died\n";
  316. std::cout<<population.VReaper(50)<<" vampire bunnies have died\n";
  317. std::cout<<population.Vampire()<<" vampire rabbits exist\n";
  318. std::cout<<population.Reproduction(2)<<" bunnies are born\n";
  319. population.AddAge(1);
  320. std::cout<<"Year "<<year<<"\n";
  321. ++year;
  322.  
  323. }
  324.  
  325. std::cout<<"Submit any key to quit\n";
  326. char x = 0;
  327. std::cin>>x;
  328.  
  329. return 0;
  330. }
  331.  
  332.  
  333. Bunny::Bunny(){
  334. age = 0;
  335.  
  336. int gRandom = rand() % 100+1;
  337. int aRandom = rand() % 3;
  338. int vRandom = rand() %100 + 1;
  339. int nRandom = rand() %4;
  340. std::string names[5] = {"Tyler","Quinn","Riley","Ryan","Casey"};
  341.  
  342. if (gRandom>49)
  343. isMale = true;
  344. else
  345. isMale = false;
  346.  
  347. switch(aRandom){
  348. case 0:
  349. appearance = white;
  350. break;
  351. case 1:
  352. appearance = brown;
  353. break;
  354. case 2:
  355. appearance = black;
  356. break;
  357. case 3:
  358. appearance = spotted;
  359. break;
  360. default:
  361. std::cout<<"A bunny was created without a colour "<<aRandom<<"\n";
  362. break;
  363. }
  364.  
  365. if (vRandom>2)
  366. radioactive_mutant_vampire_bunny = false;
  367. else
  368. radioactive_mutant_vampire_bunny = true;
  369.  
  370. switch(nRandom){
  371. case 0:
  372. name = names[0];
  373. break;
  374. case 1:
  375. name = names[1];
  376. break;
  377. case 2:
  378. name = names[2];
  379. break;
  380. case 3:
  381. name = names[3];
  382. break;
  383. case 4:
  384. name = names[4];
  385. break;
  386. default:
  387. std::cout<<"A nameless bunny was born :(\n";
  388. }
  389. }
  390.  
  391. Bunny::Bunny(std::string c){
  392. age = 0;
  393.  
  394. int gRandom = rand() % 100+1;
  395. int vRandom = rand() %100 + 1;
  396. int nRandom = rand() %4;
  397. std::string names[5] = {"Tyler","Quinn","Riley","Ryan","Casey"};
  398.  
  399. if (gRandom>49)
  400. isMale = true;
  401. else
  402. isMale = false;
  403.  
  404. if (c == "white")
  405. appearance = white;
  406. else if (c == "brown")
  407. appearance = brown;
  408. else if (c == "black")
  409. appearance = black;
  410. else if (c == "spotted")
  411. appearance = spotted;
  412. else
  413. std::cout<<"Colourless bunny was born\n";
  414.  
  415.  
  416. if (vRandom>2)
  417. radioactive_mutant_vampire_bunny = false;
  418. else
  419. radioactive_mutant_vampire_bunny = true;
  420.  
  421. switch(nRandom){
  422. case 0:
  423. name = names[0];
  424. break;
  425. case 1:
  426. name = names[1];
  427. break;
  428. case 2:
  429. name = names[2];
  430. break;
  431. case 3:
  432. name = names[3];
  433. break;
  434. case 4:
  435. name = names[4];
  436. break;
  437. default:
  438. std::cout<<"A nameless bunny was born :(\n";
  439. }
  440. }
  441.  
  442. std::string Bunny::GetIsMale(){
  443. if (isMale){
  444. return "Male";
  445. }
  446. else{
  447. return "Female";
  448. }
  449.  
  450. }
  451.  
  452. std::string Bunny::GetColour(){
  453.  
  454. switch(appearance){
  455. case 0:
  456. return "white";
  457. break;
  458. case 1:
  459. return "brown";
  460. break;
  461. case 2:
  462. return "black";
  463. break;
  464. case 3:
  465. return "spotted";
  466. break;
  467. default:
  468. std::cout<<"No colour\n";
  469.  
  470. break;
  471. }
  472. return 0;
  473. }
  474.  
  475. int Bunny::GetAge(){
  476. return age;
  477. }
  478.  
  479. std::string Bunny::GetName(){
  480. return name;
  481. }
  482.  
  483. std::string Bunny::GetVampire(){
  484. if (radioactive_mutant_vampire_bunny)
  485. return "Radioactive Mutant Vampire Bunny";
  486. else
  487. return "No abnormalities";
  488. }
  489.  
  490. void Bunny::ModifyAge(int a){
  491. age += a;
  492. }
  493.  
  494. void Bunny::ModifyVamp(bool v){
  495. radioactive_mutant_vampire_bunny = v;
  496. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement