This assignment is to write a program that plays a dice game with the user. Firs
ID: 3623790 • Letter: T
Question
This assignment is to write a program that plays a dice game with the user. First study the program diceGameEZ.c which plays a simple dice game.Next, implement the dice game described below by thoughtfully modifying the first program. (Or start from scratch if you want.)
/* Dice Game Five
|
| The game consists of 7 rounds.
| Each round consists of a human turn follows by a computer turn.
|
| In the human's turn, the human throws a die, and can either accept
| the throw or throw again. After each throw, the human's score
| for the round is the number showing on the last die thrown.
| The human can throw as many times as they want.
|
| In the computer's turn, the computer throws the die as many times
| as the human did, but the computer's score for the round is the
| maximum of all the numbers shown in the throws.
|
| The winner of the round is the player with the highest score for the
| round. In case of a tie, the computer wins.
|
| The winner of the game is the player who has won the most rounds.
| In case of a tie, neither wins.
|
*/
#include <stdio.h>
#include <stdlib.h>
/* Easy dice game
|
| The game consists of 7 rounds.
| In each round, the computer throws a die,
| then the human throws a die.
| The winner of the round is the player who has the highest throw.
| In case of a tie, neither player wins.
| The winner of the game is the player who has won the most rounds.
|
*/
char input[132]; /* user input buffer */
int throwDie()
{
static int initialized = 0;
int num;
if ( !initialized ) { srand( time(NULL) ); initialized = 1;}
num = (rand()*6)/(RAND_MAX+1) + 1 ;
return num;
}
/* Human turn
|
| This might be mode made interesting in the future.
|
*/
int humanTurn()
{
int toss;
toss = throwDie();
printf("Human throws a %d ", toss );
return toss;
}
/* Computer turn
|
| This might be made more interesting in the future.
|
*/
int computerTurn()
{
int toss;
toss = throwDie();
printf("Computer throws a %d ", toss );
return toss;
}
int main(int argc, char *argv[])
{
int round, humanWins=0, computerWins=0 ;
int humanToss, computerToss;
const int numberOfRounds = 7;
/* Play 13 Rounds */
for ( round = 1; round<=numberOfRounds; round++ )
{
printf(" Round %d ", round );
printf("Player's Turn: (hit enter)");
gets( input ); /* pause for dramatic effect */
humanToss = humanTurn();
printf("Computer's Turn: (hit enter)");
gets( input ); /* pause for dramatic effect */
computerToss = computerTurn();
/* Determine Winner of the Round */
if ( humanToss > computerToss )
{
humanWins++;
printf(" Human wins the round. human: %3d. computer: %3d ",
humanWins, computerWins );
}
else if ( computerToss > humanToss )
{
computerWins++;
printf(" Computer wins the round. human:%3d. computer: %3d ",
humanWins, computerWins );
}
else if ( computerToss == humanToss)
{
printf(" Tie. human:%3d. computer: %3d ",
humanWins, computerWins );
}
}
/* Determine Winner to the Game */
if ( humanWins > computerWins )
printf(" WINNER!! The human wins the game! ");
else if ( computerWins < humanWins )
printf(" The computer wins the game! ");
else
printf(" Tie Game! ");
printf(" ");
system("pause");
return 0;
}
Explanation / Answer
please rate - thanks
message me if any problems
#include <stdio.h>
#include <stdlib.h>
/* Easy dice game
|
| The game consists of 7 rounds.
| In each round, the computer throws a die,
| then the human throws a die.
| The winner of the round is the player who has the highest throw.
| In case of a tie, neither player wins.
| The winner of the game is the player who has won the most rounds.
|
*/
char input[132]; /* user input buffer */
int throwDie()
{
static int initialized = 0;
int num;
if ( !initialized ) { srand( time(NULL) ); initialized = 1;}
num = (rand()*6)/(RAND_MAX+1) + 1 ;
return num;
}
/* Human turn
|
| This might be mode made interesting in the future.
|
*/
int humanTurn()
{
int toss;
toss = throwDie();
printf("Human throws a %d ", toss );
return toss;
}
/* Computer turn
|
| This might be made more interesting in the future.
|
*/
int computerTurn()
{
int toss;
toss = throwDie();
printf("Computer throws a %d ", toss );
return toss;
}
int acceptIt()
{char in;
do
{printf("Do you accept that toss? (enter Y or N):");
in=getchar( );
getchar(); //get rid of the enter
in=toupper(in);
if(in=='Y')
return 1;
else if(in=='N')
return 0;
else
printf("must enter Y or N ");
}while(1);
}
int maxx(int n,int m)
{if(n>m)
return n;
return m;
}
int main(int argc, char *argv[])
{
int round, humanWins=0, computerWins=0 ;
int humanToss, computerToss;
const int numberOfRounds = 7;
int humanScore,computerScore,tosses,i,num,accept;
/* Play 13 Rounds */
for ( round = 1; round<=numberOfRounds; round++ )
{
printf(" Round %d ", round );
printf("Player's Turn: ");
tosses=0;
do
{printf("Toss: (hit enter)");
gets( input ); /* pause for dramatic effect */
humanToss = humanTurn();
tosses++;
}while(!acceptIt());
printf(" Computer's Turn: ");
printf("Computer gets %d tosses ",tosses);
computerToss=0;
for(i=0;i<tosses;i++)
{printf("toss %d (hit enter)",i+1);
gets( input ); /* pause for dramatic effect */
num = computerTurn();
computerToss=maxx(computerToss,num);
}
printf("This round computer throws a %d ", computerToss);
/* Determine Winner of the Round */
if ( humanToss > computerToss )
{
humanWins++;
printf(" Human wins the round. human: %3d. computer: %3d ",
humanWins, computerWins );
}
else if ( computerToss > humanToss )
{
computerWins++;
printf(" Computer wins the round. human:%3d. computer: %3d ",
humanWins, computerWins );
}
else if ( computerToss == humanToss)
{
printf(" Tie. human:%3d. computer: %3d ",
humanWins, computerWins );
}
}
/* Determine Winner to the Game */
if ( humanWins > computerWins )
printf(" WINNER!! The human wins the game! ");
else if ( computerWins < humanWins )
printf(" The computer wins the game! ");
else
printf(" Tie Game! ");
printf(" ");
system("pause");
return 0;
}