c++ - Making a dice game. Trying to get the loop to break as soon as one player reaches 100 -
fairly new c++. i'm create dice game lets set number of sides die , number of players. starts @ 1 pt , game ends hits 100 or more.
this code have far
int players = 5; int playerposition[players]; for(int i=0; < players; i++) { playerposition[i] = 1; } for(int = 0; < players; i++) { while(playerposition[i] < 100) { int roll; (int j = 0; j < players; j++) { roll = dieroll(6); // dieroll function made simulate die roll playerposition[j] += roll; cout << "player " << j + 1 << " rolled " << roll << " , in position " << playerposition [j] << endl; } } } so @ moment, output print out each turn each player fine. problem it'll keep going until every player reaches >= 100. have tried adding
if(playerposition[i] >= 100) { break; } in loop , in while loop. still don't work way want to. can tell me problem may be?
thanks.
the problem rolling each player, 1 @ time, until reach 100 points. need check if there player on 100 each time roll dice
one way have bool gameover variable declared outside outer loop, value set false. every time increment players score, can add line
playerposition[j] += roll; gameover = playerposition[j] >= 100 now, if change code have structure
while(!gameover) { for(int = 0; < players; i++) { it should behave intended. full code becomes
int players = 5; bool gameover = false; int playerposition[players]; for(int i=0; < players; i++) { playerposition[i] = 1; } while(!gameover) { for(int = 0; < players; i++) { int roll; roll = dieroll(6); // dieroll function made simulate die roll playerposition[j] += roll; gameover = playerposition[j] >= 100 if (gameover) { break; } cout << "player " << j + 1 << " rolled " << roll << " , in position " << playerposition [j] << endl; } } }
Comments
Post a Comment