Pig is a simple dice game first described by John Scarne in 19451. It is a form of
jeopardy dice game, where a players decisions are between protecting current gains,
or risking current gains to make greater gains.
In Pig, each player takes a turn rolling a 6-sided dice, playing to reach a certain total
(typically 100). If a player rolls a value between 2 and 6 it is added to their score, and
they can roll again. If they roll a 1 (a “pig”), they lose their turn and add nothing to
their score. At any point after rolling a 2-6, the player may choose to to hold, which
ends their turn, and adds their turn total to their score. The winner is the first player to
get a score greater than or equal to the total.
When looking at the simple rules of the game, one can surmise that the game involves
some form of repetition to deal with the different turns of the players, and decision
statements to deal with the choice of roll or hold. The game also involves a random
number generator to simulate the dice. The game has two players, so an algorithm
may perform like this (starting with one player):
1. Ask the player whether they wish to roll or hold?
1.1. If roll is chosen, use the random number generator to generate a number
between 1 and 6.
1.1.1. If the number “rolled” is 1, go to step 2.
1.1.2. If the number “rolled” is 2-6, the value is added to the players roll total. Go
to step 1.
1.2. If hold is chosen, transfer the roll total to the players total. Go to Step 2.
2. Is one of the players total greater than or equal to the total?
2.1. Yes - the game ends.
1 Scarne, J., Scarne on Dice. Harrisburg, Pennsylvania: Military Service Publishing Co. (1945)
2.2. No - go to step 3.
3. Control is passed to the other player. Go to step 1.
Task
Design and implement a C program to play the Game of Pig. The game will be played
with two human players taking alternate turns. The total for the game (which is typically
100), should be input by the user before the game begins. The program should display
the dice rolled, for example:

Your program should include the following: decision statements, loops, character input,
random number generation.
See Program Guidelines at the end of this assignment.
Test Program Execution
An example of a program with these features is shown running below (user inputs are
bolded):

Welcome to the Game of Pig

What is the game total? 20

Player 1 (score = 0)

(r)oll or (h)old?

r
dice rolls a
+-------+
| O O |
| |
| O O |
+-------+
Roll total: 4
(r)oll or (h)old?
r
dice rolls a
+-------+
| O |
| |
| O |
+-------+
Roll total: 6
(r)oll or (h)old?
h
Player 1 score = 6

Player 2 (score = 0)

(r)oll or (h)old?

r
dice rolls a
+-------+
| |
| O |
| |
+-------+
Sorry, loose a turn!
Player 2 score = 0
Player 1 (score = 6)
--------------------------[Click Here](file:///C:/Users/Joseph97/Downloads/A1_f15%20(1).pdf)

hello guys, if anyone could help me out with this pig game ill appreciate it forever. im really lost and dont know how to code it completely. i also am confused with showing the dice in the way its been asked.

hope anyone can help out. thanks!

You need to start coding, implementing the steps you were given ...

and then to show us the code you tried.

Re. the unclear graphic output part ... you will have to clear that up with the source.

here's what ive done so far, any assistance would be appreciated. my code cant seem to complie for some reason i cant figure out whats wrong. any what is the next step i should do ater printing the dice?

P.S (i am very bad at coding and i know my sheet is filled with mistakes so sorry if its gonna give you a hard time)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int gamePlay()

int main(void)
{
    gamePlay();

    return 0;
}

{
    int gameTotal = 0;
    char PlayerSelection;

    printf("Welcome to the game of pig\n");
    printf("What is the game total?");
    scanf("%d", &gameTotal);

    printf("It's player one's turn\n");
    printf("Would you like to (r)oll or (h)old? ")
    scanf("&c", &playerSelection);
    printf("c\n", playerSelection);

    }
    if(playerSelection = 'r');
    {
    void showDice(int randomNumber);
    int showDice(randomNumber);

    }
    switch(randomNumber)    
{   
    if(randomNumber == 1);
    {
        printf("+--------+\n")
        printf("|        |\n")
        printf("|    0   |\n")
        printf("|        |\n")
        printf("+--------|\n")
    }   

    if(randomNumber == 2);
    {
        printf("+--------+\n")
        printf("| 0      |\n")
        printf("|        |\n")
        printf("|    0   |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 3);
    {
        printf("+--------+\n")
        printf("| 0      |\n")
        printf("|    0   |\n")
        printf("|      0 |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 4);
    {
        printf("+--------+\n")
        printf("|  0   0 |\n")
        printf("|        |\n")
        printf("|  0   0 |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 5);
    {
        printf("+--------+\n")
        printf("| 0    0 |\n")
        printf("|    0   |\n")
        printf("| 0    0 |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 6);
    {
        printf("+--------+\n")
        printf("| 0  0  0|\n")
        printf("|        |\n")
        printf("| 0  0  0|\n")
        printf("+--------|\n")
    }
     return 0;
     }

See example solutions to common beginner problems ...

1) take in valid int (with a prompt) so program won't crash
2) keep stdin flushed as you go ...
3) loop while more
4) takeInChr( withpromptStr ) to ease getting user choices

This demo may help you get started with clean working code ...

Note:
This demo does NOT entirely conform to the spec's you were given,
but is meant to help you get started.

/* rollDice.c */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void showDice( int randomNumber );
void playGame();


/* 3 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg );
int more(); /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg );




int main()
{
    do
    {
        playGame();
    }
    while( more() );

    return 0;
}





void playGame()
{
    int gameTotal = 0, player = 0, computer = 0;
    char choice = 'p'; /* start with player */

    srand( time(0) ); /* seed rand... */

    printf( "Welcome to the GAME of PIG ... \n\n" );

    gameTotal = takeInInt( "Enter the game total (for example 40): " );

    while( player < gameTotal && computer < gameTotal )
    {
        char roll;
        if( choice == 'p')
        {
            printf( "It's time for player ... \n" );
            do
            {
                roll = takeInChr( "Your choice roll or hold (r/h) ? " );
            }
            while( roll != 'r' && roll != 'h' );

            if( roll == 'r' )
            {
                int value = rand() % 6 + 1;
                player += value;
                showDice( value );
            }
            else
            {
                printf( "Ok ... hold it is ...\n" );
                choice = 'c'; /* computer */
            }
        }
        else
        {
            int value = rand() % 6 + 1;
            printf( "It's time for computer to roll ...\n" );
            computer += value;
            showDice( value );
            choice = 'p';
        }

        printf( "Player has %d and computer has %d\n", player, computer );
    }

    if( player < gameTotal  )
    {
        printf( "Player wins %d ...  computer went over max score of %d\n", player, gameTotal );
        return;
    }

    if( computer < gameTotal  )
    {
        printf( "Computer wins %d ...  player went over max score of %d\n", computer, gameTotal );
        return;
    }

    printf( "NO winner this time ... BOTH went over max score of %d\n", gameTotal );
}


void showDice(int ranNum )
{   
    switch( ranNum )
    {
        case 1:
        printf("+---------+\n");
        printf("|         |\n");
        printf("|    0    |\n");
        printf("|         |\n");
        printf("+---------|\n");
        break;

        case 2:
        printf("+---------+\n");
        printf("| 0       |\n");
        printf("|         |\n");
        printf("|      0  |\n");
        printf("+---------|\n");
        break;

        case 3:
        printf("+---------+\n");
        printf("| 0       |\n");
        printf("|    0    |\n");
        printf("|       0 |\n");
        printf("+---------|\n");
        break;

        case 4:
        printf("+---------+\n");
        printf("|  0   0  |\n");
        printf("|         |\n");
        printf("|  0   0  |\n");
        printf("+---------|\n");
        break;

        case 5:
        printf("+---------+\n");
        printf("| 0     0 |\n");
        printf("|    0    |\n");
        printf("| 0     0 |\n");
        printf("+---------|\n");
        break;

        case 6:
        printf("+---------+\n");
        printf("| 0  0  0 |\n");
        printf("|         |\n");
        printf("| 0  0  0 |\n");
        printf("+---------|\n");
    }
}





/* 3 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return 0;
    /* else ... */
    return 1;
}

/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg )
{
    int val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "\nInteger input only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}

Edited 1 Year Ago by David W

thanks a lot David, but it gives me a warning:

too many arguments for format [-Wformat-extra-args]

printf( "player wins! %d\n", player,  gameTotal );

too many arguments for format [-Wformat-extra-args]

printf( "computer wins! %d\n", computer,  gameTotal );

In function 'showDice':
warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]

printf( "|    0    |\n");

in function 'showDice' :
undefined reference to 'printf'

any idea how i can tackle these problems because it still cant seem to compile?

forget what i mentioned above.. i made some changes and the program ran somewhat fine. but i missed the part where if a player rolls a 1, the score should not be added to his total so it'll be the second players to roll. i couldnt seem to get that command to work so far; any idea?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void showDice( int randomNumber );
void playGame();

int takeInChr( const char* msg );
int more(); /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */

int takeInInt( const char* msg );
int main()
{
    do
    {
        playGame();
    }
    while( more() );
    return 0;
}
void playGame()
{
    int gameTotal = 0, playerOne = 0, playerTwo = 0;
    char choice = 'p'; /* start with player one */
    srand( time(0) ); /* seed rand... */
    printf( "Welcome to the GAME of PIG\n" );
    gameTotal = takeInInt( "Enter the game total (for example 40): " );
    while( playerOne < gameTotal && playerTwo < gameTotal )
    {
        char roll;
        if( choice == 'p')
        {
            printf( "It's player one's turn\n" );
            do
            {
                roll = takeInChr( "Your choice roll or hold (r/h) ? " );
            }
            while( roll != 'r' && roll != 'h' );
            if( roll == 'r' )
            {
                int value = rand() % 6 + 1;
                playerOne += value;
                showDice( value );
            }
            else
            {
                printf( "Ok ... hold it is\n" );
                choice = 'c'; /* playerTwo */
            }
        }
        else
        {
            int value = rand() % 6 + 1;
            printf( "It's player two's turn\n" );
            playerTwo += value;
            showDice( value );
            choice = 'p';
        }
        printf( "Player One has %d and player Two has %d\n", playerOne, playerTwo );
    }
    if( playerOne >= gameTotal  )
    {
        printf( "Player one wins %d\n", playerOne );
        return;
    }
    if( playerTwo >= gameTotal  )
    {
        printf( "playerTwo wins %d\n", playerTwo );
        return;
    }

}
void showDice(int ranNum )
{   
    switch( ranNum )
    {
        case 1:
        printf("+---------+\n");
        printf("|         |\n");
        printf("|    0    |\n");
        printf("|         |\n");
        printf("+---------|\n");
        break;
        case 2:
        printf("+---------+\n");
        printf("| 0       |\n");
        printf("|         |\n");
        printf("|      0  |\n");
        printf("+---------|\n");
        break;
        case 3:
        printf("+---------+\n");
        printf("| 0       |\n");
        printf("|    0    |\n");
        printf("|       0 |\n");
        printf("+---------|\n");
        break;
        case 4:
        printf("+---------+\n");
        printf("|  0   0  |\n");
        printf("|         |\n");
        printf("|  0   0  |\n");
        printf("+---------|\n");
        break;
        case 5:
        printf("+---------+\n");
        printf("| 0     0 |\n");
        printf("|    0    |\n");
        printf("| 0     0 |\n");
        printf("+---------|\n");
        break;
        case 6:
        printf("+---------+\n");
        printf("| 0  0  0 |\n");
        printf("|         |\n");
        printf("| 0  0  0 |\n");
        printf("+---------|\n");
    }
}

int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return 0;
    /* else ... */
    return 1;
}

int takeInInt( const char* msg )
{
    int val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );
        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "\nInteger input only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}

... but i missed the part where if a player rolls a 1,
the score should not be added to his total so it'll be the second players to roll.
i couldnt seem to get that command to work so far;
any idea?

Take a look at this demo ... that has ADDED EXTRA ROLLS for the 'computer' ...
if the 'computer rolls' certain values ...

/* rollDice2.c */  /* 2015-10-15 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
     ... i missed the part where if a player rolls a 1,
     the score should not be added to his total
     so it'll be the second players to roll.
     i couldnt seem to get that command to work so far;
     any idea?

     BUT added here...
     if computer rolls 1, 3, 5 then computer rolls again ...
*/


/* prototypes for 3 handy utilities for many C student coding problems ... */

/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg );
int takeInChr( const char* msg );
int more(); /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */



void showDice( int randNum );
void playGame();




int main() /* ******************************************* */
{
    do
    {
        playGame();
    }
    while( more() );

    return 0;
} /* **************************************************** */




void playGame()
{
    int gameTotal = 0, player = 0, computer = 0;
    char choice = 'p'; /* start with player, 'c' is for computer */

    srand( time(0) ); /* seed rand... */

    printf( "\nWelcome to the GAME of PIG ... \n\n" );

    gameTotal = takeInInt( "Enter the game total (for example 40): " );

    while( player < gameTotal && computer < gameTotal )
    {
        char roll;
        if( choice == 'p' )
        {
            printf( "\nIt's time for player ... \n" );
            do
            {
                roll = takeInChr( "Your choice roll or hold (r/h) ? " );
            }
            while( roll != 'r' && roll != 'h' );

            if( roll == 'r' )
            {
                int value = rand() % 6 + 1;
                showDice( value );
                player += value;
                if( value == 1 )
                {
                    choice = 'c';
                    printf( "\nOk ... now it is the computer's turn ..." );
                }
            }
            else
            {
                printf( "Ok ... hold it is ...\n" );
                choice = 'c'; /* computer */
            }
        }
        else /* HERE it IS the computer's choice  ...*/
        {
            int value = rand() % 6 + 1;
            takeInChr( "\nPress 'enter' when ready for computer to roll ... " );
            computer += value;
            showDice( value );
            if( value == 2 || value == 4 || value == 6 )
            {
                choice = 'p';
            }
        }

        printf( "\nPlayer has %d and computer has %d\n", player, computer );
    }

    if( player < computer )
    {
        if( computer > gameTotal )
            printf( "Player wins %d ...  computer went over max score of %d\n", player, gameTotal );
        else
            printf( "Computer wins %d ...  computer hit max score of %d\n", computer, gameTotal );
        return;
    }

    /* else computer < player ... */
    if( player > gameTotal )
        printf( "Computer wins %d ...  player went over max score of %d\n", computer, gameTotal );
    else
        printf( "Player wins %d ...  player hit max score of %d\n", player, gameTotal );
}


void showDice(int ranNum )
{
    printf("+---------+\n");
    switch( ranNum )
    {
    case 1:
        printf("|         |\n");
        printf("|    0    |\n");
        printf("|         |\n");
        break;
    case 2:
        printf("| 0       |\n");
        printf("|         |\n");
        printf("|      0  |\n");
        break;
    case 3:
        printf("| 0       |\n");
        printf("|    0    |\n");
        printf("|       0 |\n");
        break;
    case 4:
        printf("|  0   0  |\n");
        printf("|         |\n");
        printf("|  0   0  |\n");
        break;
    case 5:
        printf("| 0     0 |\n");
        printf("|    0    |\n");
        printf("| 0     0 |\n");
        break;
    case 6:
        printf("| 0  0  0 |\n");
        printf("|         |\n");
        printf("| 0  0  0 |\n");
    }
    printf("+---------+\n");
}



/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg )
{
    int val = 0;
    while( 1 ) /* loop forever until break is reached ... */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "\nInteger input only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}
/* 3 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int c = takeInChr( "\nMore (y/n) ? " );
    if( c == 'n' || c == 'N' ) return 0;
    /* else ... */
    return 1;
}

Edited 1 Year Ago by David W

im not sure this is whats asked. the rule is if a player rolls a value between 2 and 6 t is added to their score, and they can roll again. if they roll 1, they lose their turn and add nothing to their score. At any point after rolling a 2-6, the player may choose to hold, which ends their turn, and adds their turn total to their score. the winner is the first player to get a score greater than or equal to the total.

my when my code runs, this all is implemented except for the part where if any of the players roll a 1 (not 2, 4 or 6) they lose their turn and nothing is added to their score.

what could be the code to for that action to happen?

Look at the
'if ... else ...'
examples I gave you of different tests
and branching to different sections
by set / reset of 'flag' values
and see if you can code the exact solution that you wish?

If you get stuck ...
take a deep breath and 'think'

If ( condition is true ) do this
else do that

and try again ...
and if still stuck ... show the code you tried.

thank you so much for the help! the game runs smoothly.

#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'; /* start with player one */

    srand( time(0) );

    printf( "Welcome to the  Game of Pig\n");

    gameTotal = takeInInt( "What is the game total? ");

    while( playerOne < gameTotal && playerTwo < gameTotal )
    {

        char roll;
        if( choice == 'p' )
        {
            printf( "It's player one's turn\n");
            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\n" );

                }

        } 
        else
        {
            printf( "Alright, hold it is \n" );
            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\n", playerOne, playerTwo );
    }

     if( playerOne < playerTwo )          

    {
            if( playerTwo >= gameTotal )

                printf( "player one wins! %d\n", playerOne );
            else
                printf( " player two wins! %d\n", playerTwo );
            return;
     }

    if( playerOne >= gameTotal )
        printf( "player two wins! %d\n", playerTwo );
    else
        printf( "player one wins %d\n", playerOne );
}            





void showDice(int ranNum )
{
    switch( ranNum )
    {
            case 1:
            printf("+--------+\n");
            printf("|        |\n");
            printf("|   0    |\n");
            printf("|        |\n");
            printf("+--------+\n");
            break;


            case 2:
            printf("+--------+\n");
            printf("| 0      |\n");
            printf("|        |\n");
            printf("|      0 |\n");
            printf("+--------+\n");
            break;


            case 3:
            printf("+--------+\n");
            printf("| 0      |\n");
            printf("|    0   |\n");
            printf("|      0 |\n");
            printf("+--------+\n");
            break;


            case 4:
            printf("+--------+\n");
            printf("|  0   0 |\n");
            printf("|        |\n");
            printf("|  0   0 |\n");
            printf("+--------+\n");
            break;


            case 5:
            printf("+--------+\n");
            printf("| 0   0  |\n");
            printf("|   0    |\n");
            printf("| 0   0  |\n");
            printf("+--------+\n");
            break;


            case 6:
            printf("+--------+\n");
            printf("|0  0  0 |\n");
            printf("|        |\n");
            printf("|0  0  0 |\n");
            printf("+--------+\n");

     }
}




int takeInInt( const char* msg )
{
    int val = 0;
    while( 1 ) /* endless loop till break is reached */
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            break;
        else
        {
            printf( "Integer input only here please\n" );
            while( getchar() != '\n' ); 
        }
    }
    return val;
}

int takeInChr( const char* msg)
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() !='\n' );
    return chr;
}
int more()
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' )  return 0;

    return 1;

}

But ... I think you still have same 'logic errors' ?

Take a look:

/* gameStudent151020.c */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

const int MAX_TOT = 100;
const int MIN_TOT = 25;


/* 3 utilites used here... */
int takeInInt( const char* msg, const int min, const int max );
int takeInChr( const char* msg );
int more();
/* used for game */
void showDice( const int face );
void playGame();



int main()
{
    do
    {
        playGame();
    }
    while( more() );

    return 0;
}




void playGame()
{
    int gameTotal = 0, playerOne = 0, playerTwo = 0;
    int playerNum = 1; /* start with player one */

    srand( time(0) );

    printf( "Welcome to *** THE GAME OF PIG ***\n\n" );

    gameTotal = takeInInt( "What would you like to be the 'end-game' total? ", MIN_TOT, MAX_TOT );

    while( playerOne < gameTotal && playerTwo < gameTotal )
    {
        int roll;
        if( playerNum == 1 )
        {
            printf( "\nIt's player one's turn ... do you want to roll or hold " );
            do
            {
                roll = takeInChr( "(r/h) ? " );
            }
            while( !( roll == 'r' || roll == 'h') );

            if( roll == 'r' )
            {
                const int value = rand() % 6 +1;
                showDice( value );
                if( value == 1 ) /* switch players */
                    playerNum = 2;
                else /* only count score if value NOT 1 */
                    playerOne += value;
            }
            else
            {
                printf( "Alright, hold it is \n" );
                playerNum = 2;
            }
        }
        else
        {
            const int value = rand() % 6 +1;
            printf( "\nIt's player two's turn ... " );
            takeInChr( "press 'Enter' when ready to roll ... " );

            showDice( value );
            if( value == 1 ) /* switch players */
                playerNum = 1;
            else /* only count score if value NOT 1 */
                playerTwo += value;
        }

        printf( "Player one has %d and player two has %d\n", playerOne, playerTwo );

    } /* end while */

    /* when reach here ...
       one player has reached or exceeded the max allow game total
       so ... now test end game conditions ,,, and report ... */

    if( playerOne < playerTwo )
    {
        if( playerTwo > gameTotal )
            printf( "Player two 'went over' ... so ... player one wins %d!\n", playerOne );
        else
            printf( "Player two has high score and wins %d!\n", playerTwo );
        return;
    }

    /* else if reach here ... playerOne > playerTwo */

    if( playerOne > gameTotal )
        printf( "Player one 'went over' ... so ... player two wins %d!\n", playerTwo );
    else
        printf( "Player one has high score and wins %d!\n", playerOne );
}



void showDice( const int face )
{
    printf("+---------+\n");
    switch( face )
    {
    case 1:
        printf("|         |\n");
        printf("|    0    |\n");
        printf("|         |\n");
        break;
    case 2:
        printf("| 0       |\n");
        printf("|         |\n");
        printf("|      0  |\n");
        break;
    case 3:
        printf("| 0       |\n");
        printf("|    0    |\n");
        printf("|       0 |\n");
        break;
    case 4:
        printf("|  0   0  |\n");
        printf("|         |\n");
        printf("|  0   0  |\n");
        break;
    case 5:
        printf("| 0     0 |\n");
        printf("|    0    |\n");
        printf("| 0     0 |\n");
        break;
    case 6:
        printf("| 0  0  0 |\n");
        printf("|         |\n");
        printf("| 0  0  0 |\n");
    }
    printf("+---------+\n");
}


int takeInInt( const char* msg, const int min, const int max )
{
    int val = 0;
    while( 1 ) /* endless loop till break is reached */
    {
        printf( msg );
        fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
        {
            if( min <= val && val <= max )
                break;
            printf( "Valid range here is %d..%d ...\n", min, max);
        }
        else
        {
            printf( "Integer input only here please ...\n" );
            while( getchar() != '\n' );
        }
    }
    return val;
}
int takeInChr( const char* msg )
{
    int chr;
    printf( msg );
    fflush( stdout );
    chr = getchar();
    if( chr != '\n' )
        while( getchar() !='\n' );
    return chr;
}
int more()
{
    const int c = takeInChr( "\nMore (y/n) ? " );
    if( c == 'n' || c == 'N' )
        return 0;
    return 1;

}

Edited 1 Year Ago by David W

ohh alright, ill try modifying my code and see how it runs. but thanks a lot David, ill surely refer to this website whenever i need any help.

This question has already been answered. Start a new discussion instead.