Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. package model;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Yatzy {
  6. // Face values of the 5 dice.
  7. // 1 <= values[i] <= 6.
  8. private int[] values = new int[5];
  9.  
  10. // Number of times the 5 dice have been thrown.
  11. // 0 <= throwCount <= 3.
  12. private int throwCount = 0;
  13.  
  14. // Random number generator.
  15. private Random random = new Random();
  16.  
  17. public Yatzy() {
  18.  
  19. //
  20. }
  21.  
  22. /**
  23. * Returns the 5 face values of the dice.
  24. */
  25. public int[] getValues() {
  26. // TODO
  27. return values;
  28. }
  29.  
  30. /**
  31. * Sets the 5 face values of the dice. Pre: values contains 5 face values in
  32. * [1..6]. Note: This method is only meant to be used for test, and therefore
  33. * has package visibility.
  34. */
  35. void setValues(int[] values) {
  36. // TODO
  37. this.values = values;
  38. }
  39.  
  40. /**
  41. * Returns the number of times the 5 dice has been thrown.
  42. */
  43. public int getThrowCount() {
  44. // TODO
  45.  
  46. return throwCount;
  47. }
  48.  
  49. /**
  50. * Resets the throw count.
  51. */
  52. public void resetThrowCount() {
  53. // TODO
  54. throwCount = 0;
  55. }
  56.  
  57. /**
  58. * Rolls the 5 dice. Only roll dice that are not hold. Pre: holds contain 5
  59. * boolean values.
  60. */
  61. public void throwDice(boolean[] holds) {
  62. // TODO
  63. for (int i = 0; i < values.length; i++) {
  64. if (holds[i] == false) {
  65. values[i] = random.nextInt(6) + 1;
  66. }
  67. }
  68. throwCount++;
  69.  
  70. }
  71.  
  72. // -------------------------------------------------------------------------
  73.  
  74. /**
  75. * Returns all results possible with the current face values. The order of the
  76. * results is the same as on the score board. Note: This is an optional method.
  77. * Comment this method out, if you don't want use it.
  78. */
  79. public int[] getResults() {
  80. int[] results = new int[15];
  81. for (int i = 0; i <= 5; i++) {
  82. results[i] = this.sameValuePoints(i + 1);
  83. }
  84. results[6] = this.onePairPoints();
  85. results[7] = this.twoPairPoints();
  86. results[8] = this.threeSamePoints();
  87. results[9] = this.fourSamePoints();
  88. results[10] = this.fullHousePoints();
  89. results[11] = this.smallStraightPoints();
  90. results[12] = this.largeStraightPoints();
  91. results[13] = this.chancePoints();
  92. results[14] = this.yatzyPoints();
  93.  
  94. return results;
  95. }
  96.  
  97. // -------------------------------------------------------------------------
  98.  
  99. // Returns an int[7] containing the frequency of face values.
  100. // Frequency at index v is the number of dice with the face value v, 1 <= v
  101. // <= 6.
  102. // Index 0 is not used.
  103. private int[] calcCounts() {
  104. // TODO
  105. int[] calcCounts = new int[7];
  106. for (int i = 0; i < values.length; i++) {
  107. calcCounts[values[i]]++;
  108. }
  109.  
  110. return calcCounts;
  111. }
  112.  
  113. /**
  114. * Returns same-value points for the given face value. Returns 0, if no dice has
  115. * the given face value. Pre: 1 <= value <= 6;
  116. */
  117. public int sameValuePoints(int value) {
  118. // TODO
  119. int[] calcCounts = calcCounts();
  120. return (calcCounts()[value] * value);
  121. }
  122.  
  123. /**
  124. * Returns points for one pair (for the face value giving highest points).
  125. * Returns 0, if there aren't 2 dice with the same face value.
  126. */
  127. public int onePairPoints() {
  128. // TODO
  129. int points = 0;
  130. int[] frequency = calcCounts();
  131. for (int i = 0; i < frequency.length; i++) {
  132. if (frequency[i] >= 2) {
  133. points = i * 2;
  134. }
  135. }
  136. return points;
  137. }
  138.  
  139. /**
  140. * Returns points for two pairs (for the 2 face values giving highest points).
  141. * Returns 0, if there aren't 2 dice with one face value and 2 dice with a
  142. * different face value.
  143. */
  144. public int twoPairPoints() {
  145. // TODO
  146. int counts = 0;
  147. int points = 0;
  148. int[] frequency1 = calcCounts();
  149. for (int i = 0; i < frequency1.length; i++) {
  150. if (frequency1[i] >= 2) {
  151. points = points + i * 2;
  152. counts++;
  153.  
  154. }
  155.  
  156. }
  157. if (counts != 2) {
  158. points = 0;
  159. }
  160.  
  161. return points;
  162. }
  163.  
  164. /**
  165. * Returns points for 3 of a kind. Returns 0, if there aren't 3 dice with the
  166. * same face value.
  167. */
  168. public int threeSamePoints() {
  169. // TODO
  170. int points = 0;
  171. int[] frequency1 = calcCounts();
  172. for (int i = 0; i < frequency1.length; i++) {
  173. if (frequency1[i] >= 3) {
  174. points = points + i * 3;
  175. }
  176. }
  177. return points;
  178. }
  179.  
  180. /**
  181. * Returns points for 4 of a kind. Returns 0, if there aren't 4 dice with the
  182. * same face value.
  183. */
  184. public int fourSamePoints() {
  185. // TODO
  186. int points = 0;
  187. int[] frequency1 = calcCounts();
  188. for (int i = 0; i < frequency1.length; i++) {
  189. if (frequency1[i] >= 4) {
  190. points = points + i * 4;
  191. }
  192. }
  193. return points;
  194. }
  195.  
  196. /**
  197. * Returns points for full house. Returns 0, if there aren't 3 dice with one
  198. * face value and 2 dice a different face value.
  199. */
  200. public int fullHousePoints() {
  201. // TODO
  202. int max1 = 0;
  203. int max2 = 0;
  204. int[] frequency = calcCounts();
  205.  
  206. for (int i = 0; i < frequency.length; i++) {
  207. if (frequency[i] == 3) {
  208. max1 = i;
  209. }
  210. if (frequency[i] == 2) {
  211. max2 = i;
  212. }
  213.  
  214. }
  215. if (max1 == 0 || max2 == 0) {
  216. max1 = 0;
  217. max2 = 0;
  218. }
  219. return (max1 * 3) + (max2 * 2);
  220.  
  221. }
  222.  
  223. /**
  224. * Returns points for small straight. Returns 0, if the dice are not showing
  225. * 1,2,3,4,5.
  226. */
  227. public int smallStraightPoints() {
  228. // TODO
  229. int points = 0;
  230. int rolledOnce = 0;
  231. int[] frequency1 = calcCounts();
  232. for (int i = 1; i < frequency1.length - 1; i++) {
  233. if (frequency1[i] == 1) {
  234. rolledOnce++;
  235. }
  236. }
  237. if (rolledOnce == 5) {
  238. points = (1 + 2 + 3 + 4 + 5);
  239. }
  240. return points;
  241.  
  242. }
  243.  
  244. /**
  245. * Returns points for large straight. Returns 0, if the dice is not showing
  246. * 2,3,4,5,6.
  247. */
  248. public int largeStraightPoints() {
  249. // TODO
  250. int points = 0;
  251. int rolledOnce = 0;
  252. int[] frequency1 = calcCounts();
  253. for (int i = 2; i < frequency1.length; i++) {
  254. if (frequency1[i] == 1) {
  255. rolledOnce++;
  256. }
  257. }
  258. if (rolledOnce == 5) {
  259. points = (2 + 3 + 4 + 5 + 6);
  260. }
  261. return points;
  262.  
  263. }
  264.  
  265. /**
  266. * Returns points for chance.
  267. */
  268. public int chancePoints() {
  269. // TODO
  270. int points = 0;
  271.  
  272. for (int i = 0; i < values.length; i++) {
  273. points = points + values[i];
  274.  
  275. }
  276. return points;
  277. }
  278.  
  279. /**
  280. * Returns points for yatzy. Returns 0, if there aren't 5 dice with the same
  281. * face value.
  282. */
  283. public int yatzyPoints() {
  284. // TODO
  285. int points = 0;
  286. int[] frequency1 = calcCounts();
  287. for (int i = 0; i < frequency1.length; i++) {
  288. if (frequency1[i] >= 5) {
  289. points = 50;
  290. }
  291. }
  292.  
  293. return points;
  294. }
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement