int main() { Controller *master = new Controller(); cout << master->getList()->getSize()<<endl; for(int i = 0;i<50;i++){ master->generate(); } Node<Warrior> *temp = master->getList()->getFront(); while(temp!=0) { Soldier *s = dynamic_cast<Soldier*>(temp->getData()); if(s!=0) cout<<"Soldier!"<< s->getStrength()<<endl; Timmy *t = dynamic_cast<Timmy*>(temp->getData()); if(t!=0) cout<<"Timmy"<< t->getStrength()<<endl; Pirate *p = dynamic_cast<Pirate*>(temp->getData()); if(p!=0) cout<<"Pirate!"<< p->getStrength()<<endl; King *k = dynamic_cast<King*>(temp->getData()); if(k!=0) cout<<"King!"<< k->getStrength()<<endl; Harold *h = dynamic_cast<Harold*>(temp->getData()); if(h!=0) cout<<"Harold!"<< h->getStrength() <<endl; temp = temp->getNext(); } cout << master->getList()->getSize()<<endl; return 0; }
Pirate* Pirate::createPirate(int hp) { Pirate *pRet = new Pirate(); if (pRet && pRet->init()) { pRet->setMaxHp(hp); pRet->setCurrHp(hp); pRet->autorelease(); return pRet; } else { delete pRet; pRet = NULL; return NULL; } }
void Controller::generate() { int random_number = randomInt(1,100); if(random_number<51&&WarriorList->getSize()<25) { Pirate *p = new Pirate(); p->setX(0); p->setY(randomInt(1,6)); WarriorList->push(p); } if(random_number>34&&WarriorList->getSize()<25) { Soldier *s = new Soldier(); s->setY(randomInt(2,4)); s->setX(24); WarriorList->push(s); } }
Pirate* Pirate::createPirate(Vector<Node*> points, int hp) { Pirate *pRet = new Pirate(); if (pRet && pRet->init()) { pRet->setPointsVector(points); pRet->setMaxHp(hp); pRet->setCurrHp(hp); pRet->runFllowPoint(); pRet->autorelease(); return pRet; } else { delete pRet; pRet = NULL; return NULL; } }
//this function performs simulations of the fights void Battle::simulate(class Deque* deque, class Hare* hare, class Tortoise* tort){ //FIGHT while((!deque->empty()) && (hare->getHealth() != 0) || (tort->getHealth() != 0)){ while(true){ if((deque->empty())||((hare->getHealth() == 0) && (tort->getHealth() == 0))||(tort->getHealth() == 0)) break; Pirate *pirate = deque->front();//the tortoise fights the pirate at the front of the deque if(pirate->getHealth() != 0){ pirate->tortAttack(tort); } if((tort->getHealth() != 0)&&(pirate->getHealth() != 0)) tort->pirateAttack(pirate); if(pirate->getHealth() == 0){ deque->popFront(); break; } if((deque->empty())||((hare->getHealth() == 0) && (tort->getHealth() == 0))||(tort->getHealth() == 0)) break; } if(deque->empty()) break; if((hare->getHealth() == 0) && (tort->getHealth() == 0)) break; while(true){ if((deque->empty())||((hare->getHealth() == 0) && (tort->getHealth() == 0))||(hare->getHealth() == 0)) break; Pirate *pirate = deque->back(); if(pirate->getHealth() != 0){ pirate->hareAttack(hare);//the hare fights the pirate at the back of the deque } if((hare->getHealth() != 0)&&(pirate->getHealth() != 0)) hare->pirateAttack(pirate); if(pirate->getHealth() == 0){ deque->popBack(); break; } if((deque->empty())||((hare->getHealth() == 0) && (tort->getHealth() == 0))||(hare->getHealth() == 0)) break; } } //compute the statistics totalRuns++; if(!deque->empty()){ if(hare->getSword()) hareFail++; else if(tort->getSword()) tortFail++; } else if(deque->empty()){ if((tort->getHealth()==0) && (hare->getHealth() != 0) && (hare->getSword())) harePartSuccess++; else if((hare->getHealth()==0) && (tort->getHealth() != 0) && (hare->getSword())) harePartSuccess++; else if((hare->getHealth()==0) && (tort->getHealth() != 0) && (tort->getSword())) tortPartSuccess++; else if((tort->getHealth()==0) && (hare->getHealth() != 0) && (tort->getSword())) tortPartSuccess++; else if((hare->getSword()) && (hare->getHealth() != 0) && (tort->getHealth() != 0)) hareSuccess++; else if((tort->getSword()) && (hare->getHealth() != 0) && (tort->getHealth() != 0)) tortSuccess++; } }