Advertisement
Ashanmaril

Craps

Nov 3rd, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. /*
  2.  a4q1.cpp
  3.  Craps
  4.  
  5.  Created by Hayden Lueck on 2014-10-30.
  6.  Copyright (c) 2014 Hayden Lueck. All rights reserved.
  7.  
  8.  Program Purpose: Simulate game of craps and test out function use
  9.  
  10.  main function: Declared variables, seeds RNG, contains main loop, passes variables
  11.                 to getRoll, calcSum, and printRoll. Prints out whether user has won or lost.
  12.  
  13.  getRoll function: takes in a number to set as the roll, and gives it a random number from 1 to 6
  14.  calcSum function: takes in roll1, roll2, and sum to set the value of the current point in the
  15.                     main function
  16.  printRoll function: Prints out the 2 numbers rolled, and the sum of said numbers.
  17.  
  18.  input: None from user, randomly generates numbers within the program
  19.  output: The game of craps played, all numbers rolled, and whether the round was won or lost
  20.  
  21.  limitations: User does not input any numbers themselves
  22.  
  23.  */
  24.  
  25. #include <iostream>
  26. #include <time.h>
  27.  
  28. /****** Function Prototyping ******/
  29. void getRoll(int&);
  30. void calcSum(int, int, int&);
  31. void printRoll(int, int, int);
  32.  
  33. /****** Main Function ******/
  34. int main()
  35. {
  36.     int roll1, roll2, point = 0, lastPoint = 1; //declare integers, prime point and lastPoint
  37.     bool endFlag = true, pointFlag = false; //declare boolean values for flags
  38.     srand(int(time(0))); //seed RNG based off of time
  39.    
  40.     do{
  41.         getRoll(roll1); //set random number between 1 and 6 to int roll1
  42.         getRoll(roll2); //and then roll2
  43.        
  44.         calcSum(roll1, roll2, point); //calculate the sum of roll1 and roll2, set it to point
  45.         printRoll(roll1, roll2, point); //print out the sum of roll1 and roll2
  46.        
  47.         if(point == lastPoint                       //if point is 2, 3, 12, or 11 the first round through
  48.            || (point == 2 && pointFlag == false)    //or 7 at any point, or the point is the same as the
  49.            || (point == 3 && pointFlag == false)    //last point
  50.            || (point == 12 && pointFlag == false)
  51.            || point == 7
  52.            || (point == 11 && pointFlag == false)){
  53.            
  54.             endFlag = false; //set endFlag to end the loop
  55.            
  56.         }else{ //otherwise
  57.             std::cout << "Point is " << point << std::endl; //tell user current point
  58.             lastPoint = point;                              //set lastPoint to current point
  59.             pointFlag = true;                               //set pointFlag to true to indicate
  60.                                                             //the point loop has begun
  61.         }
  62.        
  63.     }while(endFlag); //go until endFlag is true
  64.    
  65.     if(point == lastPoint
  66.        || point == 2        //if point is 2, 3, 12, or 11 with the pointFlag being true
  67.        || point == 3
  68.        || point == 12
  69.        || (point == 11 && pointFlag == true)){
  70.         std::cout << "You win." << std::endl; //print out that user won
  71.     }else if (point == 7 || (point == 11 && pointFlag == false)){ //or point is 7 or 11 with
  72.                                                                   //pointFlag being flase
  73.         std::cout << "You lose." << std::endl; //print out that user loses
  74.     }else{ //in case code is modified and breaks something
  75.         std::cout << "Error" << std::endl; //print out error
  76.     }
  77.    
  78.     return 0;
  79. }
  80.  
  81. /****** getRoll function ******/
  82. void getRoll(int& roll) //takes in a roll by reference
  83. {
  84.     roll = rand()%6+1; //get a random number between 1 and 6
  85. }
  86.  
  87. /****** calcSum function ******/
  88. void calcSum(int roll1, int roll2, int& sum) //takes in roll1, roll2, and the sum (by reference)
  89. {
  90.     sum = roll1 + roll2; // set sum (point in main function) to the sum of roll1 and roll2
  91. }
  92.  
  93. /****** printRoll function ******/
  94. void printRoll(int roll1, int roll2, int sum) //takes in roll1, roll2, and the sum (all by value)
  95. {
  96.     std::cout << "You rolled " << roll1 << " + "
  97.     << roll2 << " = " << sum << std::endl; //print out equation for dice rolled
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement