#include #include using namespace std; // declare our own function here: // (1) return type (int) // (2) name of the function (roll_dice) // (3) input parameters OPTIONAL int roll_dice() { int d1 = rand() % 6; // 0-5 d1++; // 1-6 int d2 = rand() % 6 + 1; // 1-6 int roll = d1 + d2; return roll; } int main() { srand( time( 0 ) ); // randomize // States of the game system const int NEW_GAME = 1; const int FIRST_ROLL = 2; const int SUBSEQUENT_ROLL = 3; const int WIN = 4; const int LOSE = 5; const int GAME_OVER = 6; int game_state = NEW_GAME; bool game_over = false; int roll, point; while ( game_over == false ) // !game_over { switch ( game_state ) { case NEW_GAME: cout << "Welcome to the Casino!\n"; game_state = FIRST_ROLL; break; case FIRST_ROLL: roll = roll_dice(); cout << "You rolled: " << roll << endl; if ( roll == 7 || roll == 11 ) { game_state = WIN; } else if ( roll == 2 || roll == 12 ) { game_state = LOSE; } else { point = roll; cout << "(so your point is " << point << ")\n"; game_state = SUBSEQUENT_ROLL; } break; case SUBSEQUENT_ROLL: roll = roll_dice(); cout << "You rolled: " << roll << endl; if ( roll == 7 || roll == 11 ) { game_state = LOSE; } else if ( roll == point ) { game_state = WIN; // } else { // game_state = SUBSEQUENT_ROLL; } break; case WIN: cout << "YOU WIN!\n"; game_state = GAME_OVER; break; case LOSE: cout << "YOU LOSE!\n"; game_state = GAME_OVER; break; case GAME_OVER: cout << "Game over....\n"; game_over = true; break; default: cerr << "Something's wrong!\n"; game_over = true; break; } } return 0; }