I made a code for Yahtzee. My code asks user if they want to play for a Yahtzee or a straight. It then rolls until the Yahtzee or straight is achieved.
It works well but it takes a long time and way too many rounds, so I want my code to be more efficient and able to reroll on its own. So if the numbers rolled are 1 2 2 3 2, I want the computer to reroll the 1 and 3 until they become 2's.
I just started learning how to program in c++ so I have limited knowledge on the topic of programming. I know how to use loops, switch, if and else statements. Any suggestions or easy solutions would help.
I have some ideas on how to get the computer to reroll, but they don't seem to function. If you are able to figure out how to get the computer to reroll, please provide me with the modifications to my code and guide me as to how you did it.
int die3 = 0;
int die4 = 0;
int die5 = 0;
int roundCounter = 0;
bool yahtzeeNotFound = true;
bool yahtzeeGame = true;
char game[4] = "";
//ask user if they want to play for Yahtzee or Straight
printf("Lets play Yahtzee!\n Do you want to play for a Yahtzee or a Straight? (Y/S)");
scanf("%s", game);
//if user wants to play Yahtzee, program rolls for same numbers from all the dice.
if (tolower(game[0]) == 'y')
{
yahtzeeGame = true;
//this is included so that new numbers are rolled after each round.
srand(time(0));
//if the condition of an unfound Yahtzee is met, the program proceeds to enter the loop.
while (yahtzeeNotFound)
{
roundCounter++;
//the dice are all rolled at same time.
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
die3 = rand() % 6 + 1;
die4 = rand() % 6 + 1;
die5 = rand() % 6 + 1;
//the numbers rolled are printed for user to see.
//the counter counts each round, so user knows how many rounds it take for a Yahtzee.
printf("ROUND # %d\n\n", roundCounter);
printf(" Die 1 = %d\n", die1);
printf(" Die 2 = %d\n", die2);
printf(" Die 3 = %d\n", die3);
printf(" Die 4 = %d\n", die4);
printf(" Die 5 = %d\n\n\n\n", die5);
//when all the dice have rolled the same number, a Yahtzee is achieved.
if ((die1 == die2) && (die1 == die3) && (die1 == die4) && (die1 == die5))
{
printf(" Congratulations! You finally reached Yahtzee after %d rounds!\n", roundCounter);
//when the Yahtzee is achieved, the program exits the loop.
break;
}
}
}
else
{
//if the user does not play for a Yahtzee, they must play for a straight.
yahtzeeGame = false;
//this ensures that new number are rolled after each round.
srand(time(0));
//the program enters the loop.
while (yahtzeeNotFound)
{
//the counter is declared.
roundCounter++;
//the 5 dice are rolled at the same time.
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
die3 = rand() % 6 + 1;
die4 = rand() % 6 + 1;
die5 = rand() % 6 + 1;
//the numbers are printed out with the round number for the user to see.
printf("ROUND # %d\n\n", roundCounter);
printf(" Die 1 = %d\n", die1);
printf(" Die 2 = %d\n", die2);
printf(" Die 3 = %d\n", die3);
printf(" Die 4 = %d\n", die4);
printf(" Die 5 = %d\n\n\n\n", die5);
//if the numbers rolled have unique values, the user has a Straight.
if ((die1 != die2) && (die1 != die3) && (die1 != die4) && (die1 != die5) && (die2 != die3)
&& (die2 != die4) && (die2 != die5) && (die3 != die4) && (die3 != die5) && (die4 != die5))
{
// The user is told how many rounds it took for them to get a Straight.
printf(" Congratulations! You finally reached a Straight after %d rounds!\n", roundCounter);
//when the user has a Straight, the program exits the loop.
break;
}
}
}
return 0;
}