Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DESCRIPTION //
- /*
- THIS IS THE DESCRIPTION.
- */
- // LIBRARIES //
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using std::cout;
- using std::cin;
- using std::endl;
- // CONSTANTS //
- const int numTargetDice = 3;
- // PROTOTYPES //
- // Prints a wecome statement about the program.
- void welcome();
- // Returns a valid user input for the amount of rounds to be played.
- int getNumRounds();
- // asks the user if they what to play again.
- bool playAgain();
- // does all the actions in one round and repeats for the amount of rounds.
- void playRounds(int rounds);
- // Generates a random number from to 18 by rolling 3 dice.
- int generateTarget();
- // Returns a valid user input for the amount of Dice to be rolled by the user.
- int getNumDice();
- // calculates the winner of the round. returns false if ther is a tie.
- bool calcWinner(int player, int AI, int target, int &playerPoints, int &AIpoints);
- // Rolls numDice amout of dice and returns the total;
- int getRoll(int numDice);
- // Rolls 1/4 dice of the target and returns the total;
- int getRollAI(int target);
- // Returns a value from 1 to 6.
- int rollDice();
- // MAIN_FUNCTION //
- int main()
- {
- welcome();
- // Sets the seed for rand to an everchanging value.
- srand(time(NULL));
- // Looping the program if the user wants want to play again.
- do playRounds(getNumRounds());
- while(playAgain());
- cout << endl;
- return 0;
- }
- // FUNCTIONS //
- // Prints a wecome statement about the program.
- void welcome()
- {
- cout << endl << "try to get as close as you can to the target with out surpassing it." << endl;
- }
- // Returns a valid user input for the amount of rounds to be played.
- int getNumRounds()
- {
- int input = 0;
- while(true)
- {
- cout << "How Many Rounds do you want to play(Must be an odd number): " << endl;
- if(cin >> input)
- {
- if((input % 2) == 0)
- {
- cout << input << " was not an odd intager, please try again." << endl;
- }
- else
- {
- return input;
- }
- }
- else
- {
- cout << input << " was not an intager, please try again." << endl;
- }
- }
- }
- // Asks if the user wants to play agian, returns true if they want to.
- bool playAgain()
- {
- char input = 0;
- while(true)
- {
- cout << "Do you want to play again - y/n: ";
- if(cin >> input)
- {
- if((input == 'y') || (input == 'Y'))
- {
- return true;
- }
- else if((input == 'n') || (input == 'N'))
- {
- return false;
- }
- else
- {
- cout << input << " was not y or n, please try again." << endl;
- }
- }
- else
- {
- cout << input << " was not a char, please try again." << endl;
- }
- }
- }
- // does all the actions in one round and repeats for the amount of rounds.
- void playRounds(int rounds)
- {
- int playerPoints = 0, AIpoints = 0;
- for (int i = 0; i < rounds; i++)
- {
- int target = generateTarget();
- int numDice = getNumDice();
- while(!calcWinner(getRoll(numDice), getRollAI(target), target, playerPoints, AIpoints))
- cout << "Tie! Re-rolling dice..." << endl;
- }
- printf("\nThe winner was %s because %s had %d point(s) and %s had %d point(s)\n",
- (playerPoints > AIpoints) ? "you" : "the AI",
- (playerPoints > AIpoints) ? "you" : "the AI",
- (playerPoints > AIpoints) ? playerPoints : AIpoints,
- (playerPoints > AIpoints) ? "the AI" : "you",
- (playerPoints > AIpoints) ? AIpoints : playerPoints);
- }
- // Generates a random number from to 18 by rolling 3 dice.
- int generateTarget()
- {
- int target = 0;
- for (int i = 0; i < numTargetDice; i++)
- {
- target += rollDice();
- }
- cout << endl << "the target for this round is " << target << endl;
- return target;
- }
- // Returns a valid user input for the amount of Dice to be rolled by the user.
- int getNumDice()
- {
- int input = 0;
- while(true)
- {
- cout << "How Many dice do you want to roll for this round(Must be a positive number): ";
- if(cin >> input)
- {
- if(input < 0)
- {
- cout << input << " was not a positive intager, please try again." << endl;
- }
- else
- {
- return input;
- }
- }
- else
- {
- cout << input << " was not an intager, please try again." << endl;
- }
- }
- }
- // returns false if there was a tie.
- bool calcWinner(int player, int AI, int target, int &playerPoints, int &AIpoints)
- {
- if((player <= target) && (AI <= target))
- {
- if(player == AI) return false;
- else if(player > AI)
- {
- cout << "You won this round!" << endl;
- playerPoints++;
- return true;
- }
- else if(AI > player)
- {
- cout << "The AI won this round!" << endl;
- AIpoints++;
- return true;
- }
- }
- else if((player > target) && (AI <= target))
- {
- AIpoints++;
- return true;
- }
- else if((player <= target) && (AI > target))
- {
- playerPoints++;
- return true;
- }
- else if((player > target) && (AI > target)) return false;
- // should never reach this line of code.
- return false;
- }
- // Rolls numDice amout of dice and returns the total;
- int getRoll(int numDice)
- {
- int roll = 0;
- for (int i = 0; i < numDice; i++)
- {
- roll += rollDice();
- }
- cout << "your combined roll was " << roll << endl;
- return roll;
- }
- // Rolls 1/4 dice of the target and returns the total;
- int getRollAI(int target)
- {
- int roll = 0;
- for (int i = 0; i < (target / 4); i++)
- {
- roll += rollDice();
- }
- cout << "The AI's combined roll was " << roll << endl;
- return roll;
- }
- // Returns a value from 1 to 6.
- int rollDice()
- {
- return ((rand() % 6) + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement