liangm20

dice game thing

Nov 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. //this is Michelle's code
  2. import java.util.*;
  3. import javax.swing.JFrame;
  4. import java.awt.*;
  5. public class dicegame extends JFrame
  6. {
  7. static int win; //initializing my variables I'll use throughout the entire program
  8. static int dice1;
  9. static int dice2;
  10. public void paint (Graphics g) //paint class
  11. {
  12. super.paint(g);
  13. drawdice(g,dice1,dice2);
  14. }
  15. public static void drawdice(Graphics g,int dice1,int dice2) //method used for drawing the two dice
  16. {
  17. if (dice1==1) //different if statements for each value of dice1 and dice2; dice1 is the die in the upper left hand corner, and dice2 is lower right hand corner die
  18. {
  19. g.drawRect(30,30,150,150);
  20. g.fillOval(105,105,20,20);
  21. }
  22. if (dice1==2)
  23. {
  24. g.drawRect(30,30,150,150);
  25. g.fillOval(60,60,20,20);
  26. g.fillOval(150,150,20,20);
  27. }
  28. if(dice1==3)
  29. {
  30. g.drawRect(30,30,150,150);
  31. g.fillOval(60,60,20,20);
  32. g.fillOval(105,105,20,20);
  33. g.fillOval(150,150,20,20);
  34. }
  35. if(dice1==4)
  36. {
  37. g.drawRect(30,30,150,150);
  38. g.fillOval(60,60,20,20);
  39. g.fillOval(60,150,20,20);
  40. g.fillOval(150,60,20,20);
  41. g.fillOval(150,150,20,20);
  42. }
  43. if(dice1==5)
  44. {
  45. g.drawRect(30,30,150,150);
  46. g.fillOval(60,60,20,20);
  47. g.fillOval(105,105,20,20);
  48. g.fillOval(150,150,20,20);
  49. g.fillOval(150,60,20,20);
  50. g.fillOval(60,150,20,20);
  51. }
  52. if(dice1==6)
  53. {
  54. g.drawRect(30,30,150,150);
  55. g.fillOval(60,60,20,20);
  56. g.fillOval(60,90,20,20);
  57. g.fillOval(60,120,20,20);
  58. g.fillOval(150,60,20,20);
  59. g.fillOval(150,90,20,20);
  60. g.fillOval(150,120,20,20);
  61. }
  62. if(dice2==1)
  63. {
  64. g.drawRect(200,200,150,150);
  65. g.fillOval(275,275,20,20);
  66. }
  67. if(dice2==2)
  68. {
  69. g.drawRect(200,200,150,150);
  70. g.fillOval(230,230,20,20);
  71. g.fillOval(320,320,20,20);
  72. }
  73. if(dice2==3)
  74. {
  75. g.drawRect(200,200,150,150);
  76. g.fillOval(230,230,20,20);
  77. g.fillOval(275,275,20,20);
  78. g.fillOval(320,320,20,20);
  79. }
  80. if(dice2==4)
  81. {
  82. g.drawRect(200,200,150,150);
  83. g.fillOval(230,230,20,20);
  84. g.fillOval(230,320,20,20);
  85. g.fillOval(320,230,20,20);
  86. g.fillOval(320,320,20,20);
  87. }
  88. if(dice2==5)
  89. {
  90. g.drawRect(200,200,150,150);
  91. g.fillOval(230,230,20,20);
  92. g.fillOval(275,275,20,20);
  93. g.fillOval(320,320,20,20);
  94. g.fillOval(320,230,20,20);
  95. g.fillOval(230,320,20,20);
  96. }
  97. if(dice2==6)
  98. {
  99. g.drawRect(200,200,150,150);
  100. g.fillOval(230,230,20,20);
  101. g.fillOval(230,260,20,20);
  102. g.fillOval(230,290,20,20);
  103. g.fillOval(320,230,20,20);
  104. g.fillOval(320,260,20,20);
  105. g.fillOval(320,290,20,20);
  106. }
  107. }
  108. public static void main(String [] args) //main method of the game; calls all methods related to the game
  109. {
  110. win = 500; //initializing set amount of points that the game starts out with
  111. Random rand = new Random(); //making random numbers from 1-6 for dice1 and dice2
  112. dice1 = rand.nextInt(6)+1;
  113. dice2 = rand.nextInt(6)+1;
  114. Scanner prompt = new Scanner(System.in); //scanner that allows user to play
  115. System.out.println("Welcome to Michelle's dice game! Do you want to play? Type Y or N. ");
  116. String answer = prompt.next();
  117. if(answer.equals("Y")||answer.equals("y"))
  118. {
  119. System.out.println("Welcome! You start off with 500 points. Every roll you make with these two");
  120. System.out.println("dice with impact your fortune.");
  121. System.out.println("To win the game, you must get 1000 points or more.");
  122. System.out.println("Every time you roll the dice, the dice will appear in another window.");
  123. Scanner scan = new Scanner(System.in); //scanner allowing to roll the dice
  124. System.out.println("Type ROLL to roll the two dice.");
  125. String roll1 = scan.nextLine();
  126. if(roll1.equals("roll")||roll1.equals("ROLL"))
  127. {
  128. points(win); //calls points method
  129. }
  130. else
  131. {
  132. System.out.println("That's not the word roll. Goodbye.");
  133. }
  134. }
  135. else
  136. {
  137. System.out.print("Okay, goodbye.");
  138. }
  139. }
  140. public static void points(int win) //method containing switch case, controls amount of points made per roll
  141. {
  142. if(win<1000 && win>0)
  143. {
  144. switch (dice1+dice2)
  145. {
  146. case 2:
  147. win = win+200; //if sum of dice1 and dice2 (the roll) = 2, the user gains 200 pts
  148. System.out.println("You rolled a sum of 2! You have won 200 points. Your total is now " + win + ".");
  149. break;
  150. case 3:
  151. win = win-300; //if sum = 3, user loses 300
  152. System.out.println("You rolled a sum of 3! You have lost 300 points. Your total is now " + win + ".");
  153. break;
  154. case 4:
  155. win = win+400; //if sum = 4, user wins 400
  156. System.out.println("You have rolled a sum of 4! You have won 400 points. Your total is now " + win + ".");
  157. break;
  158. case 5:
  159. win = win-100; //if sum = 5, user loses 100
  160. System.out.println("You have rolled a sum of 5! You have lost 100 points. Your total is now " + win + ".");
  161. break;
  162. case 6:
  163. win = win+150; //if sum = 6, user wins 150
  164. System.out.println("You have rolled a sum of 6! You have won 150 points. Your total is now " + win + ".");
  165. break;
  166. case 7:
  167. win = win-50; //if sum = 7, user loses 50
  168. System.out.println("You have rolled a sum of 7! You have lost 50 points. Your total is now " + win + ".");
  169. break;
  170. case 8:
  171. win = win+250; //if sum = 8, user wins 250
  172. System.out.println("You have rolled a sum of 8! You have won 250 points. Your total is now " + win + ".");
  173. break;
  174. case 9:
  175. win = win+100; //if sum = 9, user wins 100
  176. System.out.println("You have rolled a sum of 9! You have won 100 points. Your total is now " + win + ".");
  177. break;
  178. case 10:
  179. win = win+100; //if sum = 10, user wins 100
  180. System.out.println("You have rolled a sum of 10! You have won 100 points. Your total is now " + win + ".");
  181. break;
  182. case 11:
  183. win = win-200; //if sum = 11, user loses 200
  184. System.out.println("You have rolled a sum of 11! You have lost 200 points. Your total is now " + win + ".");
  185. break;
  186. case 12:
  187. win = win+250; //if sum = 12, user wins 250
  188. System.out.println("You have rolled a sum of 12! You have won 250 points. Your total is now " + win + ".");
  189. break;
  190. }
  191. dicegame app = new dicegame(); //causes the frame with the 2 dice to show up every time this method runs
  192. app.setSize(500,500);
  193. app.setVisible(true);
  194. ask(win); //calls ask method
  195. }
  196. else
  197. {
  198. System.out.println("You lost all of your points and lost the game. Game over.");
  199. }
  200. }
  201. public static void ask(int win) //method asking user if they want to roll again
  202. {
  203. if(win<1000)
  204. {
  205. Scanner prompt1 = new Scanner(System.in);
  206. System.out.println("Wanna go again? Type Y or N.");
  207. String ans = prompt1.next();
  208. if (ans.equals("y") || ans.equals("Y") || ans.equals("yes"))
  209. {
  210. Random rand = new Random(); //randomizes value of dice1 and dice2 again for each time the user wants to roll again
  211. dice1 = rand.nextInt(6)+1;
  212. dice2 = rand.nextInt(6)+1;
  213. points(win); //calling the points method again
  214. }
  215. else
  216. {
  217. System.out.println("Okay, goodbye.");
  218. }
  219. }
  220. if(win>=1000)
  221. {
  222. System.out.println("You won 1000 points or more and won the game!");
  223. }
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment