// Written By Robert Gleeson. // The "craps" game. #include #include #include using namespace std; // function prototypes. int rolldice(); int main() { int total; // int to store the total of the dice roll. srand (time(0)); // set a seed for randomized numbers. total = rolldice(); // assign the total of the dice roll to "total". if( (total == 7 || total == 11) ) // if we hit 7 or 11 and the dice has only been rolled once, user wins :) std::cout << "You win!\n"; else if ( (total == 2 || total == 3 || total == 12)) // if we hit 2,3, or 12 on the first run the user lose's :( std::cout << "You lose!\n"; else // The rolled number was either 4,5,6,8,9,10 .. add it to point variable and see if the next roll equals it, if it does the user wins but { // if they hit 7 the user lose's! int point = total; // store the point. while(total = rolldice() ) // loop to generate random numbers until one of the conditions in its body are met. { if(point == 7) { // user lose's. std::cout << "You lose! \n"; break; } else if(total == point) { // user win's. std::cout << "You win!\n"; break; } } } return(0); } int rolldice() { int diceA; // stores the first dice roll. int diceB; // stores the second dice roll. diceA = 1 + rand() % 6; // generate a random number between 1 and 6. diceB = 1 + rand() % 6; // generate a random number between 1 and 6. std::cout << "You rolled a " << diceA << " and a " << diceB << "\n"; std::cout << "Your total dice roll is " << diceA + diceB << "\n"; return diceA + diceB; }