Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program: Assignment 3 OH, Frack!
- // Programmer: Peter Chen
- // Purpose: a dice game called "Frack"
- #include "pch.h"
- #include <iostream>
- #include <ctime>
- #include <iomanip>
- #include <string>
- #include <cstdlib>
- using namespace std;
- //--------------------------------------------------------------------------------- Program introduction
- int gameIntro() { // Get the players' names
- int choice;
- cout << "Welcome to the game OH FRACK!!!\n";
- cout << "Please choose what you would like to do?\n";
- cout << "1. Play\n";
- cout << "2. Rules\n";
- cout << "3. Exit\n";
- cout << "Please choose:";
- cin >> choice;
- while (choice != 1 && choice != 2 && choice != 3) {
- cout << "Please re-enter a valid choice:";
- cin >> choice;
- }
- return choice;
- }
- //--------------------------------------------------------------------------------- Game rules
- void help() {
- cout << "\n";
- cout << "1. first player rolls 5 six-sided dice\n";
- cout << "2. if the average of the dice is less than or equal to 3\n";
- cout << "the player gets 0 points from this turn and next player plays.";
- cout << "3. if it is greater than 3, then the player is safe and the total of the dice is their score\n";
- cout << "4. then they can choose either to \n";
- cout << "\n";
- cout << "a. roll again, but with one less die, their score increase if they dont frack out\n";
- cout << "meaning they dont get an average of less than 3\n";
- cout << "the player can continue do this all the way to rolling just one die\n";
- cout << "after they die, it is passed to the next player.\n";
- cout << "b. they can declare their turn over, add their total score to final score\n";
- cout << "the next player does the same\n";
- cout << "Note that the total game score is not affected by fracking out(dying once)\n";
- cout << "only the turn score can be reset to zero!\n";
- cout << "\n";
- return;
- }
- //--------------------------------------------------------------------------------- Game calculation
- int getPoints() {
- int points;
- cout << "How many points are we playing to?";
- cin >> points;
- return points;
- }
- int playerCalc(double roll1, int roll2, int roll3, int roll4, int roll5) {
- double averageR;
- averageR = (roll1 + roll2 + roll3 + roll3 + roll4 + roll5) / 100;
- return averageR;
- }
- //--------------------------------------------------------------------------------- Main Program
- int main()
- {
- string player1; // Player 1's name
- string player2; // Player 2's name
- string p1Roll; // Player 1's roll choice
- string p2Roll; // Player 2's roll choice
- string p2Choice; // Player 2's Choice between ZERO start or player1's dice
- //-------------------------------------------------------- The 5 dices players roll
- int dice1;
- int dice2;
- int dice3;
- int dice4;
- int dice5;
- //-------------------------------------------------------- Players' choice and points they are playing to
- int choice;
- int points;
- //--------------------------------------------------------- Dice count for the 2 players
- int diceP1 = 5;
- int diceP2 = 5;
- //--------------------------------------------------------- Player totals and averages
- double p1Total;
- double p1AverageR;
- double p2Total;
- double p2AverageR;
- //--------------------------------------------------------- Player's end score
- int score2 = 0;
- int score = 0;
- //--------------------------------------------------------- Random number generator for the dices
- srand((unsigned)time(0));
- dice1 = (rand() % 5) + 1;
- dice2 = (rand() % 5) + 1;
- dice3 = (rand() % 5) + 1;
- dice4 = (rand() % 5) + 1;
- dice5 = (rand() % 5) + 1;
- choice = gameIntro();
- srand((unsigned)time (0));
- if (choice == 1) {
- cout << "Player 1 please enter your name:";
- cin >> player1;
- cout << "Player 2 please enter your name:";
- cin >> player2;
- cout << "\n";
- points = getPoints();
- //---------------------------------------------------------- Game starts here
- while (points > score || points > score2) {
- cout << player1 << "'s turn 1\n";
- cout << "Your dices are: " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << " \n";
- cout << "\n";
- p1Total = dice1 + dice2 + dice3 + dice4 + dice5;
- p1AverageR = p1Total / diceP1;
- cout << "The total is " << p1Total << ", and the average is " << p1AverageR << "\n";
- cout << "\n";
- score = p1Total;
- //----------------------------------------------------------- Loop for the players
- if (p1AverageR >= 2.6) {
- while (p1AverageR >= 2.6 && diceP1 > 0) {
- srand((unsigned)time(0));
- dice1 = (rand() % 5) + 1;
- dice2 = (rand() % 5) + 1;
- dice3 = (rand() % 5) + 1;
- dice4 = (rand() % 5) + 1;
- dice5 = (rand() % 5) + 1;
- cout << player1 << " is safe! Your turn score is now " << p1Total << "\n";
- cout << "Enter R to roll again, or P to pass:";
- cin >> p1Roll;
- if (p1Roll == "R") {
- diceP1--;
- if (diceP1 == 4) {
- dice5 = 0;
- }
- else if (diceP1 == 3) {
- dice5 = 0;
- dice4 = 0;
- }
- else if (diceP1 == 2) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- }
- else if (diceP1 == 1) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- dice2 = 0;
- }
- else if (diceP1 == 0) {
- break;
- }
- cout << "Your dices are : " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << "\n";
- p1Total = dice1 + dice2 + dice3 + dice4 + dice5;
- p1AverageR = p1Total / diceP1;
- cout << "\nThe total is " << p1Total << ", and the average is " << p1AverageR << "\n";
- if (p1AverageR >= 2.6) {
- score += p1Total;
- }
- else {
- p1Total = 0;
- score += p1Total;
- }
- cout << score << "\n";
- }// End of first if in while
- else if (p1Roll == "P") {
- diceP1 = 0;
- break;
- }// End of else if
- }// End while the user is higher than 2.6
- }// End of if loop
- else if (p1AverageR < 2.6) {
- score = 0;
- cout << "You got a score of 0\n";
- }
- cout << player1 << "'s total score for the first round is " << score << ".\n";
- //----------------------------------------------------------------------------PLAYER 2 SECTION
- cout << "What would " << player2 << " like to do?\n";
- cout << "Do you want to start from zero(Z), or with " << player1 << "'s points(P) with " << diceP1 << "?";
- cin >> p2Choice;
- cout << "\n";
- if (p2Choice == "Z") {
- diceP2 = 5;
- dice1 = (rand() % 5) + 1;
- dice2 = (rand() % 5) + 1;
- dice3 = (rand() % 5) + 1;
- dice4 = (rand() % 5) + 1;
- dice5 = (rand() % 5) + 1;
- cout << "Your dices are: " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << " \n";
- p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
- p2AverageR = p2Total / diceP2;
- cout << "The total is " << p2Total << ", and the average is " << p2AverageR << "\n";
- score2 = p2Total;
- if (p2AverageR >= 2.6) {
- while (p2AverageR >= 2.6 && diceP2 > 0) {
- srand((unsigned)time(0));
- dice1 = (rand() % 5) + 1;
- dice2 = (rand() % 5) + 1;
- dice3 = (rand() % 5) + 1;
- dice4 = (rand() % 5) + 1;
- dice5 = (rand() % 5) + 1;
- cout << player2 << " is safe! Your turn score is now " << p2Total << "\n";
- cout << "Enter R to roll again, or P to pass:";
- cin >> p2Roll;
- if (p2Roll == "R") {
- diceP2--;
- if (diceP2 == 4) {
- dice5 = 0;
- }
- else if (diceP2 == 3) {
- dice5 = 0;
- dice4 = 0;
- }
- else if (diceP2 == 2) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- }
- else if (diceP2 == 1) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- dice2 = 0;
- }
- else if (diceP2 == 0) {
- break;
- }
- cout << "Your dices are : " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << "\n";
- p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
- p2AverageR = p2Total / diceP2;
- cout << "\nThe total is " << p2Total << ", and the average is " << p2AverageR << "\n";
- if (p2AverageR >= 2.6) {
- score2 += p2Total;
- }
- else {
- p2Total = 0;
- score2 += p2Total;
- }
- cout << score << "\n";
- }// End of first if in while
- else if (p2Roll == "P") {
- diceP1 = 0;
- break;
- }// End of else if
- }// End while the user is higher than 2.6
- }// End of if loop
- if (p2AverageR < 2.6) {
- score2 = 0;
- cout << "You got a score of 0\n";
- }
- cout << player2 << "'s total score for the first round is " << score2 << ".\n";
- }
- //---------------------------------------------------------------------------- Player 2 chooses PASS
- else if (p2Choice == "P") {
- diceP2 = diceP1;
- if (diceP2 == 4) {
- dice5 = 0;
- }
- else if (diceP2 == 3) {
- dice5 = 0;
- dice4 = 0;
- }
- else if (diceP2 == 2) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- }
- else if (diceP2 == 1) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- dice2 = 0;
- }
- cout << "Your dices are: " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << " \n";
- p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
- p2AverageR = p2Total / diceP2;
- cout << "The total is " << p2Total << ", and the average is " << p2AverageR << "\n";
- score2 = p2Total;
- if (p2AverageR >= 2.6) {
- while (p2AverageR >= 2.6 && diceP2 > 0) {
- srand((unsigned)time(0));
- dice1 = (rand() % 5) + 1;
- dice2 = (rand() % 5) + 1;
- dice3 = (rand() % 5) + 1;
- dice4 = (rand() % 5) + 1;
- dice5 = (rand() % 5) + 1;
- cout << player2 << " is safe! Your turn score is now " << p2Total << "\n";
- cout << "Enter R to roll again, or P to pass:";
- cin >> p2Roll;
- if (p2Roll == "R") {
- diceP2--;
- if (diceP2 == 4) {
- dice5 = 0;
- }
- else if (diceP2 == 3) {
- dice5 = 0;
- dice4 = 0;
- }
- else if (diceP2 == 2) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- }
- else if (diceP2 == 1) {
- dice5 = 0;
- dice4 = 0;
- dice3 = 0;
- dice2 = 0;
- }
- else if (diceP2 == 0) {
- break;
- }
- cout << "Your dices are : " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << "\n";
- p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
- p2AverageR = p2Total / diceP2;
- cout << "\nThe total is " << p2Total << ", and the average is " << p2AverageR << "\n";
- if (p2AverageR >= 2.6) {
- score2 += p2Total;
- }
- else {
- p2Total = 0;
- score2 += p2Total;
- }
- cout << score << "\n";
- }// End of first if in while
- else if (p2Roll == "P") {
- diceP2 = 0;
- break;
- }// End of else if
- }// End while the user is higher than 2.6
- }// End of if loop
- if (p2AverageR < 2.6) {
- score2 = 0;
- cout << "You got a score of 0\n";
- }
- cout << player2 << "'s total score for the first round is " << score2 << ".\n";
- }
- } // End of while loop for points
- } // First if
- else if (choice == 2){ // Rules of the game
- help();
- }
- else if (choice == 3) { // Exit the game
- exit(0);
- }
- cout << "\nBy: Peter Chen Section: S11\n";
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement