Reputation: 83
When I run the code it should only make the win condition = true
after either all dragons have no health or all ships have no health.
But when it runs, it seems the do while loop exits after the first iteration, even though the condition is set to only leave while win condition is true.
I cannot seem to figure out why it wont continue running, I even assigned winCondition = false
when declaring the variable.
Any help would be appreciated.
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
struct ship //Creates type ship
{
float shipHealth;
int numOfPeople;
bool capturedDragon;
};
struct dragon //Creates type dragon
{
string riderName;
string dragonName;
float dragonHealth;
bool isCaptured = false;
};
int main()
{
srand(time(NULL));
const int MAX_SHIPS = 7;
const int MAX_RIDERS = 5;
int dragonHit;
int dragonDamage;
int dragonRemoveMenNumber;
int shipDamage;
int shipHit;
bool winCondition = false;
ship hunterShips[MAX_SHIPS]; //creates array of ships
dragon dragonRiders[MAX_RIDERS]; //creates array of dragon/riders.
for (int i = 0; i <= 4; i++)
{
cout << "Rider Name: " << endl;
cin >> dragonRiders[i].riderName;
cout << "Dragon Name: " << endl;
cin >> dragonRiders[i].dragonName;
dragonRiders[i].dragonHealth = rand() % (20 - 15 + 1) + 15;
}
for (int i = 0; i <= 6; i++)
{
hunterShips[i].shipHealth = rand() % (40 - 30 + 1) + 30;
hunterShips[i].numOfPeople = rand() % (15 - 10 + 1) + 10;
}
do
{
for (int i = 0; i <= 4; i++) //Dragons turn
{
if(dragonRiders[i].dragonHealth > 0 && dragonRiders[i].isCaptured == false) //Dragon is alive
{
dragonHit = rand() % 10 + 1;
if (dragonHit <= 7) //Dragon hits target
{
if(hunterShips[i].shipHealth > 0 || hunterShips[i].numOfPeople > 0)
{
dragonDamage = rand() % (10 - 5 + 1) + 5; //Amount of Damage done
cout << dragonRiders[i].dragonName << " hit and dealt " << dragonDamage << " damage. " << endl;
if (dragonHit <= 3) //Dragon takes men out
{
dragonRemoveMenNumber = rand() % (3 - 2 + 1) + 2;
cout << dragonRiders[i].dragonName << "Took out " << dragonRemoveMenNumber << " men" << endl;
hunterShips[MAX_SHIPS].numOfPeople = hunterShips[MAX_SHIPS].numOfPeople - dragonRemoveMenNumber; //Ships lose people
}
hunterShips[MAX_SHIPS].shipHealth = hunterShips[MAX_SHIPS].shipHealth - dragonDamage;
}
else
{
cout << "All ships are destroyed, dragons win!" << endl;
winCondition = true;
}
}
else //Dragon misses target
cout << dragonRiders[i].dragonName << " missed" << endl;
}
else //Dragon is dead
cout << dragonRiders[i].dragonName << " is dead or captured" << endl;
}
for (int i = 0; i <= 6; i++) //Ships turn
{
if(hunterShips[i].shipHealth > 0 || hunterShips[i].numOfPeople > 0) //ship is afloat
{
shipHit = rand() % 10 + 1;
if (shipHit >= 7) //40% chance to hit
{
if(dragonRiders[i].dragonHealth > 0 && dragonRiders[i].isCaptured == false)
{
shipDamage = rand() % (5 - 4 + 1) + 4; //Damage done
cout << "Ship dealt " << shipDamage << " damage" << endl;
dragonRiders[MAX_RIDERS].dragonHealth = dragonRiders[MAX_RIDERS].dragonHealth - shipDamage;
}
else
{
cout << "All dragons are dead, ships win!" << endl;
winCondition = true;
}
}
else
cout << "Ship missed. " << endl;
}
else
cout << "Ship is sunk or out of men" << endl; //ship is sunk
}
} while (winCondition == true);
return 0;
}
Upvotes: 0
Views: 1336
Reputation: 26703
Your code is
bool winCondition = false;
/* ... */
do {/* ... */}
while (winCondition == true);
and you wonder "Loop keeps exiting even though winCondition = false". You also explain "the condition is set to only leave while win condition is true".
I conclude that you misunderstood the semantics of a do-while loop to be "loop until condition is met". Instead it is however "loop while the condition is met". This means that exiting when winCondition
evaluates to false is the intended behaviour.
Upvotes: 3
Reputation: 663
You are initialising the winCondition = false
to start with. The do..while loop will run through one iteration and evaluate that the iterate condition winCondition == true
is not satisfied, and therefore exit.
In addition, if the do..while loop ever continued, there is no statement inside it where winCondition
is ever set to false
, so the loop will never exit.
Did you mean to close the do..while loop with winCondition == false
?
Upvotes: 0