I am writting a program in C for the game of yahtzee. Right now my program asks
ID: 3678815 • Letter: I
Question
I am writting a program in C for the game of yahtzee. Right now my program asks if the player wants to reroll their dice, but I need the program to ask which dice to reroll and then only reroll those dice, not all 5 dice. How would this code look?? So far I have 2 functions which I call in my play_game function. The output asks for "which dice to reroll" but doesnt actually reroll and display new values, just the old ones. Help!
int reroll(int count)
{
int reroll_index = 0;
int num_reroll = 0;
int dice[5] = { 0 };
printf("How many dice would you like to reroll? ");
scanf("%d", &num_reroll);
for (count = 0; count < num_reroll; ++count)
{
printf("Which die would you like to reroll (1 - 5): ");
scanf("%d", &reroll_index);
dice[reroll_index - 1] = rand() % 6 + 1; // replace the die with another
}
system("pause");
system("cls");
}
void print_dice(int *dice, int size)
{
int index = 0;
for (index = 0; index < size; ++index)
{
printf("%d: %d ", index + 1, dice[index]);
}
}
Explanation / Answer
int reroll(int count)
{
int reroll_index = 0;
int num_reroll = 0;
int dice[5] = { 0 };
printf("How many dice would you like to reroll? ");
scanf("%d", &num_reroll);
for (count = 0; count < num_reroll; ++count)
{
printf("Which die would you like to reroll (1 - 5): ");
scanf("%d", &reroll_index);
dice[reroll_index - 1] = rand() % 6 + 1; // replace the die with another
}
print_dice(dice, num_reroll);
system("pause");
system("cls");
}
void print_dice(int *dice, int size)
{
int index = 0;
for (index = 0; index < size; ++index)
{
printf("%d: %d ", index + 1, dice[index]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.