Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- a4q1.cpp
- Craps
- Created by Hayden Lueck on 2014-10-30.
- Copyright (c) 2014 Hayden Lueck. All rights reserved.
- Program Purpose: Simulate game of craps and test out function use
- main function: Declared variables, seeds RNG, contains main loop, passes variables
- to getRoll, calcSum, and printRoll. Prints out whether user has won or lost.
- getRoll function: takes in a number to set as the roll, and gives it a random number from 1 to 6
- calcSum function: takes in roll1, roll2, and sum to set the value of the current point in the
- main function
- printRoll function: Prints out the 2 numbers rolled, and the sum of said numbers.
- input: None from user, randomly generates numbers within the program
- output: The game of craps played, all numbers rolled, and whether the round was won or lost
- limitations: User does not input any numbers themselves
- */
- #include <iostream>
- #include <time.h>
- /****** Function Prototyping ******/
- void getRoll(int&);
- void calcSum(int, int, int&);
- void printRoll(int, int, int);
- /****** Main Function ******/
- int main()
- {
- int roll1, roll2, point = 0, lastPoint = 1; //declare integers, prime point and lastPoint
- bool endFlag = true, pointFlag = false; //declare boolean values for flags
- srand(int(time(0))); //seed RNG based off of time
- do{
- getRoll(roll1); //set random number between 1 and 6 to int roll1
- getRoll(roll2); //and then roll2
- calcSum(roll1, roll2, point); //calculate the sum of roll1 and roll2, set it to point
- printRoll(roll1, roll2, point); //print out the sum of roll1 and roll2
- if(point == lastPoint //if point is 2, 3, 12, or 11 the first round through
- || (point == 2 && pointFlag == false) //or 7 at any point, or the point is the same as the
- || (point == 3 && pointFlag == false) //last point
- || (point == 12 && pointFlag == false)
- || point == 7
- || (point == 11 && pointFlag == false)){
- endFlag = false; //set endFlag to end the loop
- }else{ //otherwise
- std::cout << "Point is " << point << std::endl; //tell user current point
- lastPoint = point; //set lastPoint to current point
- pointFlag = true; //set pointFlag to true to indicate
- //the point loop has begun
- }
- }while(endFlag); //go until endFlag is true
- if(point == lastPoint
- || point == 2 //if point is 2, 3, 12, or 11 with the pointFlag being true
- || point == 3
- || point == 12
- || (point == 11 && pointFlag == true)){
- std::cout << "You win." << std::endl; //print out that user won
- }else if (point == 7 || (point == 11 && pointFlag == false)){ //or point is 7 or 11 with
- //pointFlag being flase
- std::cout << "You lose." << std::endl; //print out that user loses
- }else{ //in case code is modified and breaks something
- std::cout << "Error" << std::endl; //print out error
- }
- return 0;
- }
- /****** getRoll function ******/
- void getRoll(int& roll) //takes in a roll by reference
- {
- roll = rand()%6+1; //get a random number between 1 and 6
- }
- /****** calcSum function ******/
- void calcSum(int roll1, int roll2, int& sum) //takes in roll1, roll2, and the sum (by reference)
- {
- sum = roll1 + roll2; // set sum (point in main function) to the sum of roll1 and roll2
- }
- /****** printRoll function ******/
- void printRoll(int roll1, int roll2, int sum) //takes in roll1, roll2, and the sum (all by value)
- {
- std::cout << "You rolled " << roll1 << " + "
- << roll2 << " = " << sum << std::endl; //print out equation for dice rolled
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement