Advertisement
Guest User

project

a guest
Nov 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1. // DESCRIPTION //
  2.  
  3. /*
  4.     THIS IS THE DESCRIPTION.
  5. */
  6.  
  7. // LIBRARIES //
  8.  
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include <ctime>
  12.  
  13. using std::cout;
  14. using std::cin;
  15. using std::endl;
  16.  
  17. // CONSTANTS //
  18.  
  19. const int numTargetDice = 3;
  20.  
  21. // PROTOTYPES //
  22.  
  23. // Prints a wecome statement about the program.
  24. void welcome();
  25.  
  26. // Returns a valid user input for the amount of rounds to be played.
  27. int getNumRounds();
  28.  
  29. // asks the user if they what to play again.
  30. bool playAgain();
  31.  
  32. // does all the actions in one round and repeats for the amount of rounds.
  33. void playRounds(int rounds);
  34.  
  35. // Generates a random number from  to 18 by rolling 3 dice.
  36. int generateTarget();
  37.  
  38. // Returns a valid user input for the amount of Dice to be rolled by the user.
  39. int getNumDice();
  40.  
  41. // calculates the winner of the round. returns false if ther is a tie.
  42. bool calcWinner(int player, int AI, int target, int &playerPoints, int &AIpoints);
  43.  
  44. // Rolls numDice amout of dice and returns the total;
  45. int getRoll(int numDice);
  46.  
  47. // Rolls 1/4 dice of the target and returns the total;
  48. int getRollAI(int target);
  49.  
  50. // Returns a value from 1 to 6.
  51. int rollDice();
  52.  
  53. // MAIN_FUNCTION //
  54.  
  55. int main()
  56. {
  57.     welcome();
  58.  
  59.     // Sets the seed for rand to an everchanging value.
  60.     srand(time(NULL));
  61.  
  62.     // Looping the program if the user wants want to play again.
  63.     do playRounds(getNumRounds());
  64.     while(playAgain());
  65.  
  66.     cout << endl;
  67.     return 0;
  68. }
  69.  
  70. // FUNCTIONS //
  71.  
  72. // Prints a wecome statement about the program.
  73. void welcome()
  74. {
  75.     cout << endl << "try to get as close as you can to the target with out surpassing it." << endl;
  76. }
  77.  
  78. // Returns a valid user input for the amount of rounds to be played.
  79. int getNumRounds()
  80. {
  81.     int input = 0;
  82.     while(true)
  83.     {
  84.         cout << "How Many Rounds do you want to play(Must be an odd number): " << endl;
  85.         if(cin >> input)
  86.         {
  87.             if((input % 2) == 0)
  88.             {
  89.                 cout << input << " was not an odd intager, please try again." << endl;
  90.             }
  91.             else
  92.             {
  93.                 return input;
  94.             }
  95.         }
  96.         else
  97.         {
  98.             cout << input << " was not an intager, please try again." << endl;
  99.         }
  100.     }
  101. }
  102.  
  103. // Asks if the user wants to play agian, returns true if they want to.
  104. bool playAgain()
  105. {
  106.     char input = 0;
  107.     while(true)
  108.     {
  109.         cout << "Do you want to play again - y/n: ";
  110.         if(cin >> input)
  111.         {
  112.             if((input == 'y') || (input == 'Y'))
  113.             {
  114.                 return true;
  115.             }
  116.             else if((input == 'n') || (input == 'N'))
  117.             {
  118.                 return false;
  119.             }
  120.             else
  121.             {
  122.                 cout << input << " was not y or n, please try again." << endl;
  123.             }
  124.         }
  125.         else
  126.         {
  127.             cout << input << " was not a char, please try again." << endl;
  128.         }
  129.     }
  130. }
  131.  
  132. // does all the actions in one round and repeats for the amount of rounds.
  133. void playRounds(int rounds)
  134. {
  135.     int playerPoints = 0, AIpoints = 0;
  136.  
  137.     for (int i = 0; i < rounds; i++)
  138.     {
  139.         int target = generateTarget();
  140.         int numDice = getNumDice();
  141.  
  142.         while(!calcWinner(getRoll(numDice), getRollAI(target), target, playerPoints, AIpoints))
  143.             cout << "Tie! Re-rolling dice..." << endl;
  144.     }
  145.  
  146.     printf("\nThe winner was %s because %s had %d point(s) and %s had %d point(s)\n",
  147.           (playerPoints > AIpoints) ? "you" : "the AI",
  148.           (playerPoints > AIpoints) ? "you" : "the AI",
  149.           (playerPoints > AIpoints) ? playerPoints : AIpoints,
  150.           (playerPoints > AIpoints) ? "the AI" : "you",
  151.           (playerPoints > AIpoints) ? AIpoints : playerPoints);
  152. }
  153.  
  154. // Generates a random number from  to 18 by rolling 3 dice.
  155. int generateTarget()
  156. {
  157.     int target = 0;
  158.     for (int i = 0; i < numTargetDice; i++)
  159.     {
  160.         target += rollDice();
  161.     }
  162.  
  163.     cout << endl << "the target for this round is " << target << endl;
  164.  
  165.     return target;
  166. }
  167.  
  168. // Returns a valid user input for the amount of Dice to be rolled by the user.
  169. int getNumDice()
  170. {
  171.     int input = 0;
  172.     while(true)
  173.     {
  174.         cout << "How Many dice do you want to roll for this round(Must be a positive number): ";
  175.         if(cin >> input)
  176.         {
  177.             if(input < 0)
  178.             {
  179.                 cout << input << " was not a positive intager, please try again." << endl;
  180.             }
  181.             else
  182.             {
  183.                 return input;
  184.             }
  185.         }
  186.         else
  187.         {
  188.             cout << input << " was not an intager, please try again." << endl;
  189.         }
  190.     }      
  191. }
  192.  
  193. // returns false if there was a tie.
  194. bool calcWinner(int player, int AI, int target, int &playerPoints, int &AIpoints)
  195. {
  196.     if((player <= target) && (AI <= target))
  197.     {
  198.         if(player == AI) return false;
  199.  
  200.         else if(player > AI)
  201.         {
  202.             cout << "You won this round!" << endl;
  203.             playerPoints++;
  204.             return true;
  205.         }
  206.         else if(AI > player)
  207.         {    
  208.             cout << "The AI won this round!" << endl;
  209.             AIpoints++;
  210.             return true;  
  211.         }
  212.     }
  213.     else if((player > target) && (AI <= target))
  214.     {
  215.         AIpoints++;
  216.         return true;
  217.     }
  218.     else if((player <= target) && (AI > target))
  219.     {
  220.         playerPoints++;
  221.         return true;
  222.     }
  223.     else if((player > target) && (AI > target)) return false;
  224.  
  225.     // should never reach this line of code.
  226.     return false;
  227. }
  228.  
  229. // Rolls numDice amout of dice and returns the total;
  230. int getRoll(int numDice)
  231. {
  232.     int roll = 0;
  233.     for (int i = 0; i < numDice; i++)
  234.     {
  235.         roll += rollDice();
  236.     }
  237.     cout << "your combined roll was " << roll << endl;
  238.     return roll;
  239. }
  240.  
  241. // Rolls 1/4 dice of the target and returns the total;
  242. int getRollAI(int target)
  243. {
  244.     int roll = 0;
  245.  
  246.     for (int i = 0; i < (target / 4); i++)
  247.     {
  248.         roll += rollDice();
  249.     }
  250.     cout << "The AI's combined roll was " << roll << endl;
  251.     return roll;
  252. }
  253.  
  254. // Returns a value from 1 to 6.
  255. int rollDice()
  256. {
  257.     return ((rand() % 6) + 1);
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement