Posted By

PalCoder on 06/05/13


Tagged

command game line


Versions (?)

Yahtzee Game with command line


 / Published in: Java
 

URL: http://en.wikipedia.org/wiki/Yahtzee

this is an implementation for Yahtzee game in JAVA programming language. Unfortunately its command line

  1. import java.util.*;
  2. public class Yahtzee {
  3.  
  4. /**
  5.   * @param args the command line arguments
  6.   */
  7. static final int PLAY = 1;
  8. static final int Exit = 2;
  9. static final int Math32 = 32;
  10. static final int Math3 = 3;
  11. static final int Math4 = 4;
  12. static final int Math5 = 5;
  13. static Random ran = new Random();
  14. static int[] dices = new int[5];
  15. static ChoiceTable table = new ChoiceTable();
  16. static int round_count = 0;
  17. static int upper_total = 0;
  18. static int lower_total = 0;
  19. static int choice = 0;
  20. static int exit_round = 0;
  21. static int score_choice = 0;
  22. static Scanner in = new Scanner(System.in);
  23.  
  24.  
  25. public static void main(String[] args) {
  26. // TODO code application logic here
  27.  
  28.  
  29. do {
  30. //LOAD THE SCORE OPTION MENU
  31.  
  32. System.out.println("Please choose one off the follow:\n1-Play\n2-Exit");
  33. choice = in.nextInt();
  34. if (choice == PLAY) {
  35. /*
  36.   * THIS CHOICE IS LEADING TO PLAY THE GAME FOR 13 ROUNDS
  37.   */
  38. while (round_count < 13) {
  39. System.out.println("_________________________________");
  40. System.out.println("| The result of Round " + (round_count + 1) + "is");
  41. System.out.print("| ");
  42. for (int i = 0; i < 5; i++) {
  43. dices[i] = ran.nextInt(6) + 1;
  44. /*
  45.   * INETAILIZE THE DICES WITH RANDOM VALUES AND PRINT THEM
  46.   */
  47. System.out.print(dices[i] + "");
  48. if (i < 5) {
  49. System.out.print(" - ");
  50. }
  51. }
  52. System.out.println();
  53. System.out.println("_________________________________");
  54.  
  55. /*
  56.   * CHOSE THE YOUR SCORE METHOD
  57.   */
  58. selection();
  59. /*
  60.   * GO TO THE NEXT ROUND UNTIL ROUND COUNT =13
  61.   */
  62.  
  63. round_count++;
  64. }
  65.  
  66. /*
  67.   * VIEW THE RESULT
  68.   */
  69. System.out.println("*******************************************************");
  70. System.out.println("* The result is\n* The upper Total is "
  71. + upper_total + "\n* The Lower Total is " + lower_total + "\n* Total is " + (lower_total + upper_total));
  72. System.out.println("*******************************************************");
  73. /*
  74.   * REST THE ROUND COUNT VALUE THE FLAGE ARRAY FOR NEW GAME
  75.   */
  76. table.resetTable();
  77. upper_total=0;
  78. lower_total=0;
  79. round_count = 0;
  80.  
  81. } else if (choice == Exit) {
  82. System.exit(1);
  83. } else {
  84. System.out.println("Bad Choice");
  85. }
  86. } while (true);
  87.  
  88. }
  89.  
  90. public static void selection() {
  91.  
  92. System.out.println(table.getPrintableMenu());
  93. score_choice = in.nextInt();
  94. Arrays.sort(dices);
  95. //ModifyList(score_choice);
  96. table.modifyTable(score_choice-1);
  97. switch (score_choice) {
  98. case 1:
  99. sumSam(1);
  100. break;
  101. case 2:
  102. sumSam(2);
  103. break;
  104. case 3:
  105. sumSam(3);
  106. break;
  107. case 4:
  108. sumSam(4);
  109. break;
  110.  
  111. case 5:
  112. sumSam(5);
  113. break;
  114. case 6:
  115. sumSam(6);
  116. break;
  117. case 7://THREE MATCHES
  118. if (table.getTable()[6].getFlag()) {
  119. if (match() == Math3) {
  120. for (int i = 0; i < dices.length; i++) {
  121. lower_total += dices[i];
  122. }
  123. System.out.println("LowerTotal =" + lower_total);
  124. } else {
  125. System.out.println("illegal option but you consum around");
  126. }
  127. table.getTable()[6].setFlag(false);
  128. } else {
  129. System.out.println("this option is consumed");
  130. }
  131.  
  132. break;
  133. case 8:
  134. //FOUR MATCHES
  135. if (table.getTable()[7].getFlag()) {
  136. if (match() == Math4) {
  137. for (int i = 0; i < dices.length; i++) {
  138. lower_total += dices[i];
  139. }
  140. System.out.println("LowerTotal =" + lower_total);
  141. } else {
  142. System.out.println("illegal option but you consum around");
  143. }
  144. table.getTable()[7].setFlag(false);
  145. } else {
  146. System.out.println("this option is consumed");
  147. }
  148.  
  149. break;
  150. case 9://THREE MATCHES AND PAIR
  151. if (table.getTable()[8].getFlag()) {
  152. if (match() == Math32) {
  153. lower_total += 25;
  154. System.out.println("LowerTotal =" + lower_total);
  155. } else {
  156. System.out.println("illegal option but you consum around");
  157. }
  158. table.getTable()[8].setFlag(false);
  159. } else {
  160. System.out.println("this option is consumed");
  161. }
  162.  
  163. break;
  164. case 10://FOUR CONSECUTIVE NUMBERS
  165. if (table.getTable()[9].getFlag()) {
  166. if (consecutive() == 4) {
  167. lower_total += 30;
  168. System.out.println("LowerTotal =" + lower_total);
  169. } else {
  170. System.out.println("illegal option but you consum around");
  171. }
  172. table.getTable()[9].setFlag(false);
  173. } else {
  174. System.out.println("this option is consumed");
  175. }
  176. break;
  177. case 11://FIVE CONSECUTIVE NUMBERS
  178. if (table.getTable()[10].getFlag()) {
  179. if (consecutive() == 5) {
  180. lower_total += 40;
  181. System.out.println("LowerTotal =" + lower_total);
  182. } else {
  183. System.out.println("illegal option but you consum around");
  184. }
  185. table.getTable()[10].setFlag(false);
  186. } else {
  187. System.out.println("this option is consumed");
  188. }
  189.  
  190. break;
  191. case 12://FIVE MATCHES
  192. if (table.getTable()[11].getFlag()) {
  193. if (match() == Math5) {
  194. lower_total += 50;
  195. System.out.println("LowerTotal =" + lower_total);
  196. } else {
  197. System.out.println("illegal option but you consum around");
  198. }
  199. table.getTable()[11].setFlag(false);
  200. } else {
  201. System.out.println("this option is consumed");
  202. }
  203.  
  204. break;
  205. case 13:
  206. if (table.getTable()[12].getFlag()) {
  207. for (int i = 0; i < dices.length; i++) {
  208. lower_total += dices[i];
  209. }
  210. System.out.println("lower Total =" + lower_total);
  211. table.getTable()[12].setFlag(false);
  212. } else {
  213. System.out.println("this option is consumed");
  214. }
  215.  
  216. break;
  217. default:
  218. System.out.println("illegal option but you consum around");
  219. }
  220.  
  221.  
  222. }
  223.  
  224. /**
  225.   *
  226.   * @param indix
  227.   */
  228. public static void sumSam(int indix) {
  229.  
  230. if (table.getTable()[indix - 1].getFlag()) {
  231. for (int i = 0; i < dices.length; i++) {
  232. if (dices[i] == indix) {
  233. upper_total += indix;
  234. if (upper_total > 63) { //IF SATISFY THE CONDITION ADD BONUSE
  235. upper_total += 35;
  236. }
  237. }
  238. }
  239. System.out.println("Upper Total =" + upper_total);
  240. table.getTable()[indix - 1].setFlag(false);
  241. } else {
  242. System.out.println("this option is consumed");
  243. }
  244.  
  245. }
  246.  
  247. public static int match() {
  248. int matches = 1;
  249. int counter = 0;
  250.  
  251. for (int i = 0; i < 4; i++) {
  252. if (dices[i] == dices[i + 1]) {
  253. matches += 1;
  254. } else {
  255. counter = matches;
  256. matches = 1;
  257. }
  258. }
  259.  
  260. if ((counter == 2 && matches == 3) || (counter == 3 && matches == 2)) {
  261. return Math32;
  262. } else if ((counter == 3 || matches == 3)) {
  263. return Math3;
  264. } else if ((counter == 4 || matches == 4)) {
  265. return Math4;
  266. } else if ((matches == 5)) {
  267. return Math5;
  268. }
  269.  
  270. return matches;
  271. }
  272.  
  273. public static int consecutive() {
  274. int count = 1;
  275. for (int i = 1; i < 5; i++) {
  276. if (dices[i] == (dices[i - 1] + 1)) {
  277. count = count + 1;
  278. }
  279. }
  280. return count;
  281. }
  282.  
  283. }
  284. /*
  285.  *
  286.  * THE FIRST CLASSS IS CHOICE
  287.  */
  288.  
  289. class Choice {
  290.  
  291. private String choice;
  292. private boolean flag;
  293.  
  294. public Choice() {
  295. this.choice = "ch";
  296. this.flag = true;
  297. }
  298. /* public Choice(String ch, boolean fl) {
  299.   this.choice = ch;
  300.   this.flag = fl;
  301.   }*/
  302.  
  303. public void setCh(String ch) {
  304. this.choice = ch;
  305. }
  306.  
  307. public String getCh() {
  308. return this.choice;
  309. }
  310.  
  311. public void setFlag(boolean fl) {
  312. this.flag = fl;
  313. }
  314.  
  315. public boolean getFlag() {
  316. return this.flag;
  317. }
  318. }
  319.  
  320. /*
  321.  *
  322.  * THE SECOND CLASSS IS CHOICE TABLE
  323.  */
  324. class ChoiceTable {
  325.  
  326. private Choice[] table;
  327.  
  328. public ChoiceTable() {
  329. this.table = new Choice[13];
  330. for (int i = 0; i < this.table.length; i++) {
  331. this.table[i] = new Choice();
  332. }
  333. this.resetTable();
  334. }
  335.  
  336. public Choice[] getTable() {
  337. return this.table;
  338. }
  339.  
  340. public String getPrintableMenu() {
  341. String menu = "\n############################################################\n"
  342. + "# Select your Score Method\n# A-Upper Part";
  343.  
  344.  
  345. for (int i = 0; i < 6; i++) {
  346. menu += this.table[i].getCh();
  347. }
  348. menu += "\n# B-Lowwer Part";;
  349.  
  350. for (int i = 6; i < 13; i++) {
  351. menu += this.table[i].getCh();
  352. }
  353. menu += "\n############################################################\n";;
  354.  
  355. return menu;
  356. }
  357.  
  358. public void modifyTable(int indix) {
  359. if (indix < 14 & indix >= 0) {
  360. this.table[indix].setCh("\n#\t" + (indix +1)+ "-XXXXXXXX");
  361. }
  362. }
  363.  
  364. public void resetTable() {
  365. for (int i = 0; i < this.table.length; i++) {
  366. if (i < 6) {
  367. this.table[i].setCh("\n#\t" + (i + 1) + "-sum " + (i + 1) + "\'s");
  368. }
  369. this.table[i].setFlag(true);
  370. }
  371. this.table[6].setCh("\n#\t7-3 Matches");
  372. this.table[7].setCh("\n#\t8-4 Matches");
  373. this.table[8].setCh("\n#\t9-3 Matches and Pair");
  374. this.table[9].setCh("\n#\t10-Small Straight (1-2-3-4, 2-3-4-5, or 3-4-5-6)");
  375. this.table[10].setCh("\n#\t11-Large Straight (1-2-3-4-5 or 2-3-4-5-6)");
  376. this.table[11].setCh("\n#\t12-Yahtzee");
  377. this.table[12].setCh("\n#\t13-Chance");
  378. }
  379. }

Report this snippet  

You need to login to post a comment.