I am brand new to c++, I have been taking a c++ class for about a month, and our current assignment is to program Yahtzee. My current problem is that I am having a problem when I ask the user if they would like to keep or reroll certain numbers. It will reroll every number no matter what I input. Could someone help me so I can continue coding this? My current code is as follows
#include <iostream>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
int main () {
int rand ( void );
int dice[5];
int turn;
char reroll[5];
dice[0]=rand() % 6+1;
dice[1]=rand() % 6+1;
dice[2]=rand() % 6+1;
dice[3]=rand() % 6+1;
dice[4]=rand() % 6+1;
turn=1;
while (turn<=2) {
cout << "Turn #" << turn;
cout << "Your first roll is: " << dice[0] << dice[1] << dice[2] << dice[3] << dice[4] << " (Type 'y' to re-roll or 'n' to stay)";
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
if (reroll[0]='y'){
dice[0]=rand() % 6+1;}
if(reroll[0]='n'){
dice[0];}
if (reroll[1]='y'){
dice[1]=rand() % 6+1;}
if (reroll[2]='y'){
dice[2]=rand() % 6+1;}
if (reroll[3]='y'){
dice[3]=rand() % 6+1;}
if (reroll[4]='y'){
dice[4]=rand() % 6+1;}
cout << "Your second Roll is " << dice [0] << dice[1] << dice [2] << dice[3] << dice[4] << " (Type 'y' to re-roll or 'n' to stay)";
dice[0]=rand() % 6+1;
dice[1]=rand() % 6+1;
dice[2]=rand() % 6+1;
dice[3]=rand() % 6+1;
dice[4]=rand() % 6+1;
turn++;}
return 0;
}
it is obvious that this code is not finished, it does not keep score and it does not continue on to the third roll for each turn, but I simply need help with saving some numbers and rerolling others before I move on to those.
Could you explain briefly what the game is meant to do. I assume from your comment you would pretty much go through that middle section once more, so that you can re-roll each dice twice if you want. But how does Yahtzee scoring work? My only understanding of Yahtzee is from that one southpark episode. And he just wins everytime straight away.
Basically in Yahtzee you roll 5 dice (or random numbers from 1-6 in this case) and based upon combinations of those dice, such as a pair of ones, a straight, or all dice reading the same, you gain points. When playing you are given the option to re roll some of your dice twice, and that it what I am having issues with. The user will type ynynn and that would mean they want to re roll dice[0] and dice[2], but the program simply generates a new value for all of the dice, and I am not sure why.
ah I see. Immediately after you do cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4]; check the values of each of your rerolls. And make sure that they are what you think that they should be. If you don't know any better way just do cout <<"reroll[0]: "<< reroll[0] << endl; for each one.