N Player Dice Games The program is to be written in C . Program must be liberall
ID: 3737193 • Letter: N
Question
N Player Dice Games The program is to be written in C . Program must be liberally commented. The prompts must be clear and allow for correcting typos. i.e. if the input must be "y" or "n" any other input would be ignored and the prompt repeated. The program will allow the entering of N, the number of players and M, the number of points needed to win. During an inning, the player rolls two dice. As long as neither die is a "1" the player may continue rolling and adding the total to a variable, Tmp, which at the end of the inning is added to player i's total Tot(i). Where i is the player who just rolled the dice. If the player rolls a "1" turn stops and Tmp = 0 for that inning. Game ends when one player has Tot(i) of M or more points and all players have had the same number of innings. If player i < N gets to 50 first, the remaining players have one last turn. At the end of the game, the scores of all players must be clearly shown and the winner indicated. Player = [ 0 … 0 ] at start of the game Tmp = 0 at start of the player's inning.
Explanation / Answer
Screenshot
----------------------------------------------------------------------------------------------------------
Program
//Header file
#include <stdio.h>
#include <stdlib.h>
//main function
int main(void) {
//Variable declaration
int n,m,temp=0,dice1,dice2,j=0;
//Ask user for number of players
printf("Enter the number of players : ");
scanf("%d ",&n);
//array for score
int arr[n];
//Ask user for the score needed
printf("Enter the number of points needed : ");
scanf("%d",&m);
//Dice values generation
dice1=(rand()%6)+1;
dice2=(rand()%6)+1;
//loop through n players
for(int i=0;i<n;i++){
check whether get 1
while(dice1>1 && dice2>1)
{
//steps to generate scores
if( j<((m/12)+2)){
//value comparison with the score needed
if(temp<m){
temp+=dice1+dice2;
dice1=(rand()%6)+1;
dice2=(rand()%6)+1;
}
else{
printf("Your turn is over ");
break;
}
}
j=j+1;
}
//store values in array
arr[i]=temp;
temp=0;
dice1=(rand()%6)+1;
dice2=(rand()%6)+1;
}
//Display the winner
for(int i=0;i<n;i++){
if(arr[i]>=m){
printf("The winner is Player%d ",i+1);
}
}
//Array of user score
printf("[");
for(int i=0;i<n;i++){
printf("%d ,",arr[i]);
}
printf("]");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.