My question is how do I fix the loop? Because I either have an endless loop, or
ID: 3550632 • Letter: M
Question
My question is how do I fix the loop? Because I either have an endless loop, or no iterations of it at all. Any other suggestion or corrections are welcomed
main.cpp
int main(){
Pokemon poke1, poke2;
Dice d10(10);
int playerTurn = d10.roll();
int round = 1;
std::cout<< "Player 1, build your Pokemon!"<<std::endl;
std::cout<< "============================="<<std::endl;
poke1.userBuild();//Builds Pokemon 1
std::cout<< "Player 2, build your Pokemon!"<<std::endl;
std::cout<< "============================="<<std::endl;
poke2.userBuild();//Builds Pokemon 2
//Randomly decides which player will go first
//bool isitdead = false;//Variable used to check if the Pokemon has died
while (round <=10){//Makes sure that the fight doesn't go past 10 rounds, after round 10 it returns 0 to end program
if (playerTurn > 5){
//player 1 goes first
std::cout<<"Player 1 rolls a "<<playerTurn<<" and will go first"<<std::endl;
bool isitdead = false;
while (isitdead == false){
std::cout<<"Round "<<round<<"!"<<' '<<std::endl;
std::cout<<poke1.getname()<<" is attacking "<<poke2.getname()<<std::endl;
isitdead = poke1.attack(poke2);
if(isitdead == true){//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke2.getname()<<" has been defeated!"<<std::endl;
return 0;
}
}
isitdead = poke2.attack(poke1);
if(isitdead == true){//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke1.getname()<<" has been defeated!"<<std::endl;
return 0;
}
if (playerTurn <=5){
//player 2 goes first
std::cout<<"Player 1 rolls a "<<playerTurn<<" and will go second"<<std::endl;
while (isitdead == false){
std::cout<<"Round "<<round<<"!"<<' '<<std::endl;
std::cout<<poke2.getname()<<" is attacking "<<poke1.getname()<<std::endl;
isitdead = poke2.attack(poke1);
if(isitdead == true){//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke1.getname()<<" has been defeated!"<<std::endl;
return 0;
}
}
isitdead = poke1.attack(poke2);
if(isitdead == true){//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke2.getname()<<" has been defeated!"<<std::endl;
return 0;
}
}
++round;
}
if (poke1.getHP() || poke2.getHP() <0){//If both pokemon are still standing after round 10 then the match ends in a draw
std::cout<<"Both fighters are still standing! It's a draw!"<<std::endl;
return 0;
}
}
}
Explanation / Answer
My question is how do I fix the loop? Because I either have an endless loop, or no iterations of it at all. Any other suggestion or corrections are welcomed
//main.cpp
int main(){
Pokemon poke1, poke2;
Dice d10(10);
int playerTurn = d10.roll();
int round = 1;
std::cout<< "Player 1, build your Pokemon!"<<std::endl;
std::cout<< "============================="<<std::endl;
poke1.userBuild();//Builds Pokemon 1
std::cout<< "Player 2, build your Pokemon!"<<std::endl;
std::cout<< "============================="<<std::endl;
poke2.userBuild();//Builds Pokemon 2
//Randomly decides which player will go first
//bool isitdead = false;//Variable used to check if the Pokemon has died
while (round <=10)
{
//Makes sure that the fight doesn't go past 10 rounds, after round 10 it returns 0 to end program
if (playerTurn > 5)
{
//player 1 goes first
std::cout<<"Player 1 rolls a "<<playerTurn<<" and will go first"<<std::endl;
bool isitdead = false;
while (isitdead == false)
{
std::cout<<"Round "<<round<<"!"<<' '<<std::endl;
std::cout<<poke1.getname()<<" is attacking "<<poke2.getname()<<std::endl;
isitdead = poke1.attack(poke2);
if(isitdead == true)
{
//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke2.getname()<<" has been defeated!"<<std::endl;
return 0;
}
}
isitdead = poke2.attack(poke1);
if(isitdead == true)
{
//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke1.getname()<<" has been defeated!"<<std::endl;
return 0;
}
}
if (playerTurn <=5)
{
//player 2 goes first
std::cout<<"Player 1 rolls a "<<playerTurn<<" and will go second"<<std::endl;
while (isitdead == false)
{
std::cout<<"Round "<<round<<"!"<<' '<<std::endl;
std::cout<<poke2.getname()<<" is attacking "<<poke1.getname()<<std::endl;
isitdead = poke2.attack(poke1);
if(isitdead == true)
{
//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke1.getname()<<" has been defeated!"<<std::endl;
return 0;
}
}
isitdead = poke1.attack(poke2);
if(isitdead == true)
{
//check which pokemon is dead (or fainted if you care for the correct terminology)
std::cout<<poke2.getname()<<" has been defeated!"<<std::endl;
return 0;
}
}
if (poke1.getHP() || poke2.getHP() <0)
{
//If both pokemon are still standing after round 10 then the match ends in a draw
std::cout<<"Both fighters are still standing! It's a draw!"<<std::endl;
return 0;
}
++round;
}
}
//pokemon.cpp
int attackBonus = d20.roll();
int defenseBonus = d20.roll();
std::cout<<Pname<<" rolls an attack bonus of "<<attackBonus<<std::endl;
std::cout<<this ->Pname<<" rolls an defense bonus of "<<defenseBonus<<std::endl;
//if the attackLevel+attackBonus of the attacker is greater than the the defenseLevel+defenseBonus of the defender, then roll for damage. Otherwise the attack misses
if (attackL+attackBonus >= opponent.defL+defenseBonus){//Pokemon 1 attack
int roll1, roll2, roll3; //bonus 3 d6 rolls
roll1 = d6.roll();
roll2 = d6.roll();
roll3 = d6.roll();
int totalDamage = roll1 + roll2 + roll3;
std::cout<<"The attack hits dealing 3-D6 damage!"<<std::endl;
std::cout<<"The rolls are "<<roll1<<", "<<roll2<<", and "<<roll3<<" totalling: "<<totalDamage<<" damage!"<<std::endl;
opponent.healthPoints = (opponent.healthPoints - totalDamage);
std::cout<<opponent.Pname<<" has"<<(opponent.healthPoints)- totalDamage<<"hit points left"<<std::endl;
if (opponent.healthPoints <= 0){
return true;
}
}
else if (attackL+attackBonus <= opponent.defL+defenseBonus){
std::cout<<"The attack missed"<<std::endl;
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.