# include # include # include using namespace std; int main ( ) // // CRAPS simulates the game of craps. // // On the first roll, the player: // WINS by getting a 7 or 11; // LOSES by getting a 2, 3 or 12; // CONTINUES otherwise, and the number rolled is the POINT. // // On subsequent rolls, the player: // WINS by rolling POINT again; // LOSES by rolling 7. // { int count; char next; int point; int result; int roll_two_dice ( int count ); int score; int seed; // // Initialization: // seed = time ( 0 ); srand ( seed ); // // The first roll: win, lose or set the point. // count = 1; score = roll_two_dice ( count ); switch ( score ) { case 7: case 11: result = +1; break; case 2: case 3: case 12: result = -1; break; default: result = 0; point = score; cout << "\n"; cout << " The player must roll until making " << point << " again."; break; } // // If RESULT is 0, we haven't won or lost. // while ( result == 0 ) { next = cin.get ( ); count = count + 1; score = roll_two_dice ( count ); if ( score == point ) { result = +1; } else if ( score == 7 ) { result = -1; } } if ( result == +1 ) { cout << "\n"; cout << " The player wins.\n"; } else { cout << "\n"; cout << " The house wins.\n"; } return 0; } int roll_two_dice ( int count ) // // ROLL_TWO_TWICE simulates the roll of two dice, // prints the values and returns the sum. // { int die1; int die2; int random_int ( int a, int b ); int score; die1 = random_int ( 1, 6 ); die2 = random_int ( 1, 6 ); score = die1 + die2; cout << " " << count << ": " << die1 << " + " << die2 << " = " << score; return score; } int random_int ( int a, int b ) // // RANDOM_INT returns a random int between a and b. // { int range; int value; // // If we want integers between A and B, there are actually // B - A + 1 values. // range = b - a + 1; value = a + rand ( ) % range; return value; }