// Fig. 3.10: fig03_10.cpp // Craps // 181H mods 10/19/98 #include #include #include int rollDice( void ); // function prototype int playAGame(); int main( int argc, char* argv[] ) { int numPlays = 1; if ( argc > 1 ) { // atoi converts a string of digits to integer, // the prototype is: int atoi( char s[] ) numPlays = atoi( argv[1] ); cout << "The command is: " << argv[0] << endl; // just curious } while ( numPlays-- > 0 ) { playAGame(); } } int playAGame() { enum Status { CONTINUE, WON, LOST }; int sum, myPoint; Status gameStatus; unsigned int t = time( 0 ); cout << "current time in sec: " << t << endl; srand( t ); sum = rollDice(); // first roll of the dice switch ( sum ) { case 7: case 11: // win on first roll gameStatus = WON; break; case 2: case 3: case 12: // lose on first roll gameStatus = LOST; break; default: // remember point gameStatus = CONTINUE; myPoint = sum; cout << "Point is " << myPoint << endl; break; // optional } while ( gameStatus == CONTINUE ) { // keep rolling sum = rollDice(); if ( sum == myPoint ) // win by making point gameStatus = WON; else if ( sum == 7 ) // lose by rolling 7 gameStatus = LOST; else ; //just continue } if ( gameStatus == WON ) cout << "Player wins" << endl; else cout << "Player loses" << endl; return 0; } int rollDice( void ) { int die1, die2, workSum; die1 = 1 + rand() % 6; die2 = 1 + rand() % 6; workSum = die1 + die2; cout << "Player rolled " << die1 << " + " << die2 << " = " << workSum << endl; return workSum; }