N Player Dice Games The program is to be written in Matlab. Program must be libe
ID: 3589584 • Letter: N
Question
N Player Dice Games
The program is to be written in Matlab. 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
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N,M,Tmp,Tot,i,end=0,dice1,dice2,has50=0,hasM=0,winner=0;
printf("Enter number of players and number of points needed to win.. ");
scanf("%d%d",&N,&M);
int Player[N];
for(i=0;i<N;i++)
Player[i]=0;
while(!end)
{
for(i=0;i<N;i++)
{
int inning = 1;
Tmp=0;
while(inning)
{
//printf("Enter value for two dice.. ");
do
{
scanf("%d%d",&dice1,&dice2);
}while(!(dice1>=1&&dice1<=6&&dice2>=1&&dice2<=6));//prompt again if typo in input
if(dice1==1||dice2==1)
{
Tmp=0;
inning=0;
}
Tmp=Tmp+dice1+dice2;
}
Player[i]+=Tmp;
if(Player[i]>=50)
{
has50=1;
inning=0;
}
if(Player[i]>=M)
hasM=1;
}
//at this point, all the players have same number of innings
if(has50||hasM)
{
end=1;
}
}
printf("Score of players.. ");
for(i=0;i<N;i++)
{
printf("Player%d : %d ",i+1,Player[i]);
if(Player[winner]<Player[i])
winner=i;
}
printf("And winner is Player%d",winner+1);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.