Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. //Amber Kommavongsa
  2. //test cases
  3. //input: 3 output: INVALID, MENU
  4. // single chip: (input was 1,2) Output: [2.0 1.5 2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0] WINNINGS: $0 MENU// the path never goes below zero
  5. // single chip: (input 1,7) output: [7.0 7.5 8.0 7.5 6.0 6.5 7.0 7.5 7.0 6.5 6.0 5.5 5.0] WINNINGS: $0 MENU //path never goes above eight
  6. //multiple chips: (input was 2, 100, 1) output: WINNINGS: $29600.00 Average: $296.00 MENU
  7. //input: 3 output: GOODBYE
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <ctime>
  11. #include <cstdlib>
  12. #include <limits>
  13. using namespace std;
  14.  
  15. //FUNCTIONS
  16.  
  17. int falling_through_slots(int slot, bool output)//simulates the chip falling through the twelve slots
  18. {
  19. double finalslot = slot;
  20. double const SHIFT = .5;
  21. int r = rand() % 2;
  22. for (int peg_counter = 0; peg_counter < 12; peg_counter++)//simulates where the chip falls for each chip
  23. {
  24. int r = rand() % 2;
  25. if (finalslot < 8 && r == 1 || finalslot == 0)//moves right
  26. {
  27. finalslot += SHIFT;
  28. }
  29. else if (finalslot > 0 && r == 0 || finalslot == 8)//moves left
  30. {
  31. finalslot -= SHIFT;
  32. }
  33. if (output == false)
  34. {
  35.  
  36. }
  37. if (output == true)
  38. {
  39. cout << fixed << setprecision(1) << " " << finalslot;
  40. }
  41.  
  42. }
  43. return finalslot;
  44. }
  45.  
  46. double prize_moneyTotal(int finalslot)//computes total prize money
  47. {
  48. const double PRIZE0 = 100.00;
  49. const double PRIZE1 = 500.00;
  50. const double PRIZE2 = 1000.00;
  51. const double PRIZE3 = 0;
  52. const double PRIZE4 = 10000.00;
  53. const double PRIZE5 = 0;
  54. const double PRIZE6 = 1000.00;
  55. const double PRIZE7 = 500.00;
  56. const double PRIZE8 = 100.00;
  57. int slotfinal = finalslot;
  58. double prize = 0;
  59. switch (slotfinal)
  60. {
  61. case 0: prize += PRIZE0; break;
  62. case 1: prize += PRIZE1; break;
  63. case 2: prize += PRIZE2; break;
  64. case 3: prize += PRIZE3; break;
  65. case 4: prize += PRIZE4; break;
  66. case 5: prize += PRIZE5; break;
  67. case 6: prize += PRIZE6; break;
  68. case 7: prize += PRIZE7; break;
  69. case 8: prize += PRIZE8; break;
  70. }
  71. return prize;
  72. }
  73.  
  74. //function that tests the validity of the user input, must reject the user input if string or invalid number *hardest part
  75. bool TEST_userinput(int min, int max, int userinput)
  76. {
  77. int result = userinput;
  78. if (min < userinput && userinput < max)
  79. return true;
  80. else if (userinput == min || userinput == max)
  81. return true;
  82. else if (userinput < min || userinput > max)
  83. {
  84. cout << "Invalid character, please input a valid number:\n";
  85. return false;
  86. }
  87. else if ('a' < userinput && userinput < 'z')
  88. {
  89. cout << "Invalid character, please input a valid number:\n";
  90. return false;
  91. }
  92. else if ('A' < userinput && userinput < 'Z')
  93. {
  94. cout << "Invalid character, please input a valid number:\n";
  95. return false;
  96. }
  97. else if ('A' == userinput || 'Z' == userinput || 'a' == userinput || 'z' == userinput)
  98. {
  99. cout << "Invalid character, please input a valid number:\n";
  100. return false;
  101. }
  102. }
  103.  
  104. int main()
  105. {
  106. const int QUIT = 4;
  107. const bool VALID = true;
  108. const bool INVALID = false;
  109. const bool SHOW_OUTPUT = true;
  110. const bool NO_OUTPUT = false;
  111. const int SLOT_MIN = 0;
  112. const int SLOT_MAX = 8;
  113. const int max_chips = std::numeric_limits<int>::max();
  114.  
  115. int userchoice = 0;
  116. double select_slot;
  117. double total_prize;
  118. int chips;
  119. bool valid_response = true;
  120. srand(time(0));
  121.  
  122. while (userchoice != QUIT)
  123. {
  124. cout << "MENU: Please select one of the following options:\n";
  125. cout << "1 - Drop a single chip into one slot\n";//first game that can be played
  126. cout << "2 - Drop Multiple chips into one slot\n";//2nd game that can be played
  127. cout << "3 - Drop Multiple chips into multiple slots\n";
  128. cout << "4 - Quit the Program\n";
  129. int userchoice;
  130. cin >> userchoice;
  131. valid_response = TEST_userinput(1, 4, userchoice);
  132.  
  133. if (userchoice == 1)
  134. {
  135. cout << "***SINGLE CHIP***\n";
  136. cout << "Pick a slot numbers 0 - 8 to drop the chip:";
  137. cin >> select_slot;
  138. valid_response = TEST_userinput(SLOT_MIN, SLOT_MAX, select_slot);//validates the user's input
  139. if (valid_response == true)//ONly continues on if user input is true;
  140. {
  141. cout << "\[ " << setprecision(1) << select_slot;
  142. select_slot = falling_through_slots(select_slot, SHOW_OUTPUT);//prints out the location of the chip
  143. cout << "\]\n";
  144. total_prize = prize_moneyTotal(select_slot);//computes total prize money
  145. cout << "Money Won: $" << fixed << setprecision(2) << total_prize << endl;
  146. }
  147. }
  148. if (userchoice == 2)
  149. {
  150. double prize;
  151. double total_prize = 0;
  152. cout << "***MULTIPLE CHIPS***\n";
  153. cout << "How many chips would you like to drop:";
  154. cin >> chips;
  155. valid_response = TEST_userinput(1, max_chips, chips);
  156. if (valid_response == VALID)
  157. {
  158. cout << "Select a slot 0 - 8 to drop the chips:";
  159. cin >> select_slot;
  160. valid_response = TEST_userinput(SLOT_MIN, SLOT_MAX, select_slot);
  161. if (valid_response == VALID)
  162. {
  163. for (int i = 0; i < chips; i++)//drops multiple chips
  164. {
  165. double final_slot = select_slot;//resets the selected slot each time to the number the user input
  166. final_slot = falling_through_slots(select_slot, NO_OUTPUT);
  167. prize = prize_moneyTotal(final_slot);
  168. total_prize += prize;
  169. }
  170. double prize_average = total_prize / chips;//computes average
  171. cout << "Total won: $" << fixed << setprecision(2) << total_prize << endl;//outputs the total money earned
  172. cout << "Average won per chip: $" << fixed << setprecision(2) << prize_average << endl;
  173. }
  174. }
  175. }
  176. if (userchoice == 3)
  177. {
  178. cout << "**MULTIPLE CHIPS IN MULTIPLE SLOTS**\n";
  179. cout << "Indicate how many chips would you like to drop into each slot:";
  180. cin >> chips;
  181. valid_response = TEST_userinput(1, max_chips, chips);
  182. if (valid_response == VALID)
  183. {
  184. for (int slot_number = SLOT_MIN; slot_number <= SLOT_MAX; slot_number++)//moves the slot number up one for each time it goes through
  185. {
  186. double total_prize = 0;
  187. int final_slot = slot_number;
  188. for (int i = 0; i < chips; i++)
  189. {
  190. final_slot = falling_through_slots(slot_number, NO_OUTPUT);
  191. double prize = prize_moneyTotal(final_slot);
  192. total_prize += prize;
  193. }
  194. double prize_average = total_prize / chips;
  195. cout << "For SLOT " << slot_number << " you won: $" << fixed << setprecision(2) << total_prize << "\nand per chip: $" << fixed << setprecision(2) << prize_average << endl;
  196. cout << "\n";
  197. }
  198. }
  199. }
  200. while (userchoice == 4)
  201. {
  202. cout << "GOODBYE" << endl;
  203. system("pause");
  204. return 0;
  205. }
  206. }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement