//goes through list of bunnies and kills those over 10 and not radioactive or over 50 if radioactive void BunnyWorld::killOldBunnies() { //point tracker to first 'dummy' bunny Bunny* tracker = m_first; //point next bunny to first 'real' bunny Bunny* nextBunny = tracker->m_next; /* The program keeps bunnies in age order so only goes through the bunnies that are aged 10 or over to decide whether to kill it or not */ while (nextBunny->getAge() >= 10) { //if bunny is 50, always dies. If not radioactive, is 10 by the while condition so kill if (nextBunny->getAge() == 50 || !nextBunny->getRadioactive()){ tracker->m_next = nextBunny->m_next; killBunny(nextBunny); } else tracker = tracker->m_next; nextBunny = tracker->m_next; /* if we've reached the end of the list (no bunnies under 10) break out of while loop. It's implemented like this because the while loop requires a getAge call which doesn't work for the nullptr pointer. */ if (nextBunny==nullptr){ break; } } }
/* For every female aged 2 or older and not radioactive, give birth to a new bunny of the same color and place it at the end of the list */ void BunnyWorld::giveBirth() { Bunny* tracker = m_first->m_next; //find last bunny in list. Bunny* findEnd = tracker; while (findEnd->m_next != nullptr){ findEnd = findEnd->m_next; } //go through whole list while (tracker != nullptr){ if (tracker->getSex() == bny::female && tracker->getAge() >=2 && !tracker->getRadioactive()){ int color = tracker->getColor(); Bunny* newBunny = new Bunny(color); //put new bunny at end of list newBunny->m_next = nullptr; findEnd->m_next = newBunny; findEnd = newBunny; m_population++; if (newBunny->getRadioactive()){ m_radioactivePopulation++; } } tracker = tracker->m_next; } }
//Check to see if there is a Bunny with age >=2 and not radioactive in the population bool BunnyWorld::isEligibleMale() const { Bunny* tracker = m_first->m_next; while(tracker!=nullptr){ if (tracker->getAge() >= 2 && tracker->getSex() == bny::male && !tracker->getRadioactive()){ return true; } tracker = tracker->m_next; } return false; }