Question
Here is my C code in Yahtzee, my main problem is when I type something that's not in integer between 1-13, like typing an alphabetical
Here is my C code in Yahtzee, my main problem is when I type something that's not in integer between 1-13, like typing an alphabetical input, it will go for infinite loop, how can i use a do-while loop to avoid this? since I've been trying for so long and still couldn't figure it out.
#include "header.h"
//DISPLAY THE GAME MENU
void game_menu(void) {
int menu_option = 0;
do {
printf("Yahtzee: ");
printf("1. Print Game Rules. ");
printf("2. Play Game. ");
printf("3. Exit. ");
scanf("%d", &menu_option);
switch (menu_option) {
case 1:
system("cls");
printf("Hello ");
game_rules();
break;
case 2:
run_game();
break;
case 3:
printf("Goodbye! ");
//exit(1);
break;
default:
printf("Not an option! ");
}
}while (menu_option != 3);
}
//PRINT THE GAME RULES
void game_rules(void) {
printf("The scorecard used for Yahtzee is composed of two sections. A upper section and a lower section. ");
printf("A total of thirteen boxes or thirteen scoring combinations are divided amongst the sections. ");
printf("The upper section consists of boxes that are scored by summing the value of the dice matching the faces of the box. ");
printf("If a player rolls four 3's, then the score placed in the 3's box is the sum of the dice which is 12. ");
printf("Once a player has chosen to score a box, it may not be changed and the combination is no longer in play for future rounds. ");
printf("If the sum of the scores in the upper section is greater than or equal to 63, then 35 more points are added to the players overall score as a bonus. ");
printf("The lower section contains a number of poker like combinations. See the table provided below: ");
printf("******Name***********************Combination************************************Score*************** ");
printf("Three-of-a-kind Three dice with the same face Sum of all face values on the 5 dice ");
printf(" Four-of-a-kind Four dice with the same face Sum of all face values on the 5 dice ");
printf(" Full house One pair and a three-of-a-kind 25 ");
printf(" straight A sequence of four dice 30 ");
printf(" Large straight A sequence of five dice 40 ");
printf(" Yahtzee Five dice with the same face 50 ");
printf(" Chance May be used for any sequence of dice; Sum of all face values on the 5 dice ");
printf(" this is the catch all combination ");
}
//ROLL A DICE
int roll_dice(void) {
return (rand() % 6) + 1;
}
//SCORE for players
int run_game(void) {
int p1_score = 0, p2_score = 0;
for (int i = 0; i < 14; i++) {
printf("Player 1's turn! ");
p1_score += round_start();
printf("Player 1's total score: %d ", p1_score);
printf("Player 2's turn! ");
p2_score += round_start();
printf("Player 2's total score: %d ", p2_score);
if (p1_score >= 63) {
p1_score += 35;
if (p1_score > p2_score) {
printf("Player 1's score: %d, Player 2's score: %d, Player 1 wins!", p1_score, p2_score);
exit(1);
}
}
else if (p2_score >= 63) {
p2_score += 35;
if (p1_score < p2_score) {
printf("Player 1's score: %d, Player 2's score: %d, Player 2 wins!", p1_score, p2_score);
exit(1);
}
else {
printf("Player 1 and 2 have the same score: %d, the game ties.", p1_score);
exit(1);
}
}
}
}
//displays the results between two players
//PLAYER'S TURN
int round_start(void) {
printf("Press ENTER to roll the dice! ");
getchar(); getchar();
int dice_value[5], rolls = 0;
for (int i = 0; i < 5; i++)
dice_value[i] = roll_dice();
do {
//Players have three chances to re-roll
if (rolls < 3) {
char yn;
for (int i = 0; i < 5; i++)
printf("Dice %d: %d ", (i + 1), dice_value[i]);
//prompts players to choose the combinations
printf("Would you like to use this role for a game combination?(press Y for yes or N for no) ");
scanf(" %c", &yn);
if (yn == 'Y' || yn == 'y') {
break;
}
else if (yn == 'N' || yn == 'n') {
//prompt players if they want to take a chance to re-roll
for (int i = 0; i < 5; i++) {
char answer;
do {
printf("Would you like to re-roll the dice %d? ", (i + 1));
scanf(" %c", &answer);
answer = tolower(answer);
if (answer == 'y') {
dice_value[i] = roll_dice();
break;
}
else if (answer == 'n') {
dice_value[i] = dice_value[i];
break;
}
} while (answer != 'y' || answer != 'n');
}
}
rolls++;
}
else if (rolls == 3) {
break;
}
} while (rolls <= 3);
//displays the dice numbers and values
for (int i = 0; i < 5; i++)
printf("Dice %d. %d ", (i + 1), dice_value[i]);
//tells the players for the point of the round
int points = choose_comb(dice_value);
printf("Points of this round: %d ", points);
return points;
}
//Choose the combinations.
int choose_comb(int *dice_value) {
int choose_number = 0;
printf("1. Sum of 1's. ");
printf("2. Sum of 2's. ");
printf("3. Sum of 3's. ");
printf("4. Sum of 4's. ");
printf("5. Sum of 5's. ");
printf("6. Sum of 6's. ");
printf("7. Three of a Kind. ");
printf("8. Four of a Kind. ");
printf("9. Full House. ");
printf("10. Small Straight. ");
printf("11. Large Straight. ");
printf("12. Yahtzee. ");
printf("13. Chance ");
printf("Please choose an option: ");
scanf("%d", &choose_number);
//use the combinations depending player's option and calculate score
int count, sum = 0;
int dice_check[6] = { 0 };
for (int i = 0; i < 5; i++) {
sum += dice_value[i];
switch (choose_number) {
for (int i = 0; i < 5; i++)
sum += dice_value[i];
//Sum of ONES
case 1:
return sum_up_score(1, dice_value);
break;
//Sum of TWOS
case 2:
return sum_up_score(2, dice_value);
break;
//Sum of THREES
case 3:
return sum_up_score(3, dice_value);
break;
//Sum of FOURS
case 4:
return sum_up_score(4, dice_value);
break;
////Sum of FIVES
case 5:
return sum_up_score(5, dice_value);
break;
//Sum of SIXES
case 6:
return sum_up_score(6, dice_value);
break;
//THREE OF A KIND / FOUR OF A KIND
//since their combinations are in the same way, I grouped them in one function
case 7:
case 8:
for (int i = 0; i < 5; i++)
dice_check[dice_value[i] - 1] += 1;
int count = dice_check[0];
for (int i = 0; i <= 5; i++)
if (i == 4 && dice_check[4] < dice_check[5])
count = dice_check[5];
else if (dice_check[i] < dice_check[i + 1])
count = dice_check[i + 1];
if (count >= 3)
return sum;
break;
//FULL HOUSE
case 9:
for (int i = 0; i < 5; i++)
dice_check[dice_value[i] - 1] += 1;
for (int i = 0; i <= 5; i++)
for (int j = 0; j <= 5; j++)
if (dice_check[i] == 3 && dice_check[j] == 2) //check for three of a kind by i and pair by j
return 25;
break;
//SMALL STRAIGHT
case 10:
for (int i = 0; i < 4; i++)
count = 0;
for (int j = 0; j < 4; j++)
if (dice_value[i] = dice_value[j])
count++;
else
return 0;
if (count == 4)
return 30;
break;
//LARGE STRAIGHT
case 11:
for (int i = 0; i < 5; i++)
count = 0;
for (int j = 0; j < 5; j++)
if (dice_value[i] = dice_value[j])
count++;
else
return 0;
if (count == 5)
return 40;//check if there are 5 straight dices
break;
//YAHTZEE
case 12:
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
if (dice_value[i] != dice_value[j])
count++;
if (count == 5)
return 50;//check if there is a Yahtzee, if yes, return 50 points
break;
//CHANCE
case 13:
return sum;
break;
default:
printf("Please choose an option: ");
do {
choose_comb(dice_value);
} while ((isdigit(choose_number) == 0) || (choose_number < 1) || (choose_number > 13));
}
}
return 0;
}
//ADD UP THE SCORES
int sum_up_score(int quantity, int dice_value[]) {
int count = 0;
for (int i = 0; i < 5; i++)
if (dice_value[i] == quantity)
count++;
return count * quantity;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started