fork download
  1. import java.util.*;
  2. public class Yahtzee{
  3. public static boolean forWhile = true; // not needed out here. I'm to lazy to move it
  4. public static boolean [] scoreOptions = new boolean[13]; // Keeps track of used scoring options
  5. public static void main(String [] args){// Main Method
  6. Scanner input = new Scanner(System.in); // scanner
  7. int [] player1 = new int [6];// player dice and score
  8. System.out.println(" Welcome new player. \n You are about to play Robert's Yahtzee Program" +
  9. "\n In the event that you don't remember how to play yahtzee you can find the instructions here:"+
  10. "\n http://www.hasbro.com/common/instruct/Yahtzee.pdf"+
  11. "\n The computer will roll the dice for you the first then you can either keep what you have "+
  12. "\n or reroll and choose specific dice to keep. \n Alright! Now Let's Play");
  13.  
  14. while(running()){// start game loop
  15. boolean [] keepDice = new boolean [5];// determines whether a particular die will be rerolled or not
  16. for (int rollNum=0; rollNum<3; rollNum++){// cycle through three rolls
  17. for (int roll= 0; roll < 5; roll++){ // cycle through each dice
  18. if(!keepDice[roll]){// if dice gets rerolled
  19. player1 [roll] = ((int)(Math.random()*100) %6 + 1);//roll dice
  20. }
  21. }// end dice rolling
  22. //------------DISPLAYS CURRENT DICE AND SCORE-------------------------------------
  23. System.out.print("Your Current dice are:\n");
  24. for(int counter = 0; counter < 5; counter++){
  25. System.out.print("Dice " + counter + " is " + player1[counter]+ " \n");
  26. }
  27. System.out.println("and your score is " +player1[5]);
  28. //----------------------------------------------------------------------------------
  29. if(rollNum < 2)System.out.println("Would you like to roll again? (Y/N)");
  30. if(rollNum < 2 && input.next().equalsIgnoreCase("n")){// breaks loop if you don't want to roll again
  31. break;
  32. }
  33. //------------------CHOOSE WHICH DICE TO KEEP ON REROLL-------------------------------
  34. else if(rollNum<2){
  35. System.out.println("Please type in the dice numbers of the dice of you wish to save or type 7 to reroll all dice\n"+
  36. " Example: if you wanted to keep dice 3 and 4 you would type: 34");
  37. String response = input.next();
  38. char [] keepers = response.toCharArray();
  39. for(int counter = 0; counter<keepDice.length; counter++){
  40. keepDice[counter] = false;
  41. }
  42. for(int counter = 0; counter <keepers.length; counter++){
  43. if (keepers[counter]-48>= 0 && keepers[counter]-48 <= 4){
  44. keepDice[keepers[counter]-48] = true;
  45. }
  46. }
  47. }
  48. //-- ----------------------------------------------------------------------------------------
  49. }// end of dice rolls
  50. //----------------SCORE OPTION MENU------------------------------------------------------------
  51. while(forWhile){// loops until valid choice is selected
  52. System.out.println("\nPlease pick your score option:\n 1) Ones 2) Twos 3) Threes \n 4) Fours 5) Fives 6) Sixs"+
  53. "\n 7) 3 Of A Kind 8) 4 Of A Kind 9) Full House \n 10) Small Straight 11) Large Straight 12) Chance \n 13) YAHTZEE!");
  54. int scoreChoice = 0;
  55. do{
  56. try{
  57. scoreChoice = input.nextInt();// score option choice
  58. }
  59. catch(InputMismatchException e){
  60. input.next();
  61. System.out.println("Error: Invalid. Please type in an integer value!");
  62. }
  63. }while (scoreChoice == 0);
  64. //-----------------------------------------------------------------------------------------------
  65. //------------------Select choice, mark choice as used and run relevant method that returns an int value added to the score--------
  66. if(scoreChoice>= 1 && scoreChoice <= 6){
  67. if(!scoreOptions[scoreChoice-1]){
  68. scoreOptions[scoreChoice-1] = true;
  69. player1[5] += diceNumber(player1, scoreChoice);
  70. forWhile = false;
  71. }
  72. else{System.out.println("Error: Option already used. Please pick a new option!");}
  73. }
  74. if(scoreChoice == 7){
  75. if(!scoreOptions[scoreChoice-1]){
  76. scoreOptions[scoreChoice-1]=true;
  77. player1[5] += threeOfAKind(player1);
  78. forWhile = false;
  79. }
  80. else{System.out.println("Error: Option already used. Please pick a new option!");}
  81. }
  82. if(scoreChoice == 8){
  83. if(!scoreOptions[scoreChoice-1]){
  84. scoreOptions[scoreChoice-1]=true;
  85. player1[5] += fourOfAKind(player1);
  86. forWhile = false;
  87. }
  88. else{System.out.println("Error: Option already used. Please pick a new option!");}
  89. }
  90. if(scoreChoice == 9){
  91. if(!scoreOptions[scoreChoice-1]){
  92. scoreOptions[scoreChoice-1]=true;
  93. player1[5] += fullHouse(player1);
  94. forWhile = false;
  95. }
  96. else{System.out.println("Error: Option already used. Please pick a new option!");}
  97. }
  98. if(scoreChoice == 10){
  99. if(!scoreOptions[scoreChoice-1]){
  100. scoreOptions[scoreChoice-1]=true;
  101. player1[5] += smallStraight(player1);
  102. forWhile = false;
  103. }
  104. else{System.out.println("Error: Option already used. Please pick a new option!");}
  105. }
  106. if(scoreChoice == 11){
  107. if(!scoreOptions[scoreChoice-1]){
  108. scoreOptions[scoreChoice-1]=true;
  109. player1[5] += largeStraight(player1);
  110. forWhile = false;
  111. }
  112. else{System.out.println("Error: Option already used. Please pick a new option!");}
  113. }
  114. if(scoreChoice == 12){
  115. if(!scoreOptions[scoreChoice-1]){
  116. scoreOptions[scoreChoice-1]=true;
  117. player1[5] += chance(player1);
  118. forWhile = false;
  119. }
  120. else{System.out.println("Error: Option already used. Please pick a new option!");}
  121. }
  122. if(scoreChoice == 13){
  123. if(!scoreOptions[scoreChoice-1]){
  124. scoreOptions[scoreChoice-1]=true;
  125. player1[5] += yahtzee(player1);
  126. forWhile = false;
  127. }
  128. else{System.out.println("Error: Option already used. Please pick a new option!");}
  129. }
  130. //----------------------------------------------------------------------------------------------------------------------
  131. }
  132. forWhile = true;
  133. }// end of game loop
  134. System.out.print("\nYour Final Score Is " + player1[5]);
  135. }
  136. // -----------------methods that check dice against rules for points--------------------------------------
  137. public static int diceNumber(int [] dice, int x){
  138. int sum = 0;
  139. for(int counter = 0; counter < dice.length-1; counter ++){
  140. if(dice[counter]== x){
  141. sum+= x;
  142. }
  143. }
  144. return sum;
  145. }
  146. public static int threeOfAKind(int [] dice){
  147. int sum= 0, x=0,y=1;
  148. for(int counter = 0; counter < dice.length-1; counter++){
  149. y=1;
  150. for(int counter2 =0; counter2 < dice.length-1; counter2++){
  151. if(dice[counter]==dice[counter2]&& counter!=counter2){
  152. x=dice[counter];
  153. y++;
  154. }
  155. }
  156. if(y>=3){
  157. sum = x;
  158. }
  159. }
  160. if(sum>0){
  161. sum=0;
  162. for(int counter=0; counter< dice.length-1; counter++){
  163. sum+=dice[counter];
  164. }
  165. }
  166. return sum;
  167. }
  168. public static int fourOfAKind(int [] dice){
  169. int sum= 0, x=0,y=1;
  170. for(int counter = 0; counter < dice.length-1; counter++){
  171. y=1;
  172. for(int counter2 =0; counter2 < dice.length-1; counter2++){
  173. if(dice[counter]==dice[counter2]&& counter!=counter2){
  174. x=dice[counter];
  175. y++;
  176. }
  177. }
  178. if(y>=4){
  179. sum = x;
  180. }
  181. }
  182. if(sum>0){
  183. sum=0;
  184. for(int counter=0; counter< dice.length-1; counter++){
  185. sum+=dice[counter];
  186. }
  187. }
  188. return sum;
  189. }
  190. public static int fullHouse(int [] dice){
  191. int sum= 0,y=1,z=1;
  192. for(int counter = 0; counter < dice.length-1; counter++){
  193. y=1;
  194. z=1;
  195. for(int counter2 =0; counter2 < dice.length-1; counter2++){
  196. if(dice[counter]==dice[counter2]&& counter!=counter2){
  197. y++;
  198. z++;
  199. }
  200. }
  201. if((y==3)&&(sum==2 || sum==0)){
  202. sum += y;
  203. }
  204. if((z==2)&&(sum==3 || sum==0)){
  205. sum += z;
  206. }
  207. }
  208. if(sum==5){
  209. sum=25;}
  210. return sum;
  211. }
  212. public static int chance(int [] dice){
  213. int sum = 0;
  214. for(int counter = 0; counter< dice.length-1; counter++){
  215. sum+= dice[counter];
  216. }
  217. return sum;
  218. }
  219. public static int yahtzee(int [] dice){
  220. int x=0;
  221. for(int counter = 0; counter < dice.length-1; counter++){
  222. if(dice[counter]==dice[counter+1])
  223. x++;
  224. }
  225. if(x==4)
  226. return 50;
  227. return 0;
  228. }
  229. public static int smallStraight(int [] dice){
  230. int []dice2 = {dice[0], dice[1], dice[2], dice[3], dice[4]};
  231. dice2= Useful.sortIntArray(dice2);
  232. int x = 0;
  233. for(int counter = 0; counter < dice2.length-1; counter++){
  234. if(dice2[counter]+1 == dice2[counter+1]){
  235. x++;
  236. }
  237. }
  238. if(x ==3){
  239. return 30;
  240. }
  241. else{return 0;}
  242. }
  243. public static int largeStraight(int [] dice){
  244. int []dice2 = {dice[0], dice[1], dice[2], dice[3], dice[4]};
  245. dice2= Useful.sortIntArray(dice2);
  246. int x = 0;
  247. for(int counter = 0; counter < dice2.length-1; counter++){
  248. if(dice2[counter]+1 == dice2[counter+1]){
  249. x++;
  250. }
  251. }
  252. if(x ==4){
  253. return 40;
  254. }
  255. else{return 0;}
  256. }
  257. //----------------------------------------------------------------------------------------------
  258. public static boolean running(){// game loop condition, is true while any score options aren't used
  259. for( int counter = 0; counter < scoreOptions.length; counter++){
  260. if(!scoreOptions[counter]){
  261. return true;}
  262. }
  263. return false;
  264. }
  265. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:2: error: class Yahtzee is public, should be declared in a file named Yahtzee.java
public class Yahtzee{
       ^
Main.java:231: error: cannot find symbol
		dice2= Useful.sortIntArray(dice2); 
		       ^
  symbol:   variable Useful
  location: class Yahtzee
Main.java:245: error: cannot find symbol
		dice2= Useful.sortIntArray(dice2); 
		       ^
  symbol:   variable Useful
  location: class Yahtzee
3 errors
stdout

Standard output is empty