Advertisement
MnMWizard

Deal or no deal final day

Dec 14th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. //Mason Marnell
  2. // Cases class here, main class down below
  3.  
  4.  
  5. public class Cases {
  6. private int[] cases;
  7.  
  8. public Cases(){
  9. cases = new int[27];
  10. cases[0] = 0;
  11. cases[1] = 1;
  12. cases[2] = 2;
  13. cases[3] = 5;
  14. cases[4] = 10;
  15. cases[5] = 25;
  16. cases[6] = 50;
  17. cases[7] = 75;
  18. cases[8] = 100;
  19. cases[9] = 200;
  20. cases[10] = 300;
  21. cases[11] = 400;
  22. cases[12] = 500;
  23. cases[13] = 750;
  24. cases[14] = 1000;
  25. cases[15] = 5000;
  26. cases[16] = 10000;
  27. cases[17] = 25000;
  28. cases[18] = 50000;
  29. cases[19] = 75000;
  30. cases[20] = 100000;
  31. cases[21] = 200000;
  32. cases[22] = 300000;
  33. cases[23] = 400000;
  34. cases[24] = 500000;
  35. cases[25] = 750000;
  36. cases[26] = 1000000;
  37.  
  38.  
  39. }
  40.  
  41.  
  42.  
  43. public int numcasesleft(){
  44. int count = 0;
  45. for (int i = 1; i < cases.length; i++) {
  46. if(cases[i] != 0){
  47. count++;
  48. }
  49.  
  50. }
  51. return count;
  52. }
  53.  
  54. public String[] casesleft(){
  55. int left = numcasesleft();
  56. String [] toRet = new String[left];
  57. int location = 0;
  58. for (int o = 1; o < cases.length; o++) {
  59. if(cases[o] != 0){
  60. toRet[location] = o + "";
  61. location++;
  62. }
  63. }
  64. return toRet;
  65. }
  66.  
  67. public String valuesleft(){
  68. String toRet = "";
  69. for (int p = 1; p < cases.length; p++) {
  70. if(cases[p] != 0){
  71. toRet = toRet + cases[p] + " ";
  72. }
  73. }
  74. return toRet;
  75. }
  76.  
  77. public String toString(){
  78. String temp = "";
  79. for (int i = 0; i < cases.length; i++) {
  80. temp += cases[i] + " ";
  81. }
  82. return temp;
  83. }
  84.  
  85. public void swap(int A, int B){
  86. int temp = cases[A];
  87. cases[A] = cases[B];
  88. cases[B] = temp;
  89. }
  90. public int location(int number){
  91. String [] location = casesleft();
  92. String val = location[number];
  93. int intval = Integer.parseInt(val.trim());
  94. return intval;
  95. }
  96.  
  97. public int ValueOfCase(int location){
  98. return cases[location];
  99. }
  100.  
  101. public void findvalue(int value){
  102. for (int t = 0; t < cases.length; t++) {
  103. if(cases[t] == value){
  104. cases[t] = 0;
  105. break;
  106. }
  107.  
  108. }
  109. }
  110.  
  111. public void shuffle(){
  112. for (int i = 1; i < cases.length; i++) {
  113. int newlocation = (int)(Math.random() * (cases.length - 1)) + 1;
  114. swap(i,newlocation);
  115. }
  116. }
  117.  
  118. public void setLocation(int location){
  119. cases[location] = 0;
  120. }
  121.  
  122. public int SumOfValue(){
  123. int sum = 0;
  124.  
  125. for (int i = 0; i < cases.length; i++) {
  126. sum = sum + cases[i];
  127. }
  128.  
  129. return sum;
  130. }
  131.  
  132. public double average(){
  133. int sum = SumOfValue();
  134. int count = this.numcasesleft();
  135. return sum / count;
  136. }
  137.  
  138. public int midVal(){
  139. int value = 0;
  140. int spot = numcasesleft() / 2;
  141. int count = 0;
  142. for (int i = 0; i < cases.length; i++) {
  143. if(cases[i] > 0){
  144. count++;
  145. }
  146. if(count >= spot){
  147. value = cases[i];
  148. break;
  149. }
  150.  
  151. }
  152. return value;
  153. }
  154.  
  155. //public static void main(String[] args) {
  156. // TODO Auto-generated method stub
  157.  
  158. //}
  159.  
  160. }
  161.  
  162. //-------------------------------------------------------------------------------------------------------------------
  163. // Main class starts
  164.  
  165. import javax.swing.JOptionPane;
  166.  
  167. public class DealOrNoDealMain {
  168.  
  169. public static void pickfirstcase(Cases mycase, Cases valuecases){
  170. String t[] = mycase.casesleft();
  171. String values = valuecases.valuesleft();
  172. int n = JOptionPane.showOptionDialog(null,
  173. values,
  174. "what case do you want",
  175. JOptionPane.YES_NO_CANCEL_OPTION,
  176. JOptionPane.QUESTION_MESSAGE,
  177. null, t, t[1]);
  178. mycase.swap(n+1, 0);
  179. }
  180. public static void pickcases(Cases mycase, Cases valuecases){
  181. for (int r = 0; r < casestopick; r++) {
  182. String t[] = mycase.casesleft();
  183. String values = valuecases.valuesleft();
  184. int n = JOptionPane.showOptionDialog(null,
  185. values,
  186. "what case do you want, " + (casestopick - r),
  187. JOptionPane.YES_NO_CANCEL_OPTION,
  188. JOptionPane.QUESTION_MESSAGE,
  189. null, t, t[1]);
  190. int location = mycase.location(n);
  191. int value = mycase.ValueOfCase(location);
  192. JOptionPane.showMessageDialog(null, "The case has " + value);
  193. mycase.setLocation(location);
  194. valuecases.findvalue(value);
  195. }
  196. }
  197. public static int casestopick = 6;
  198. public static void main(String[] args) {
  199. Cases test = new Cases();
  200. test.shuffle();
  201. Cases test2 = new Cases();
  202.  
  203. pickfirstcase(test,test2);
  204. boolean choose = true;
  205. int value = 0;
  206. while(choose){
  207. pickcases(test,test2);
  208. value = test2.midVal() + (int)test2.average() / 2;
  209. int ch = JOptionPane.showConfirmDialog(null, "Your offer is " + value + ". Deal or No Deal?");
  210. if(ch ==0){
  211. choose = false;
  212. }
  213. if(casestopick > 1){
  214. casestopick = casestopick - 1;
  215. }
  216. if(test.numcasesleft() <= 2){
  217. choose = false;
  218. }
  219.  
  220. }
  221. int gob = test.ValueOfCase(0);
  222. int ta = 131448; // average of all the array numbers, not sure if I did the extra credit part right but it works
  223. JOptionPane.showMessageDialog(null, "Your case had " + gob);
  224. if(gob < ta){
  225. JOptionPane.showMessageDialog(null, "Your case was low, so you guessed good! :)");
  226. }
  227. if(gob > ta){
  228. JOptionPane.showMessageDialog(null, "Your case was high, so that's statistically bad... :(");
  229. }
  230.  
  231. }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement