c programming help The object of the game is to reach a score of 100. Each playe
ID: 3690746 • Letter: C
Question
c programming help
The object of the game is to reach a score of 100. Each player takes turns rolling a die. After rolling the die, the player is faced with one of two choices:
Your program should allow any number of players, prompt the user when the program starts.
If the player rolls a 1, the player forfeits their score for that round -and their turn is over
If the player rolls anything other than a 1, the number is added to their score for the round, they may then either roll again to increase their score or end their turn.
Note that for each round, the player can keep rolling as long as they want. If they roll a 6, 2, 3, 4, then end their turn, their score for the round would be 15.
If the player rolls a 6, 5, 3, 2, and 1 their score for the round will be 0 because they rolled a 1.
The game ends when a player reaches a score of 100.
Explanation / Answer
Please go through the code and description in the comments :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stdbool.h>
int roll(void)
{
int num = (rand()%6)+1;
return num;
}
/*
void player(void)
{
int i=0;
int sum=0;
int dice;
char turn;
while(i!=6)
{
printf("Player 1- roll or hold? ");
turn = getchar();
while(getchar()!=' ');
if(turn=='r')
{
dice = roll();
if(dice!=1)
{
sum+=dice;
printf("Rolled = %i Your turn total = %i ",dice,sum);
}
else
{
sum=0;
}
}
else if(turn =='h')
{
printf("Your total score is = %i ",sum);
}
i++;
}
}
*/
int sum1player2=0;
int player1(void);
void player2(void)
{
_Bool j=true;
int sum1=0;
int dice;
char turn;
while(j)
{
printf("Player 2- roll or hold? ");
turn = getchar();
//j++;
while(getchar()!=' ');
if(turn=='r')
{
dice = roll();
if(dice!=1)
{
sum1+=dice;
printf("Rolled = %i Your turn total = %i ",dice,sum1);
}
else
{
printf("Sorry, you rolled 1, Your score is 0 Next player turn.");
j=false;
}
}
else if(turn =='h')
{
j=false;
sum1player2+=sum1;
printf("Your total score is = %i ",sum1player2);
}
}
if(sum1player2 < 100)
{
player1();
}
else
{
printf("player 1 wins");
}
}
int sumPlayer1=0;
int player1(void)
{
_Bool k=true;
int sum=0;
int dice;
char turn;
while(k)
{
printf("Player 1 - roll or hold? ");
turn = getchar();
while(getchar()!=' ');
if(turn=='r')
{
dice = roll();
if(dice!=1)
{
sum+=dice;
printf("Rolled = %i Your turn total = %i ",dice,sum);
}
else
{
printf("Sorry, you rolled 1, Your score is 0 Next player turn.");
k=false;
}
}
else if(turn =='h')
{
k=false;
sumPlayer1+=sum;
printf("Your total score is = %i ",sumPlayer1);;
}
}
if(sumPlayer1 < 100)
{
player2();
}
else
{
printf("player 2 wins");
}
return 0;
}
int main(void)
{
srand(time(NULL));
player1();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.