#include #include using namespace std; // (1) return value data type is int // (2) name the function (e.g. roll_dice) // (3) inputs to the function in () OPTIONAL int roll_dice() { // roll the dice: int d1 = rand() % 6 + 1; // 1-6 int d2 = rand() % 6 + 1; // 1-6 int roll = d1 + d2; cout << "You rolled " << roll << endl; return roll; } // function first_roll_wins() that returns bool // value showing whether the first dice roll // is a win or not // The "int r" is an input variable representing // the dice roll bool first_roll_wins( int r ) { if ( r == 7 || r == 11 ) { return true; } else { return false; } } // TO DO: create a subsequent_roll_wins() function int main() { // Dice game CRAPS: // // FIRST DICE ROLL: // player wins if s/he rolls 7 or 11 // player loses if s/he rolls 2 or 12 // otherwise, the dice roll is the POINT // EACH SUBSEQUENT ROLL: // player loses if s/he rolls 7 or 11 // player wins if s/he rolls the POINT // // Roll #1: 8 <== point value // Roll #2: 9 // Roll #3: 10 // ... // Roll #?: 8 YOU WIN! srand( time( 0 ) ); // randomize // roll the dice: int roll = roll_dice(); bool game_over = false; if ( first_roll_wins( roll ) ) { cout << "YOU WIN!\n"; game_over = true; } else { cout << "THAT'S YOUR POINT VALUE.\n"; } // switch ( roll ) // { // case 7: case 11: // cout << "YOU WIN!\n"; // game_over = true; // break; // case 2: case 12: // cout << "YOU LOSE.\n"; // game_over = true; // break; // default: // cout << "THAT'S YOUR POINT VALUE.\n"; // break; // } if ( !game_over ) // i.e. ( game_over == false ) { int point = roll; // roll the dice: int roll = roll_dice(); while ( roll != 7 && roll != 11 && roll != point ) { // roll the dice: roll = roll_dice(); } // if ( roll == 7 || roll == 11 ) // { // cout << "YOU LOSE.\n"; // } // else if ( roll == point ) // { // cout << "YOU WIN!\n"; // } switch ( roll ) { case 7: case 11: cout << "YOU LOSE.\n"; break; default: cout << "YOU WIN!\n"; break; } } return 0; }