You are to finish the program below that implements the dice game PIG played by
ID: 3669524 • Letter: Y
Question
You are to finish the program below that implements the dice game PIG played by 2 human players. Pig is a dice game where each player during their turn rolls a 6-sided die. If the player rolls a 2 through 6, they decide whether they want to keep their turn score or roll again. If they roll a 1, the lose all of the points for that turn and their turn is over. The first player to reach 100 points or more wins.
sample output of thr program:
Welcome to the dice game Pig!
The objective is to be first to score 100 points.
Player 1 - Enter your name: Aragorn
Player 2 - Enter your name: Legolas
Aragorn
You rolled a 6
Your score: 6
Do you want to roll again? (y/n): y
Aragorn
You rolled a 2
Your score: 8
Do you want to roll again? (y/n): y
Aragorn
You rolled a 6
Your score: 14
Do you want to roll again? (y/n): n
Legolas
You rolled a 4
Your score: 4
Do you want to roll again? (y/n): y
Legolas
You rolled a 5
Your score: 9
Do you want to roll again? (y/n): n
Aragorn
You rolled a 6
Your score: 20
Do you want to roll again? (y/n): y
Aragorn
You rolled a 1 (PIG!)
Your turn is over
Your score: 14
Legolas
You rolled a 2
Your score: 11
Do you want to roll again? (y/n)
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int takeInInt( const char* msg );
int takeInChr( const char* msg );
int more();
void showDice( int randNum );
void playGame();
int main()
{
do
{
playGame();
}
while( more() );
return 0;
}
void playGame()
{
int gameTotal = 0, playerOne = 0, playerTwo = 0;
char choice = 'p';
srand( time(0) );
printf( " welcome to Game of Pig ");
gameTotal = takeInInt( "what is game total ");
while( playerOne < gameTotal && playerTwo < gameTotal )
{
char roll;
if( choice == 'p' )
{
printf( "It's player one's turn ");
do
{
roll = takeInChr( "Do you want to roll or hold (r/h) ? " );
}
while( roll != 'r' && roll != 'h' );
if( roll == 'r' )
{
int value = rand() % 6 +1;
showDice( value );
playerOne += value;
if( value == 1 )
{
choice = 'c';
printf( "It's player two's turn " );
}
}
else
{
printf( "Alright, hold it is " );
choice = 'c';
}
}
else
{
int value = rand() % 6 +1;
takeInChr( "Do you want to roll or hold (r/h) ?" );
playerTwo += value;
showDice( value );
if( value == 1 )
{
choice = 'p';
}
}
printf( "Player one has %d and player two has %d ", playerOne, playerTwo );
}
if( playerOne < playerTwo )
{
if( playerTwo >= gameTotal )
printf( "player one wins! %d ", playerOne );
else
printf( " player two wins! %d ", playerTwo );
return;
}
if( playerOne >= gameTotal )
printf( "player two wins! %d ", playerTwo );
else
printf( "player one wins %d ", playerOne );
}
void showDice(int ranNum )
{
switch( ranNum )
{
case 1:
printf("+--------+ ");
printf("| | ");
printf("| 0 | ");
printf("| | ");
printf("+--------+ ");
break;
case 2:
printf("+--------+ ");
printf("| 0 | ");
printf("| | ");
printf("| 0 | ");
printf("+--------+ ");
break;
case 3:
printf("+--------+ ");
printf("| 0 | ");
printf("| 0 | ");
printf("| 0 | ");
printf("+--------+ ");
break;
case 4:
printf("+--------+ ");
printf("| 0 0 | ");
printf("| | ");
printf("| 0 0 | ");
printf("+--------+ ");
break;
case 5:
printf("+--------+ ");
printf("| 0 0 | ");
printf("| 0 | ");
printf("| 0 0 | ");
printf("+--------+ ");
break;
case 6:
printf("+--------+ ");
printf("|0 0 0 | ");
printf("| | ");
printf("|0 0 0 | ");
printf("+--------+ ");
}
}
int takeInInt( const char* msg )
{
int val = 0;
while( 1 )
{
printf( msg ); fflush( stdout );
if( scanf( "%d", &val ) == 1 && getchar() == ' ' )
break;
else
{
printf( "Integer input only here please " );
while( getchar() != ' ' );
}
}
return val;
}
int takeInChr( const char* msg)
{
char chr;
printf( msg ); fflush( stdout );
chr = getchar();
if( chr != ' ' ) while( getchar() !=' ' );
return chr;
}
int more()
{
int c = takeInChr( "More (y/n) ? " );
if( c == 'n' || c == 'N' ) return 0;
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.