制作骰子游戏。试图在一名玩家达到 100 时立即打破循环
Making a dice game. Trying to get the loop to break as soon as one player reaches 100
对 c++ 相当陌生。我要创建一个骰子游戏,让您可以设置骰子的面数和玩家人数。每个人都从 1 分开始,一旦有人达到 100 分或更多,游戏就会结束。
这是我目前的代码
int players = 5;
int playerPosition[players];
for(int i=0; i < players; i++)
{
playerPosition[i] = 1;
}
for(int i = 0; i < players; i++)
{
while(playerPosition[i] < 100)
{
int roll;
for (int j = 0; j < players; j++)
{
roll = dieRoll(6);
// dieRoll is a function I made to simulate a die roll
playerPosition[j] += roll;
cout << "Player " << j + 1 << " rolled a " << roll << " and is now in position " << playerPosition [j] << endl;
}
}
}
所以目前,输出会打印出每个玩家的每个回合。问题是它会一直持续到每个玩家达到 >= 100。我尝试添加
if(playerPosition[i] >= 100)
{
break;
}
在 for 循环和 while 循环中。他们仍然没有按照我想要的方式工作。你能告诉我可能是什么问题吗?
谢谢。
玩家的总和增加后每次都要检查总和是否超过100:
int players = 5;
int playerPosition[players];
for(int i=0; i < players; i++)
{
playerPosition[i] = 1;
}
while(true)
{
int roll;
for (int j = 0; j < players; j++)
{
roll = dieRoll(6);
// dieRoll is a function I made to simulate a die roll
playerPosition[j] += roll;
cout << "Player " << j + 1 << " rolled a " << roll << " and is now in position " << playerPosition [j] << endl;
if(playerPosition[j] >= 100)
{
return;
}
}
}
问题是您要滚动每个玩家,一次一个,直到他们达到 100 分。每次掷骰子时需要检查是否有玩家超过 100
实现此目的的一种方法是在外部 for 循环外声明一个 bool gameOver
变量,其值初始设置为 false
。每次增加玩家分数时,您可以添加行
playerPosition[j] += roll;
gameOver = playerPosition[j] >= 100
现在,如果您更改代码以具有结构
while(!gameOver) {
for(int i = 0; i < players; i++) {
它应该按预期运行。完整代码因此变成
int players = 5;
bool gameOver = false;
int playerPosition[players];
for(int i=0; i < players; i++)
{
playerPosition[i] = 1;
}
while(!gameOver) {
for(int i = 0; i < players; i++) {
int roll;
roll = dieRoll(6);
// dieRoll is a function I made to simulate a die roll
playerPosition[j] += roll;
gameOver = playerPosition[j] >= 100
if (gameOver)
{
break;
}
cout << "Player " << j + 1 << " rolled a " << roll << " and is now in position " << playerPosition [j] << endl;
}
}
}
对 c++ 相当陌生。我要创建一个骰子游戏,让您可以设置骰子的面数和玩家人数。每个人都从 1 分开始,一旦有人达到 100 分或更多,游戏就会结束。
这是我目前的代码
int players = 5;
int playerPosition[players];
for(int i=0; i < players; i++)
{
playerPosition[i] = 1;
}
for(int i = 0; i < players; i++)
{
while(playerPosition[i] < 100)
{
int roll;
for (int j = 0; j < players; j++)
{
roll = dieRoll(6);
// dieRoll is a function I made to simulate a die roll
playerPosition[j] += roll;
cout << "Player " << j + 1 << " rolled a " << roll << " and is now in position " << playerPosition [j] << endl;
}
}
}
所以目前,输出会打印出每个玩家的每个回合。问题是它会一直持续到每个玩家达到 >= 100。我尝试添加
if(playerPosition[i] >= 100)
{
break;
}
在 for 循环和 while 循环中。他们仍然没有按照我想要的方式工作。你能告诉我可能是什么问题吗?
谢谢。
玩家的总和增加后每次都要检查总和是否超过100:
int players = 5;
int playerPosition[players];
for(int i=0; i < players; i++)
{
playerPosition[i] = 1;
}
while(true)
{
int roll;
for (int j = 0; j < players; j++)
{
roll = dieRoll(6);
// dieRoll is a function I made to simulate a die roll
playerPosition[j] += roll;
cout << "Player " << j + 1 << " rolled a " << roll << " and is now in position " << playerPosition [j] << endl;
if(playerPosition[j] >= 100)
{
return;
}
}
}
问题是您要滚动每个玩家,一次一个,直到他们达到 100 分。每次掷骰子时需要检查是否有玩家超过 100
实现此目的的一种方法是在外部 for 循环外声明一个 bool gameOver
变量,其值初始设置为 false
。每次增加玩家分数时,您可以添加行
playerPosition[j] += roll;
gameOver = playerPosition[j] >= 100
现在,如果您更改代码以具有结构
while(!gameOver) {
for(int i = 0; i < players; i++) {
它应该按预期运行。完整代码因此变成
int players = 5;
bool gameOver = false;
int playerPosition[players];
for(int i=0; i < players; i++)
{
playerPosition[i] = 1;
}
while(!gameOver) {
for(int i = 0; i < players; i++) {
int roll;
roll = dieRoll(6);
// dieRoll is a function I made to simulate a die roll
playerPosition[j] += roll;
gameOver = playerPosition[j] >= 100
if (gameOver)
{
break;
}
cout << "Player " << j + 1 << " rolled a " << roll << " and is now in position " << playerPosition [j] << endl;
}
}
}