In: Computer Science
PROGRAMMING IN C-DICE GAME
Q1.
A player rolls two dice at the same time. Each die has six faces, which contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated.
(i) If the sum is 2 or 10 on the first throw, the player wins.
(ii) If the sum is 3, 7 or 12 on the first throw, the player loses.
(iii) If the sum is 4, 5, 6, 8, 9 or 11 on the first throw, the sum becomes the player’s point. To win, the player must continue rolling the dice until he/she makes the point again. However, the player loses by rolling a 10 before making the point.
Thanks for the question. Here is the complete program in C.
=======================================================================
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
srand(time(NULL));
int dice_one=0, dice_two=0;
char c;
printf("Welcome!\n");
while(1){
printf("Hit any key to roll the dice.");
scanf("%c",&c);
dice_one=rand()%6 +1;
dice_two=rand()%6 +1;
printf("You rolled %d %d in your first throw. Total = %d\n",dice_one,dice_two,dice_one+dice_two);
if(dice_one+dice_two==2 || dice_one+dice_two==10){
printf("You Win!\n");
break;
}
else if(dice_one+dice_two==3 || dice_one+dice_two==7 ||dice_one+dice_two==12){
printf("You Lose!\n");
break;
}
int points = dice_one+dice_two;
printf("\nYour point now is %d. You must get %d to win now.\n\n",points,points);
do{
printf("\nHit any key to roll the dice.\n");
scanf("%c",&c);
dice_one=rand()%6 +1;
dice_two=rand()%6 +1;
printf("You rolled %d %d.Total = %d\n",dice_one,dice_two,dice_one+dice_two);
if(dice_one+dice_two==points){
printf("You Win!\n\n",points);
}
else if(dice_one+dice_two==points){
printf("Your rolled 10. You Lose!\n");
}
}while(dice_one+dice_two!=points && dice_one+dice_two!=10);
break;
}
}
=======================================================================