Advertisement
Guest User

Untitled

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