int main()
{
int dice[10] = {0};
int counter = 1,Player_Dice = 0,Computer_Dice = 0;
bool score = true, cont = true, gameover = false;
srand(unsigned(time(0)));
unsigned int userScore = 0, computerScore = 0;
while (!gameover)
{
do
{
cout << "Choose how many dice to roll, Must be less than 11: ";
cin >> Player_Dice;
if(Player_Dice < 10)
++counter;
}
while(Player_Dice > 10);
for(int i = 0; i < Player_Dice; ++i)
{
dice[i] = rand()%6+1;
cout << dice[i] << endl;
if(i > Player_Dice)
break;
} for(int i = 0; i < Player_Dice; ++i)
{
if(dice[i] == 1)
{
cout << "Player recieves 0 points." << endl;
score = false;
break;
}
}
for(int i = 0; i < Player_Dice; ++i)
{
if(score = true)
userScore += dice[i];
}
cout << "Your score is " << userScore << endl;
}
return0;
}
The program will ask for how many dice to roll and then generate randomly the value of each die. If no 1 appears then it's suppose to add up all the dice and add it to the cumulative score, but even if a 1 shows up the program still adds it to the cumulative score.
while (!gameover)
{
do
{
cout << "Choose how many dice to roll, Must be less than 11: ";
cin >> Player_Dice;
if (Player_Dice < 10) // why not < 11?
++counter;
} while (Player_Dice > 10);
for (int i = 0; i < Player_Dice; ++i)
{
dice[i] = rand()%6+1;
cout << dice[i] << endl;
if (i > Player_Dice) // why?
break;
}
for (int i = 0; i < Player_Dice; ++i)
{
if (dice[i] == 1)
{
cout << "Player recieves 0 points." << endl;
score = false;
break;
}
}
for (int i = 0; i < Player_Dice; ++i)
{
if (score = true) // assign, not equality test
userScore += dice[i];
}
cout << "Your score is " << userScore << endl;
}
Why are lines 7 and 8 inside the loop. Why test < 10?
Sorry I should have explained that I am also trying to run AI so 7 and 8 are used to determine when the AI should go (every even turn). The Maximum ammount of dice you can roll is 10. I may need to check my logic on that. I think that your last comment is what the problem was.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int P_dice[10] = {0}, C_dice[10] = {0};
int counter = 1,Player_Dice = 0,Computer_Dice = 0;
bool score = true, cont = true, gameover = false;
srand(unsigned(time(0)));
unsignedint userScore = 0, computerScore = 0;
while (!gameover)
{
do
{
cout << "Choose how many dice to roll, Must be less than 11: ";
cin >> Player_Dice;
if(Player_Dice <= 10)
++counter;
}
while(Player_Dice > 10);
for(int i = 0; i < Player_Dice; ++i)
{
P_dice[i] = rand()%6+1;
cout << P_dice[i] << endl;
}
for(int i = 0; i < Player_Dice; ++i)
{
if(P_dice[i] == 1)
{
cout << "Player recieves 0 points." << endl;
score = false;
}
}
if(score == true)
{
for(int i = 0; i < Player_Dice; ++i)
userScore += P_dice[i];
}
cout << "Your Score is " << userScore << endl;
if(score == false)
score = true;
Computer_Dice = 3;
for(int i = 0; i < Computer_Dice; ++i)
{
C_dice[i] = rand()%6+1;
cout << C_dice[i] << endl;
}
for(int i = 0; i < Computer_Dice; ++i)
{
if(C_dice[i] == 1)
cout << "Computer recieves 0 points." << endl;
score = false;
}
if(score == true)
{
for(int i = 0; i < Computer_Dice; ++i)
computerScore += C_dice[i];
}
cout << "The Computer's Score is " << computerScore << endl;
}
return 0;
}
So I cleaned out the if breaks with the player dice not thinking that I already had it in a for loop. Now When I try to report back the scores it wont add them. I think this is due to the fact that score stays false once it is false and therefore will not add the scores in again until it is reinitialize to true. I using an if statement stating if score was false to change to true at the end but it did not work. What am I missing?
To be honest, I find the code hard to read without indentation and line breaks to separate each block. I copied and pasted it to an editor, just so I could read it.
First a minor point of style, doesn't affect the outcome:
1 2 3 4 5 6 7 8
do
{
cout << "Choose how many dice to roll, Must be less than 11: ";
cin >> Player_Dice;
if(Player_Dice <= 10)
++counter;
}
while (Player_Dice > 10);
Compare this, it has the same outcome, but is simpler to understand:
1 2 3 4 5 6 7 8
do
{
cout << "Choose how many dice to roll, Must be less than 11: ";
cin >> Player_Dice;
}
while (Player_Dice > 10);
++counter;
Now this leaped out at me, without even understanding what the program was supposed to do:
1 2 3 4 5 6 7 8
for (int i = 0; i < Player_Dice; ++i)
{
if (P_dice[i] == 1)
{
cout << "Player recieves 0 points." << endl;
score = false;
}
}
1 2 3 4 5 6
for (int i = 0; i < Computer_Dice; ++i)
{
if (C_dice[i] == 1)
cout << "Computer recieves 0 points." << endl;
score = false;
}
Notice how the computer's dice are handled differently to the player's dice?
Quote:
I using an if statement stating if score was false to change to true
I think it would be more conventional to omit the "if" and simply set it to true, always.
And remember the rhyme, 'i' before 'e' except after 'c' :)
Thank you Chervil for all your help! I just have one more question. How would i go about returning to the while loop if I wanted to create a continue outside of the while loop?