Advertisement
Crenox

Craps Java Program

Sep 17th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. // By: Sammy Samkough
  2. // Craps
  3. // To simulate the popular game called, "Craps"
  4.  
  5. import java.util.Random;
  6.  
  7. public class Craps
  8. {
  9. private static Random r = new Random();
  10.  
  11. private int numFaces;
  12. private int faceValue;
  13.  
  14. public Craps()
  15. {
  16. numFaces = 6;
  17. faceValue = 1;
  18. }
  19.  
  20. public Craps(int sides)
  21. {
  22. numFaces = sides;
  23. faceValue = 1;
  24. }
  25.  
  26. public void roll()
  27. {
  28. faceValue = (int)(Math.random() * numFaces) + 1;
  29. }
  30.  
  31. // get results of rolled die
  32. public int getFaceValue()
  33. {
  34. return faceValue;
  35. }
  36.  
  37. public String toString()
  38. {
  39. // we put strings since faceValue is an int
  40. return "" + faceValue;
  41. }
  42. }
  43. -------------------------------------------------------------------------------------------------------------------------------
  44. // By: Sammy Samkough
  45. // Craps
  46. // To simulate the popular game called, "Craps"
  47.  
  48. import java.util.Scanner;
  49.  
  50. public class CrapsMain
  51. {
  52. public static void main(String args[])
  53. {
  54. // instance variables
  55. Craps d1 = new Craps();
  56. Craps d2 = new Craps(6);
  57. Scanner sc = new Scanner(System.in);
  58.  
  59. // variables
  60. int sum = 0;
  61. int points = 0;
  62. int roll = 1;
  63. boolean keepPlaying = true;
  64. String s;
  65.  
  66. while (keepPlaying)
  67. {
  68. // roll
  69. d1.roll();
  70. d2.roll();
  71.  
  72. // get results
  73. d1.getFaceValue();
  74. d2.getFaceValue();
  75.  
  76. sum = d1.getFaceValue() + d2.getFaceValue();
  77.  
  78. // print outs
  79. System.out.println("D1 = " + d1);
  80. System.out.println("D2 = " + d2);
  81. System.out.println("Sum = " + sum);
  82.  
  83. // RULES
  84. // #1 on the 1st roll if it rolls 7 or 11 you win
  85. if (roll == 1 && ((sum == 7)||(sum == 11)))
  86. {
  87. System.out.println("YOU WIN!");
  88. points = 0;
  89. roll = 1;
  90. }
  91.  
  92. // #2 you lose if you roll a 2, 3, or 12 on the first roll
  93. if (roll == 1 && sum == 2 || sum == 3 || sum == 12)
  94. {
  95. System.out.println("CRAPS! YOU LOSE!");
  96. points = 0;
  97. roll = 1;
  98. }
  99.  
  100. // #3 if you roll a 4, 5, 6, 8, 9, or 10 you get that amount of points
  101. if (sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10)
  102. {
  103. // add the points to each other
  104. points += sum;
  105. System.out.println("You have " + points + " point(s)");
  106. roll++;
  107. }
  108.  
  109. // #4 on any other roll besides the first and you get a 7, you lose
  110. if (roll != 1 && sum == 7)
  111. {
  112. System.out.println("CRAPS! YOU LOSE!");
  113. points = 0;
  114. roll = 1;
  115. }
  116.  
  117. // #5 you roll again if you roll a 2, 3, 11, or 12 if it's not the fist roll
  118. if (roll != 1 && ((sum == 2)||(sum == 3)||(sum == 11)||(sum == 12)))
  119. {
  120. System.out.println("Roll again.");
  121. roll++;
  122. }
  123.  
  124. System.out.println("Would you like to keep playing? Say yes to keep playing and no to stop.");
  125. // asking for input
  126. s = sc.nextLine();
  127.  
  128. // .equalsIgnoreCase excepts the string in any way it's capitalized
  129. if (s.equalsIgnoreCase("yes"))
  130. {
  131. System.out.println("Alright back to the top!");
  132. }
  133. else
  134. {
  135. // game over
  136. System.out.println("Okay, thanks for playing!");
  137. keepPlaying = false;
  138. }
  139. }
  140. }
  141. }
  142.  
  143. /*
  144. D1 = 3
  145. D2 = 2
  146. Sum = 5
  147. You have 5 point(s)
  148. Would you like to keep playing? Say yes to keep playing and no to stop.
  149. yes
  150. Alright back to the top!
  151. D1 = 2
  152. D2 = 1
  153. Sum = 3
  154. CRAPS! YOU LOSE!
  155. Would you like to keep playing? Say yes to keep playing and no to stop.
  156. yes
  157. Alright back to the top!
  158. D1 = 1
  159. D2 = 6
  160. Sum = 7
  161. YOU WIN!
  162. Would you like to keep playing? Say yes to keep playing and no to stop.
  163. yes
  164. Alright back to the top!
  165. D1 = 4
  166. D2 = 6
  167. Sum = 10
  168. You have 10 point(s)
  169. Would you like to keep playing? Say yes to keep playing and no to stop.
  170. yes
  171. Alright back to the top!
  172. D1 = 2
  173. D2 = 4
  174. Sum = 6
  175. You have 16 point(s)
  176. Would you like to keep playing? Say yes to keep playing and no to stop.
  177. yes
  178. Alright back to the top!
  179. D1 = 4
  180. D2 = 5
  181. Sum = 9
  182. You have 25 point(s)
  183. Would you like to keep playing? Say yes to keep playing and no to stop.
  184. yes
  185. Alright back to the top!
  186. D1 = 1
  187. D2 = 1
  188. Sum = 2
  189. Roll again.
  190. Would you like to keep playing? Say yes to keep playing and no to stop.
  191. yes
  192. Alright back to the top!
  193. D1 = 4
  194. D2 = 2
  195. Sum = 6
  196. You have 31 point(s)
  197. Would you like to keep playing? Say yes to keep playing and no to stop.
  198. yes
  199. Alright back to the top!
  200. D1 = 5
  201. D2 = 1
  202. Sum = 6
  203. You have 37 point(s)
  204. Would you like to keep playing? Say yes to keep playing and no to stop.
  205. yes
  206. Alright back to the top!
  207. D1 = 3
  208. D2 = 4
  209. Sum = 7
  210. CRAPS! YOU LOSE!
  211. Would you like to keep playing? Say yes to keep playing and no to stop.
  212. yes
  213. Alright back to the top!
  214. D1 = 5
  215. D2 = 2
  216. Sum = 7
  217. YOU WIN!
  218. Would you like to keep playing? Say yes to keep playing and no to stop.
  219. yes
  220. Alright back to the top!
  221. D1 = 4
  222. D2 = 4
  223. Sum = 8
  224. You have 8 point(s)
  225. Would you like to keep playing? Say yes to keep playing and no to stop.
  226. yes
  227. Alright back to the top!
  228. D1 = 2
  229. D2 = 4
  230. Sum = 6
  231. You have 14 point(s)
  232. Would you like to keep playing? Say yes to keep playing and no to stop.
  233. yes
  234. Alright back to the top!
  235. D1 = 4
  236. D2 = 4
  237. Sum = 8
  238. You have 22 point(s)
  239. Would you like to keep playing? Say yes to keep playing and no to stop.
  240. yes
  241. Alright back to the top!
  242. D1 = 6
  243. D2 = 5
  244. Sum = 11
  245. Roll again.
  246. Would you like to keep playing? Say yes to keep playing and no to stop.
  247. yes
  248. Alright back to the top!
  249. D1 = 1
  250. D2 = 5
  251. Sum = 6
  252. You have 28 point(s)
  253. Would you like to keep playing? Say yes to keep playing and no to stop.
  254. yes
  255. Alright back to the top!
  256. D1 = 5
  257. D2 = 6
  258. Sum = 11
  259. Roll again.
  260. Would you like to keep playing? Say yes to keep playing and no to stop.
  261. yes
  262. Alright back to the top!
  263. D1 = 6
  264. D2 = 3
  265. Sum = 9
  266. You have 37 point(s)
  267. Would you like to keep playing? Say yes to keep playing and no to stop.
  268. yes
  269. Alright back to the top!
  270. D1 = 3
  271. D2 = 2
  272. Sum = 5
  273. You have 42 point(s)
  274. Would you like to keep playing? Say yes to keep playing and no to stop.
  275. yes
  276. Alright back to the top!
  277. D1 = 1
  278. D2 = 5
  279. Sum = 6
  280. You have 48 point(s)
  281. Would you like to keep playing? Say yes to keep playing and no to stop.
  282. yes
  283. Alright back to the top!
  284. D1 = 3
  285. D2 = 5
  286. Sum = 8
  287. You have 56 point(s)
  288. Would you like to keep playing? Say yes to keep playing and no to stop.
  289. yes
  290. Alright back to the top!
  291. D1 = 3
  292. D2 = 4
  293. Sum = 7
  294. CRAPS! YOU LOSE!
  295. Would you like to keep playing? Say yes to keep playing and no to stop.
  296. no
  297. Okay, thanks for playing!
  298. Press any key to continue . . .
  299. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement