//Execution Begins Here! int main(int argc, char** argv) { //declare main menu variables int choice; FancyDice diceIs; Score print; //main menu output cout << " =========================" << endl; cout << "|| PIG: THE DICE GAME ||" << endl; cout << "|| PROGRAM ||" << endl; cout << " =========================" << endl << endl << endl; cout << "1. Play Pig" << endl; cout << "2. How to Play" << endl; cout << "3. Dice Probability Checker" << endl; cout << "4. Are You Competant Enough to Run This Program? Found Out! " << endl; cout << "5. Quit Program" << endl << endl << endl; cout << "Enter choice number and press [enter]."; cin >> choice; //main menu w/ input validation switch (choice) { //Play the game case 1: { char playAgain; //for multiple games float moneyWon; //will be either positive/negative if win/lose int gamesPlayed = 0; //keeps track of total games played float betHistory[10]; //holds all moneyWon values across games const int ROWS = 50; //number of user turns (most likely will not exceed 50) char rollHistory[ROWS][COLS]; //array for displaying action ('R' or 'H') and dice value (1-6) int i = 0; //counter for betHistory float total; //will calculate sum of all moneyWon across games do { //declare variables float bet; //will be positive value if user wins, negative if lose int dice; //6-sided die short action; //variable for roll or hold bool userTurn = true; //user always gets first turn int compScore = 0; //score of computer int userScore = 0; //score of user const int GOAL = 5; //whoever surpasses this score wins bool win = false; //if true user won, game ends bool loss = false; //if true user lost, game ends srand(static_cast<unsigned int>(time(0))); //set rand() seed unsigned int sleep(unsigned int seconds); //sleep() function setup int j = 0; //counter for rollHistory //gamble cout << "Type how much money you want to bet: $"; cin >> bet; try { if(bet < 0){ throw 1; } } catch (int e) { cout << "Error number: " << e << endl; } //Game begins and turns proceed until game has ended while (win == false && loss == false) { //when userTurn == true, it is user's turn to roll or hold if (userTurn == true) { //must reset turn total int turnTotal = 0; //must reset roll number bool rolled = false; //sees if user rolled at least one time (only used for "hold" input validation) do { dice = rand() % 6 + 1; //[1,6] cout << endl; cout << "Your turn: Type \"1\" to roll and \"2\" to hold: "; cin >> action; try { if(action < 0){ throw 2; } } catch (int e) { cout << "Error number: " << e << endl; } print.scoreBoard(turnTotal, userScore); //if user picks "roll" if (action == 1) { diceIs.rolling();//rolling(); //if dice lands on 1, no value is banked and userTurn becomes false, meaning computer's turn starts if (dice == 1){ cout << string( 15, '\n' ); cout << " |User|" << endl << endl; cout << "Turn Total: 0 Score: " << userScore << endl; diceIs.face1();//diceArt1(); userTurn = false; cout << endl; } //if dice lands on value other than 1, that value is added to turn total else if (dice == 2) { print.clearScreen(turnTotal, dice, userScore); diceIs.face2();//diceArt2(); //turnTotal += dice; totale(dice, turnTotal); } else if (dice == 3) { print.clearScreen(turnTotal, dice, userScore); diceIs.face3();//diceArt3(); totale(dice, turnTotal); } else if (dice == 4) { print.clearScreen(turnTotal, dice, userScore); diceIs.face4();//diceArt4(); totale(dice, turnTotal); } else if (dice == 5) { print.clearScreen(turnTotal, dice, userScore); diceIs.face5();//diceArt5(); totale(dice, turnTotal); } else if (dice == 6) { print.clearScreen(turnTotal, dice, userScore); diceIs.face6();//diceArt6(); totale(dice, turnTotal); } checkWin(GOAL, turnTotal, userScore, win, bet); rolled = true; rollHistory[j][0] = 'R'; rollHistory[j][1] = dice + 48; j++; } //if user picks "2" else if (action == 2) { //player must have rolled once before "hold" banks the turn total if (rolled == true) { diceIs.holding();//holding(); //turn total is banked and added to the overall score of user userScore += turnTotal; turnTotal = 0; print.scoreBoard(turnTotal, userScore); userTurn = false; cout << "Banked your total. " << endl; } //if player has not rolled once before selecting "hold," the program automatically rolls and similar if else statements from above activate else if (rolled == false) { cout << "Cannot hold yet." << endl; } rollHistory[j][0] = 'H'; rollHistory[j][1] = '-'; j++; } else { cout << "Please type a valid action of \"1\" or \"2.\"" << endl; } //user must have picked valid action, it must be the users turn, and the game must still be active (userScore < GOAL) } while ((action == 1 || action == 2) && userTurn == true && win == false && loss == false); } //computer's turn (only input user does is pressing [enter]) else if (userTurn == false) { int turnTotal = 0; //turnTotal is reset for computer bool rolled = false; //reset roll number cout << "Hit [enter] once to progress through the computer's turn."; cin.ignore(2); do { dice = rand() % 6 + 1; //same dice and probability as user int compAction = rand() % 4 + 1; //computer will roll 3 out of 4 times print.compScoreBoard(turnTotal, compScore); //roll action should happen 3 out of 4 times if (compAction == 1 || compAction == 2 || compAction == 3) { diceIs.rolling();//rolling(); if (dice == 1){ cout << string( 15, '\n' ); cout << " |Computer|" << endl << endl; cout << "Turn Total: 0 Score: " << compScore << endl; diceIs.face1();//diceArt1(); userTurn = true; cout << endl; } else if (dice == 2) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face3();//diceArt2(); turnTotal += dice; cout << endl; } else if (dice == 3) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face3();//diceArt3(); turnTotal += dice; cout << endl; } else if (dice == 4) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face4();//diceArt4(); turnTotal += dice; cout << endl; } else if (dice == 5) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face5();//diceArt5(); turnTotal += dice; cout << endl; } else if (dice == 6) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face6();//diceArt6(); turnTotal += dice; cout << endl; } checkLoss(GOAL, turnTotal, compScore, loss, bet); rolled = true; } //hold action should happen 1 out of 4 times else if (compAction == 4) { if (rolled == true) { diceIs.holding();//holding(); compScore += turnTotal; turnTotal = 0; print.compScoreBoard(turnTotal, compScore); cout << "The computer has banked its turn total." << endl; userTurn = true; } else if (rolled == false) { diceIs.rolling();//rolling(); //roll will happen automatically if (dice == 1){ cout << string( 15, '\n' ); cout << " |Computer|" << endl << endl; cout << "Turn Total: 0 Score: " << compScore << endl; diceIs.face1();//diceArt1(); userTurn = true; cout << endl; } else if (dice == 2) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face2();//diceArt2(); turnTotal += dice; cout << endl; } else if (dice == 3) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face3();//diceArt3(); turnTotal += dice; cout << endl; } else if (dice == 4) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face4();//diceArt4(); turnTotal += dice; cout << endl; } else if (dice == 5) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face5();//diceArt5(); turnTotal += dice; cout << endl; } else if (dice == 6) { print.compClearScreen(turnTotal, dice, compScore); diceIs.face6();//diceArt6(); turnTotal += dice; cout << endl; } checkLoss(GOAL, turnTotal, compScore, loss, bet); rolled = true; } else { cout << "If you are seeing this, then something horribly wrong has happened."; } } else { cout << "If you are seeing this, then something horribly wrong has happened." << endl; } //computer delay between turns for (int x = 0; x < 2; x++) { time_t t1, t2; t1 = time(0); do { t2 = time(0); } while (difftime (t2, t1) < 1); } } while (userTurn == false && win == false && loss == false); } } gamesPlayed++; cout << endl << "Rolls Stats:" << endl; cout << endl; sortArray(rollHistory, j); moneyWon = earnings(win, bet); betHistory[i] = moneyWon; i++; cout << "Play again?"; cin >> playAgain; try { if(playAgain != 'y' || playAgain != 'n'){ throw 3; } } catch (int e) { cout << "Error number: " << e << endl; } } while (toupper(playAgain) == 'Y'); //writing all the money won and lost and total across games cout << "Gambling receipt has been printed to a file."; ofstream outputFile; outputFile.open("receipt.txt"); for (int j = 0; j < gamesPlayed; j++) { outputFile << "Game: " << j + 1 << " Money won: $ " << setw(8) << betHistory[j] << endl; total += betHistory[j]; } outputFile << " ------------" << endl; outputFile << " " << setw(9) << total; outputFile.close(); }