// Problem Statement: This program will play the game of craps. The user starts with $100 and // bets money, and the computer randomly selects numbers for the dice to see if they win or lose // Author: Brian Brindle #include #include #include #include const int STARTBAL = 100; void intro(void); // called by main function, explains the game to the user double getbet(double); // called by main function, takes balance - returns the bet amount int play(void); // called by main function, returns win or lose game flag int toss(void); // called by play function, returns the value of the dice int myrand(int,int); // called by toss function, returns random number rolled by dice void diepic(int); // called by toss function, prints picture of dice void gamereport(int,double,double&); // called by main function, displays results of each game void endstats(int,int,double); // called by main function, displays results of all games //ofstream fout("C:/Comp141/craps.txt"); void main() { double balance=0, nextbet=1; int gameflag=0, numplays=0, numwins=0; srand(time(NULL)); // the random number generator function balance = STARTBAL; intro(); // this fuction will explain the rules to the user while ((balance != 0) && (nextbet != 0)) { nextbet = getbet(balance); // calls the function to get the value of the next bet if (nextbet == 0) break; gameflag = play(); // tells if the game was won or lost gamereport(gameflag,nextbet,balance); // reports results to gamereport if (gameflag == 1) numwins++; numplays++; } endstats(numplays,numwins,balance); //displays final results of all games to the user } void intro(void) { cout << "Welcome to Brian's game of craps!\n\n" << "You will start with a balance of $100.\n" << "You may bet up to your balance in each game.\n\n" << "Two dice will be rolled, and if you get a 7 or 11 on the first roll you win!\n" << "If you get a 2, 3, or 12 on the first roll you lose!\n" << "If you get any other value for the first roll, it will be assigned as the point.\n" << "Then you will continue to 'roll the dice' until you get either\n" << "your point (and win) or a 7 (and lose).\n\n" << "Enter a 0 for your bet to end the game.\n\n"; /*fout << "Welcome to Brian's game of craps!\n\n" << "You will start with a balance of $100.\n" << "You may bet up to your balance in each game.\n\n" << "Two dice will be rolled, and if you get a 7 or 11 on the first roll you win!\n" << "If you get a 2, 3, or 12 on the first roll you lose!\n" << "If you get any other value for the first roll - it will be assigned as the point.\n" << "Then you will continue to 'roll the dice' until you get either\n" << "your point (and win) or a 7 (and lose).\n\n" << "Enter a 0 for your bet to end the game.\n\n\n";*/ return; } double getbet(double bal) { double amount = -1.; while ((amount < 0) || (amount > bal)) { cout << "\nPlease enter the amount of your bet: "; cin >> amount; cout << endl; //fout << "\nPlease enter the amount of your bet: "; //fout << amount; //fout << endl; if (amount == 0) { return 0.; break; } else if ((amount <= bal) && (amount > 0)) break; else { cout << "Invalid entry!\n"; //fout << "Invalid entry!\n"; } } return amount; // returns bet to main funtion } int play(void) { int flag=0, numdice=0, point=0; numdice = toss(); // calls toss function for dice value if ((numdice == 7) || (numdice == 11)) flag = 1; else if ((numdice == 2) || (numdice == 3) || (numdice == 12)) flag = -1; else { point = numdice; // the value rolled is assigned as the point numdice = 0; while ((numdice != 7) && (numdice != point)) { numdice = toss(); // calls toss funtion for another dice value if (numdice == 7) { flag = -1; break; } else if (numdice == point) { flag = 1; break; } } } return flag; // returns if game is won or lost } int toss(void) { int x=1, y=6, numrolled1=0, numrolled2=0, numrolled=0; numrolled1 = myrand(x,y); // assigns random numbers to variables numrolled2 = myrand(x,y); diepic(numrolled1); cout << endl; //fout << endl; diepic(numrolled2); cout << endl << endl << endl << endl; //fout << endl << endl << endl << endl; numrolled = numrolled1 + numrolled2; return numrolled; } int myrand(int a, int b) { int nvals, numbad, rnum, shiftedval; nvals = b - a + 1; // This is the number of possible values from a to b inclusive. numbad = (RAND_MAX + 1) % nvals; // remainder on division by nvals. /* This is the number of "bad values" from the highest exact multiple of nvals up to RAND_MAX. We do not want to allow a rand() value to be used if it equals one of these "bad values" */ rnum = rand(); // if rand() takes on one of the bad values, get another one! while (rnum > RAND_MAX - numbad) { rnum = rand(); } // rnum = rand() will now be between 0 and RAND_MAX - numbad shiftedval = (rnum % nvals) + a; /* Since the remainder = (rnum % nvals) runs from 0 to nvals - 1 = (b-a+1)-1 = b-a, we simply add on an "a" to shift it to be from 0+a=a to (b-a) + a = b. Viola! It goes from a to b inclusive like we wanted :-) */ return shiftedval; } void diepic(int k) { if (k == 1) { cout << "|-----|\n" << "| |\n" << "| 0 |\n" << "| |\n" << "|-----|\n"; /*fout << "|-----|\n" << "| |\n" << "| 0 |\n" << "| |\n" << "|-----|\n";*/ } else if (k == 2) { cout << "|-----|\n" << "| |\n" << "| 0 0 |\n" << "| |\n" << "|-----|\n"; /*fout << "|-----|\n" << "| |\n" << "| 0 0 |\n" << "| |\n" << "|-----|\n";*/ } else if (k == 3) { cout << "|-----|\n" << "| 0|\n" << "| 0 |\n" << "|0 |\n" << "|-----|\n"; /*fout << "|-----|\n" << "| 0|\n" << "| 0 |\n" << "|0 |\n" << "|-----|\n";*/ } else if (k == 4) { cout << "|-----|\n" << "| 0 0 |\n" << "| |\n" << "| 0 0 |\n" << "|-----|\n"; /*fout << "|-----|\n" << "| 0 0 |\n" << "| |\n" << "| 0 0 |\n" << "|-----|\n";*/ } else if (k == 5) { cout << "|-----|\n" << "|0 0|\n" << "| 0 |\n" << "|0 0|\n" << "|-----|\n"; /*fout << "|-----|\n" << "|0 0|\n" << "| 0 |\n" << "|0 0|\n" << "|-----|\n";*/ } else if (k == 6) { cout << "|-----|\n" << "|0 0 0|\n" << "| |\n" << "|0 0 0|\n" << "|-----|\n"; /*fout << "|-----|\n" << "|0 0 0|\n" << "| |\n" << "|0 0 0|\n" << "|-----|\n";*/ } return; } void gamereport(int flag, double bet, double& bal) { if (flag ==1) { cout << "You win!! You must be lucky! Play again.\n"; //fout << "You win!! You must be lucky! Play again.\n"; } else if (flag == -1) { cout << "You lost. You bum, you might as well give up.\n"; //fout << "You lost. You bum, you might as well give up.\n"; } cout << "\nYour balace before playing this game was : $" << bal << endl; //fout << "\nYour balace before playing this game was : $" << bal << endl; cout << "Your bet was : $" << bet << endl; //fout << "Your bet was : $" << bet << endl; bal = bal + flag * bet; // calculates new balance cout << "Your new balance is : $" << bal << endl; //fout << "Your new balance is : $" << bal << endl; return; } void endstats(int nplays, int nwins, double bal) { double difference=0, winpercentage=0, wins=0, plays=0; cout << endl << "Games played: " << nplays; //fout << endl << "Games played: " << nplays; cout << endl << "Games won: " << nwins; //fout << endl << "Games won: " << nwins; if (nplays > 0) { wins = nwins * 1.0; plays = nplays * 1.0; winpercentage = (wins / plays) * 100.0; cout << endl << "Winning percentage: " << winpercentage; //fout << endl << "Winning percentage: " << winpercentage; } cout << endl << "Your final balance is: $" << bal << endl << endl; //fout << endl << "Your final balance is: $" << bal << endl << endl; difference = STARTBAL - bal; if (difference == 0) { cout << "You broke even."; //fout << "You broke even."; } else if (difference < 0) { difference = difference * (-1); cout << "You won $" << difference; //fout << "You won $" << difference; } else { cout << "You lost $" << difference; //fout << "You lost $" << difference; } cout << endl << endl << endl; // This ending here, enables a pause before the program closes in DOS mode char trash; cin.get(trash); cout << "Press [Enter] key to exit: "; cin.get(trash); return; }