⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crapsgame.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Tutorial 12: CrapsGame.cpp
// This application plays a simple craps game.
#include <iostream> // required to perform C++ stream I/O
#include <string>   // required to access string functions
#include <ctime>    // contains prototype for function time

// contains function prototypes for srand and rand
#include <cstdlib>

using namespace std; // for accessing C++ Standard Library members
   
int rollDice( void ); // function prototype

// function main begins program execution
int main()
{
   // constants representing winning dice rolls
   const int LUCKY_SEVEN = 7;
   const int YO_LEVEN = 11;

   // constants representing losing dice rolls
   const int SNAKE_EYES = 2;
   const int TREY = 3;
   const int BOX_CARS = 12;
   const int CRAPS = 7; // loses only after first roll

   // enumeration constants represent game status
   enum Status { CONTINUE, WON, LOST };

   int sum;      // stores sum of the dice
   int myPoint;  // stores the player's point
   string input; // stores input after each dice roll

   Status gameStatus; // can contain CONTINUE, WON or LOST

   // randomize random number generator using current time
   srand( time( 0 ) ); 

   sum = rollDice(); // first roll of the dice

   // determine game status and point based on sum of dice
   switch ( sum ) 
   {
      // win on first roll
      case LUCKY_SEVEN: 
	   case YO_LEVEN:            
         gameStatus = WON;
         break;

      // lose on first roll
      case SNAKE_EYES: 
	   case TREY: 
	   case BOX_CARS:             
         gameStatus = LOST;
         break;

      // remember point
      default:            
         gameStatus = CONTINUE;
         myPoint = sum;
         cout << "Point is " << myPoint << endl;
         break; // optional   

   } // end switch 

   // repeat while the game is not complete
   while ( gameStatus == CONTINUE )  
   {
      cout << "\nPress Enter to continue...";
      getline( cin, input ); // pauses until user presses Enter

      sum = rollDice(); // roll dice again

      // determine game status
      if ( sum == myPoint ) // win by matching point
      {
         gameStatus = WON;
      } // end if
      else if ( sum == CRAPS ) // lose by rolling 7
      {
         gameStatus = LOST;
      } // end else if

   } // end while 

   // display won or lost message
   if ( gameStatus == WON )
   {
      cout << "Player wins" << endl << endl;
   } // end if
   else
   {
      cout << "Player loses" << endl << endl;
   } // end else

   return 0; // indicates successful termination

} // end function main

// roll dice, calculate sum and display results
int rollDice( void )
{
   int die1;    // stores the value of the first die
   int die2;    // stores the value of the second die
   int diceSum; // stores the sum of the dice values

   die1 = 1 + rand() % 6; // pick random die1 value
   die2 = 1 + rand() % 6; // pick random die2 value
   diceSum = die1 + die2; // sum die1 and die2

   // display the results of this roll
   cout << "\nPlayer rolled " << die1 << " + " << die2
        << " = " << diceSum << endl;

   return diceSum; // return the sum of the dice

} // end function rollDice


/**************************************************************************
 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -