Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1. /*
  2. * File: Yahtzee.java
  3. * ------------------
  4. * This program plays the game Yahtzee.
  5. * Names: Disha Dasgupta and Amanda Tu
  6. * Section Leader: Joshua Spayd
  7. */
  8.  
  9. import acm.io.*;
  10. import acm.program.*;
  11. import acm.util.*;
  12.  
  13. public class Yahtzee extends GraphicsProgram implements YahtzeeConstants {
  14.  
  15. /* These constants specify the scores for any special category where the score is the same
  16. * fixed number every time (Full House, Yahtzee, Small Straight, and Large Straight). */
  17. public static final int FULL_HOUSE_SCORE = 25;
  18. public static final int YAHTZEE_SCORE = 50;
  19. public static final int SMALL_STRAIGHT_SCORE = 30;
  20. public static final int LARGE_STRAIGHT_SCORE = 40;
  21. public static final int BONUS_THRESHOLD = 63;
  22. public static final int BONUS_AMOUNT = 35;
  23.  
  24. public static void main(String[] args) {
  25. new Yahtzee().start(args);
  26. }
  27.  
  28. public void run() {
  29. IODialog dialog = getDialog();
  30. nPlayers = dialog.readInt("Please enter the number of players.");
  31. playerNames = new String[nPlayers];
  32. for (int playerNum = 1; playerNum <= nPlayers; playerNum++) {
  33. playerNames[playerNum - 1] = dialog.readLine("Enter name for player " + playerNum);
  34. }
  35. display = new YahtzeeDisplay(getGCanvas(), playerNames);
  36. scoreCardMatrix = new int [N_CATEGORIES + 1][nPlayers];
  37. for(int round = 1; round <= 2; round++) {
  38. for (int turn = 1; turn <= nPlayers; turn++) {
  39. player = turn;
  40. playGame();
  41. }
  42. }
  43. displayConcludingScores();
  44. }
  45.  
  46. private void playGame() {
  47. display.printMessage(playerNames [player - 1] + ", please click 'Roll Dice' button to roll the dice");
  48. display.waitForPlayerToClickRoll(player);
  49. runFirstRoll();
  50. for(int i = 2; i <= 3; i++) {
  51. runSubsequentRoll();
  52. }
  53. display.printMessage(playerNames [player - 1] + ", select a category for this roll");
  54. chooseCategoryToCheck();
  55.  
  56. }
  57.  
  58. private void runFirstRoll() {
  59. for (int i = 0; i <= N_DICE - 1; i++) {
  60. diceResult[i] = rgen.nextInt(1,6);
  61. }
  62. display.displayDice(diceResult);
  63. }
  64.  
  65. private void runSubsequentRoll() {
  66. display.printMessage(playerNames [player - 1] + ", please select the dice you wish to re-roll and click 'Roll Again'");
  67. display.waitForPlayerToSelectDice();
  68. rerollSelectedDice();
  69. display.displayDice(diceResult);
  70. }
  71.  
  72. private void rerollSelectedDice() {
  73. for (int dice = 0; dice <= N_DICE - 1; dice++) {
  74. if (display.isDieSelected(dice) == true) {
  75. diceResult[dice] = rgen.nextInt(1,6);
  76.  
  77. }
  78. }
  79. }
  80.  
  81. private void chooseCategoryToCheck() {
  82. category = display.waitForPlayerToSelectCategory();
  83. if(category == ONES || category == TWOS || category == THREES || category == FOURS || category == FIVES || category == SIXES) {
  84. checkUpperSectionCategories();
  85. } else {
  86. checkLowerSectionCategories();
  87. }
  88. }
  89.  
  90. private void checkUpperSectionCategories() {
  91. resetAllCounters();
  92. for(int categoryNum = ONES; categoryNum <= SIXES; categoryNum++){
  93. if(category == categoryNum) {
  94. for(int i = 0; i <= diceResult.length - 1; i++) {
  95. if(diceResult[i] == categoryNum) {
  96. score += diceResult[i];
  97. }
  98. }
  99. }
  100. }
  101. display.updateScorecard(category, player, score);
  102. updateOngoingScore();
  103. }
  104.  
  105. private void checkLowerSectionCategories() {
  106. resetAllCounters();
  107. checkThreeOfAKind();
  108. checkFourOfAKind();
  109. checkFullHouse();
  110. checkSmallStraight();
  111. checkLargeStraight();
  112. checkYahtzee();
  113. checkChance();
  114. updateOngoingScore();
  115. }
  116.  
  117. private void checkThreeOfAKind() {
  118. if(category == THREE_OF_A_KIND){
  119. readAllDiceValues();
  120. if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
  121. score = onesCounter + twosCounter + threesCounter + foursCounter + fivesCounter + sixesCounter;
  122. }
  123. display.updateScorecard(category, player, score);
  124. }
  125. }
  126.  
  127. private void checkFourOfAKind() {
  128. if(category == FOUR_OF_A_KIND){
  129. readAllDiceValues();
  130. if (onesCounter >= 4 || twosCounter >= 8 || threesCounter >= 12 || foursCounter >= 16 || fivesCounter >= 20 || sixesCounter >= 24) {
  131. score = onesCounter + twosCounter + threesCounter + foursCounter + fivesCounter + sixesCounter;
  132. }
  133. }
  134. }
  135.  
  136. private void checkFullHouse() {
  137. if (category == FULL_HOUSE) {
  138. readAllDiceValues();
  139. if (onesCounter >= 2) {
  140. if (twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
  141. score = FULL_HOUSE_SCORE;
  142. }
  143. }
  144. if (twosCounter >= 4){
  145. if (onesCounter >= 3 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
  146. score = FULL_HOUSE_SCORE;
  147. }
  148. }
  149. if (threesCounter >= 6){
  150. if (onesCounter >= 3 || twosCounter >= 6 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
  151. score = FULL_HOUSE_SCORE;
  152. }
  153. }
  154. if (foursCounter >= 8){
  155. if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || fivesCounter >= 15 || sixesCounter >= 18) {
  156. score = FULL_HOUSE_SCORE;
  157. }
  158. }
  159. if (fivesCounter >= 10){
  160. if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || sixesCounter >= 18) {
  161. score = FULL_HOUSE_SCORE;
  162. }
  163. }
  164. if (sixesCounter >= 12){
  165. if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || twosCounter >= 6) {
  166. score = FULL_HOUSE_SCORE;
  167. }
  168. }
  169. display.updateScorecard(category, player, score);
  170. }
  171. }
  172.  
  173. private void checkSmallStraight() {
  174. if (category == SMALL_STRAIGHT) {
  175. readAllDiceValues();
  176. if (onesCounter >= 1 && twosCounter >= 2 && threesCounter >= 3 && foursCounter >= 4) {
  177. score = SMALL_STRAIGHT_SCORE;
  178. }
  179. if (twosCounter >= 2 && threesCounter >= 3 && foursCounter >= 4 && fivesCounter >= 5) {
  180. score = SMALL_STRAIGHT_SCORE;
  181. }
  182. if (threesCounter >= 3 && foursCounter >= 4 && fivesCounter >= 5 && sixesCounter >= 6) {
  183. score = SMALL_STRAIGHT_SCORE;
  184. }
  185. }
  186. display.updateScorecard(category, player, score);
  187. }
  188.  
  189. private void checkLargeStraight() {
  190. if (category == LARGE_STRAIGHT) {
  191. readAllDiceValues();
  192. if (onesCounter == 1 && twosCounter == 2 && threesCounter == 3 && foursCounter == 4 && fivesCounter == 5) {
  193. score = LARGE_STRAIGHT_SCORE;
  194. }
  195. if (twosCounter == 2 && threesCounter == 3 && foursCounter == 4 && fivesCounter == 5 && sixesCounter == 6) {
  196. score = LARGE_STRAIGHT_SCORE;
  197. }
  198. }
  199. display.updateScorecard(category, player, score);
  200. }
  201.  
  202. private void checkYahtzee() {
  203. if(category == YAHTZEE) {
  204. for (int i = 1; i < N_DICE; i++) {
  205. if(diceResult[i] == diceResult[i-1]) {
  206. score = YAHTZEE_SCORE;
  207. }
  208. }
  209. }
  210. }
  211.  
  212. private void checkChance() {
  213. if(category == CHANCE){
  214. readAllDiceValues();
  215. score = onesCounter + twosCounter + threesCounter + foursCounter + fivesCounter + sixesCounter;
  216. display.updateScorecard(category, player, score);
  217. }
  218. }
  219.  
  220. private void readAllDiceValues() {
  221. for (int i = 0; i < N_DICE; i++) {
  222. if(diceResult[i] == 1) {
  223. onesCounter++;
  224. } else if (diceResult[i] == 2) {
  225. twosCounter+=2;
  226. } else if (diceResult[i] == 3) {
  227. threesCounter+=3;
  228. } else if (diceResult[i] == 4) {
  229. foursCounter+=4;
  230. } else if (diceResult[i] == 5) {
  231. fivesCounter+=5;
  232. } else if (diceResult[i] == 6) {
  233. sixesCounter+=6;
  234. }
  235. }
  236. }
  237.  
  238. private void resetAllCounters() {
  239. score = 0;
  240. onesCounter = 0;
  241. twosCounter = 0;
  242. threesCounter = 0;
  243. foursCounter = 0;
  244. fivesCounter = 0;
  245. sixesCounter = 0;
  246. }
  247.  
  248. private void updateOngoingScore() {
  249. scoreCardMatrix [category][player - 1] = score;
  250. scoreCardMatrix [TOTAL][player - 1] += score;
  251. display.updateScorecard(category, player, score);
  252. display.updateScorecard(TOTAL, player, scoreCardMatrix [TOTAL][player - 1]);
  253. }
  254.  
  255. private void calculateUpperSectionScore() {
  256. for (int player = 1; player <= nPlayers; player++) {
  257. for(int i = ONES; i <= SIXES; i++) {
  258. scoreCardMatrix [UPPER_SCORE][player - 1] = scoreCardMatrix [i] [player - 1];
  259. display.updateScorecard(UPPER_SCORE, player, scoreCardMatrix [UPPER_SCORE][player - 1]);
  260. }
  261. }
  262. }
  263.  
  264. private void calculateLowerSectionScore() {
  265. for (int player = 1; player <= nPlayers; player++) {
  266. for(int i = THREE_OF_A_KIND; i <= CHANCE; i++) {
  267. scoreCardMatrix [LOWER_SCORE][player - 1] = scoreCardMatrix [i] [player - 1];
  268. display.updateScorecard(LOWER_SCORE, player, scoreCardMatrix [LOWER_SCORE][player - 1]);
  269. }
  270. }
  271. }
  272.  
  273. private void calculateUpperBonus() {
  274. if(scoreCardMatrix [UPPER_SCORE][player - 1] >= BONUS_THRESHOLD) {
  275. scoreCardMatrix [UPPER_BONUS][player - 1] = BONUS_AMOUNT;
  276. display.updateScorecard(UPPER_BONUS, player, scoreCardMatrix [UPPER_BONUS][player - 1]);
  277. }
  278. }
  279.  
  280. private void calculateFinalScore() {
  281. for (int player = 1; player <= nPlayers; player++) {
  282. scoreCardMatrix [TOTAL][player - 1] += scoreCardMatrix [UPPER_BONUS] [player - 1];
  283. display.updateScorecard(TOTAL, player, scoreCardMatrix [TOTAL][player - 1]);
  284.  
  285. }
  286. }
  287.  
  288. private void determineWinner() {
  289. int highestValue = scoreCardMatrix [TOTAL][0];
  290. for (int player = 1; player <= nPlayers; player++) {
  291. if (scoreCardMatrix [TOTAL][player - 1] > highestValue) {
  292. highestValue = scoreCardMatrix [TOTAL][player - 1];
  293. }
  294. }
  295.  
  296. display.printMessage("You win with a score of: " + highestValue);
  297. }
  298.  
  299. private void displayConcludingScores () {
  300. calculateUpperSectionScore();
  301. calculateLowerSectionScore();
  302. calculateUpperBonus();
  303. calculateFinalScore();
  304. determineWinner();
  305. }
  306.  
  307. /* Private instance variables */
  308. private int nPlayers;
  309. private String[] playerNames;
  310. private YahtzeeDisplay display;
  311. private RandomGenerator rgen = new RandomGenerator();
  312. private int[] diceResult = new int[N_DICE];
  313. private int category;
  314. private int score = 0;
  315. private int player;
  316. private int onesCounter = 0;
  317. private int twosCounter = 0;
  318. private int threesCounter = 0;
  319. private int foursCounter = 0;
  320. private int fivesCounter = 0;
  321. private int sixesCounter = 0;
  322. private int scoreCardMatrix [][];
  323.  
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement