Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. package dicegame;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8.  
  9. /**
  10. *
  11. * @author Zaki
  12. */
  13. public class DiceGame {
  14.  
  15. static Scanner L = new Scanner(System.in);
  16. static Random rand = new Random();
  17. static int diceroll;
  18. static int dice1;
  19. static int dice2;
  20. static int dice3 = 0;
  21. static int p1Score;
  22. static int p2Score;
  23. static int roundScore;
  24. static int p1Grand;
  25. static int p2Grand;
  26. static int p1suddendeath;
  27. static int p2suddendeath;
  28.  
  29. public static void main(String[] args) {
  30.  
  31. int option;
  32. System.out.println("Welcome to the Dice Game!");
  33. fileReader();
  34. do {
  35.  
  36. System.out.println("______________________________");
  37. System.out.println("Main Menu");
  38. System.out.println("Choose from the following options: \n 1 Register to play \n 2 Log in to play \n 3 Hi-Scores \n 4 End game");
  39. System.out.println("Enter the preferred number");
  40.  
  41. option = L.nextInt();
  42. L.skip("\n");
  43. switch (option) {
  44. case 1:
  45. Register();
  46. break;
  47. case 2:
  48. LogIn();
  49. break;
  50. case 3:
  51. HiScore();
  52. break;
  53. case 4:
  54. EndGame();
  55. break;
  56. default:
  57. System.out.println("Invalid Option");
  58. break;
  59. }
  60. } while (option > 4 || option <= 3);
  61.  
  62. }
  63.  
  64. private static void Register() {
  65. System.out.println("_______________________________");
  66. System.out.println("Register to play \n Please enter your Username:");
  67. System.out.println("Please enter your Password");
  68.  
  69. }
  70.  
  71. private static void LogIn() {
  72. System.out.println("Player 1 enter your username:");
  73. System.out.println("Player 2 enter your password:");
  74. System.out.println("Player 1 enter your username:");
  75. System.out.println("Player 2 enter your password:");
  76. round();
  77. }
  78.  
  79. private static void HiScore() {
  80. System.out.println("INSERT TABLE HERE BRO");
  81. }
  82.  
  83. private static void EndGame() {
  84. System.out.println("Thank you for playing the Dice Game!");
  85. }
  86.  
  87. private static void game() {
  88. System.out.println("Lets play! \n Player 1 Press any key to roll the dice");
  89.  
  90. char roll = L.nextLine().charAt(0);
  91. scorecalc();
  92.  
  93. System.out.println("Player 2 turn \n Press any key");
  94. char roll2 = L.nextLine().charAt(0);
  95. scorecalc2();
  96.  
  97. }
  98.  
  99. public static int diceRoll() {
  100. return diceroll = rand.nextInt(6) + 1;
  101. }
  102.  
  103. private static int oddEven(int Score) {
  104. if (Score % 2 == 0) {
  105. Score = Score + 10;
  106. } else {
  107. Score = Score - 5;
  108. }
  109.  
  110. return Score;
  111. }
  112.  
  113. private static void round() {
  114. for (int i = 1; i < 6; i++) {
  115. System.out.println("--------------Round " + i + "---------------");
  116. game();
  117. SuddenDeath();
  118. }
  119. }
  120.  
  121. private static void scorecalc() {
  122. dice1 = diceRoll();
  123. dice2 = diceRoll();
  124. System.out.println("You rolled a " + dice1 + " and a " + dice2);
  125. if (dice1 == dice2) {
  126. System.out.println("You rolled a double");
  127. dice3 = diceRoll();
  128. System.out.println("The score for the third roll is: " + dice3);
  129. }
  130.  
  131. p1Score = dice1 + dice2 + dice3;
  132. p1Score = oddEven(p1Score);
  133. p1Score = BelowZero(p1Score);
  134. System.out.println("Your Score is: " + p1Score);
  135. p1Grand = p1Grand + p1Score;
  136. System.out.println("Player1 Grand Total is: " + p1Grand);
  137. }
  138.  
  139. private static void scorecalc2() {
  140.  
  141. dice1 = diceRoll();
  142. dice2 = diceRoll();
  143. System.out.println("You rolled a " + dice1 + " and a " + dice2);
  144. if (dice1 == dice2) {
  145. System.out.println("You rolled a double");
  146. dice3 = diceRoll();
  147. System.out.println("The score for the third roll is: " + dice3);
  148. }
  149. p2Score = dice1 + dice2 + dice3;
  150. p2Score = oddEven(p2Score);
  151. p2Score = BelowZero(p2Score);
  152. System.out.println("Your Score is: " + p2Score);
  153. p2Grand = p2Grand + p2Score;
  154. System.out.println("Player1 Grand Total is: " + p2Grand);
  155.  
  156. }
  157.  
  158. private static int BelowZero(int score) {
  159. if (score < 0) {
  160. score = 0;
  161. }
  162. return score;
  163. }
  164.  
  165. private static void SuddenDeath() {
  166.  
  167. if (p1Grand == p2Grand) {
  168. char trigger = L.nextLine().charAt(0);
  169. p1suddendeath = diceRoll();
  170. p2suddendeath = diceRoll();
  171.  
  172. do {
  173. if (p1suddendeath > p2suddendeath) {
  174. System.out.println("Player 1 Wins!");
  175. } else if (p2suddendeath > p1suddendeath) {
  176. System.out.println("Player 2 Wins!");
  177. } else if (p2suddendeath == p1suddendeath) {
  178. System.out.println("Sudden Death Again...");
  179. }
  180. } while (p1Grand == p2Grand);
  181.  
  182. }
  183. }
  184.  
  185. private static void fileReader() {
  186. try {
  187. BufferedReader br = new BufferedReader((new FileReader("N:/PlayerData.txt")));
  188. String s;
  189. while ((s = br.readLine()) != null) {
  190. }
  191. br.close();
  192. System.out.println("Data read Successfully");
  193. } catch (IOException e) {
  194. System.out.println("Error: " + e.getMessage());
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement