当我运行代码时,只有在所有巨龙都没有健康或者所有舰船都没有健康之后,才能赢得胜利condition = true
。
但是当它运行时,即使条件设置为仅在win条件为true时才离开,但do while循环似乎在第一次迭代后退出。
我似乎无法弄清楚为什么它无法继续运行,甚至在声明变量时分配了winCondition = false
。
任何帮助将不胜感激。
#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;
}
答案 0 :(得分:3)
您的代码是
bool winCondition = false;
/* ... */
do {/* ... */}
while (winCondition == true);
,您想知道“即使winCondition = false,循环仍会退出”。您还将解释“条件设置为仅在获胜条件为真时才离开”。
我得出结论,您误解了do-while循环的语义是“直到满足条件才循环”。相反,它是“在满足条件时循环”。这意味着当winCondition
评估为false时退出是预期的行为。
答案 1 :(得分:0)
您正在初始化winCondition = false
。 do..while循环将运行一次迭代,并评估不满足迭代条件winCondition == true
,因此退出。
此外,如果do..while循环继续进行,则其中没有将winCondition
设置为false
的语句,因此循环将永远不会退出。
您是要用winCondition == false
关闭do..while循环吗?