问题描述
我目前正在为课堂制作Chutes and Ladders游戏。这场比赛的胜利条件是如果一名球员登上广场100,他们赢了。如果他们碰巧超过了100,他们会留在那里,直到每个人都完成了比赛(无论是有100人还是100人)。如果EVERYONE已经超过100,那么我很难摆脱while循环。有很多代码,所以我会尽可能简化。在!!!这里是问题!是我需要帮助的地方。
是否有一种方法可以指定一个数组中的所有元素都是100以上的破坏?对不起,如果问题是重复...这个帖子有更多的细节。
string name [MAXplayers]; //数组存储名字
int position [MAXplayers]; //数组存储板位置
无符号的种子; //随机数生成器种子
seed = time(0); //将种子设置为0
srand(seed); //调用srand函数
int旋转;
bool done = false;
int counter = 0;
const int WIN = 100;
while(done!= true)
{
if(counter == WIN)
{
cout< 就是这样,游戏结束了! << endl<< ENDL;
完成= true;
for(int i = 0; i< players; i ++)
{
if(position [i]> WIN)// !!!!!这是问题!!!!!!
{
cout<< 对不起<<名字[我]<< !你不能移动!你被困在<<位置[i]<< endl<< ENDL;
}
else
{
cout<<名字[我]<< 轮到了!Pres [Enter]旋转车轮!;
cin.get();
spin = rand()%12 + 1;
cout<< 你编号<<旋转 ! << ENDL;
int temploc = position [i] + = spin;
等等... etc ... etc ... etc ... etc ...
当您的WIN条件满足时,要打破循环的Inter ,您可以使用关键字的断裂即可。这将停止循环处理,而不管循环的条件如何。 然而注意,程序仍然会在循环中遍历你的,直到它是有条件的 done!= true 即可。
I'm currently making a Chutes and Ladders game for class. The win condition for this game is if a player lands on Square 100, they win. If they happen to go past 100, they stay there until everyone finishes the game (Either someone lands on 100 or everyone goes past 100). I'm having trouble breaking out of the while loop if EVERYONE is past 100.
There's a lot of code so I'll simplify as much as I can. Under !!!HERE IS THE PROBLEM!!!! is where I need help.
Is there a way to specify once ALL the elements in an array are ABOVE 100 to break? Sorry if the question is a repeat...this post has a lot more detail.
string name[MAXplayers]; //Array to store names int position[MAXplayers]; //Array to store board position unsigned seed; //Random Number Generator Seed seed = time(0); //Set seed to 0 srand(seed); //Call srand function int spin; bool done = false; int counter = 0; const int WIN = 100; while (done != true) { if (counter == WIN) { cout << "That's it, game over!" << endl << endl; done = true; } else { for (int i = 0; i < players; i++) { if (position[i] > WIN) //!!!!!HERE IS THE PROBLEM!!!!!! { cout << "Sorry " << name[i] << "! You can't move! You're stuck at " << position[i] << endl << endl; } else { cout << name[i] << "'s turn! Pres [Enter] to spin the wheel!"; cin.get(); spin = rand() % 12 + 1; cout << "You spun the number " << spin << "!" << endl; int temploc = position[i] += spin; etc...etc...etc...etc...etc...etc...
To break out of the inter for loop when your WIN condition is met, you can use the keyword break. This will stop the loop processing regardless of the conditional on the for loop.
Note however after breaking out, the program will still iterate around your while loop until it's conditional done != true is met.
这篇关于用数组条件打破循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!