Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package nea;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.Random;
  15. import java.util.Scanner;
  16.  
  17. /**
  18. *
  19. * @author 9400
  20. */
  21. public class NEA {
  22.  
  23. /**
  24. * @param args the command line arguments
  25. */
  26. static int player1 = 0;
  27. static int player2 = 0;
  28. static Random rand = new Random();
  29. static String x;
  30. static String y;
  31. static Scanner s = new Scanner(System.in);
  32. static char User1;
  33. static char User2;
  34. static ArrayList <general>users = new ArrayList();
  35.  
  36. public static void main(String[] args) {
  37. // TODO code application logic here
  38. fileReader();
  39. int choice;
  40. do {
  41. System.out.println("Dice Game!");
  42. System.out.println("____________________");
  43. System.out.println("Main Menu: ");
  44. System.out.println("1 > Register");
  45. System.out.println("2 > Log-in & Play");
  46. System.out.println("3 > Hi-Scores");
  47. System.out.println("4 > Exit Game");
  48. System.out.println("____________________");
  49. System.out.println("Choose an option: 1-4");
  50. System.out.println("");
  51. choice = s.nextInt();
  52. s.skip("\n");
  53.  
  54. switch (choice) {
  55. case 1:
  56. register();
  57. break;
  58. case 2:
  59. login();
  60. break;
  61. case 3:
  62. hiscores();
  63. break;
  64. case 4:
  65. System.out.println("Goodbye");
  66. fileWriter();
  67. System.exit(0);
  68. break;
  69. default:
  70. System.out.println("Invalid Option. Try Again");
  71. System.out.println("");
  72. break;
  73. }
  74. } while (choice != 4);
  75. }
  76.  
  77. private static void register() {
  78. System.out.println("Create Username: ");
  79. System.out.println("Create Password: ");
  80. System.out.println("");
  81. }
  82.  
  83. private static void login() {
  84. System.out.println("Enter Username: ");
  85. System.out.println("Enter Password: ");
  86. System.out.println("");
  87.  
  88. for (int r = 1; r < 6; r++) {
  89. System.out.println("-------------Round " + r + "-------------");
  90. System.out.println("");
  91. System.out.println("Player 1: Enter any key to roll the dice");
  92. x = s.nextLine();
  93. int dice1 = Dice();
  94. System.out.println("Dice one: " + dice1);
  95. int dice2 = Dice();
  96. System.out.println("Dice two: " + dice2);
  97. int dice3 = 0;
  98. if (dice1 == dice2) {
  99. dice3 = Dice();
  100. System.out.println("Dice three: " + dice3);
  101. }
  102. int total = dice1 + dice2 + dice3;
  103. player1 = total + player1;
  104. if (player1 % 2 == 1) {
  105. player1 = player1 - 5;
  106. System.out.println("Player 1 TOTAL: " + player1);
  107. if (player1 < 0) {
  108. player1 = 0;
  109. System.out.println("Player 1 TOTAL: " + player1);
  110. }
  111. } else {
  112. player1 = player1 + 10;
  113. System.out.println("Player 1 TOTAL: " + player1);
  114. }
  115.  
  116. System.out.println("-------------------X-------------------");
  117.  
  118. System.out.println("Player 2: Enter any key to roll the dice");
  119. y = s.nextLine();
  120. dice1 = Dice();
  121. System.out.println("Dice one: " + dice1);
  122. dice2 = Dice();
  123. System.out.println("Dice two: " + dice2);
  124. dice3 = 0;
  125. if (dice1 == dice2) {
  126. dice3 = Dice();
  127. System.out.println("Dice three: " + dice3);
  128. }
  129. total = dice1 + dice2 + dice3;
  130. player2 = total + player2;
  131.  
  132. if (player2 % 2 == 1) {
  133. player2 = player2 - 5;
  134. System.out.println("Player 2 TOTAL: " + player2);
  135. if (player2 < 0) {
  136. player2 = 0;
  137. System.out.println("Player 2 TOTAL: " + player2);
  138. }
  139. } else {
  140. player2 = player2 + 10;
  141. System.out.println("Player 2 TOTAL: " + player2);
  142. }
  143.  
  144. }
  145. System.out.println("--------------------------------------");
  146. System.out.println("| Player 1 score = " + player1 + " |");
  147. System.out.println("--------------------------------------");
  148. System.out.println("| Player 2 score = " + player2 + " |");
  149. if (player1 > player2) {
  150. System.out.println("");
  151. System.out.println("THE WINNER IS: PLAYER 1");
  152. System.out.println("");
  153. } else {
  154. System.out.println("");
  155. System.out.println("THE WINNER IS: PLAYER 2");
  156. System.out.println("");
  157. }
  158. System.out.println("");
  159. if (player1 == player2) {
  160. sudden();
  161. }
  162. }
  163.  
  164. private static void hiscores() {
  165. System.out.println("Hi-Scores: ");
  166. System.out.println("");
  167. System.out.println("1. ");
  168. System.out.println("2. ");
  169. System.out.println("3. ");
  170. System.out.println("4. ");
  171. System.out.println("5. ");
  172. System.out.println("");
  173. }
  174.  
  175. private static void exit() {
  176. }
  177.  
  178. private static int Dice() {
  179. return (rand.nextInt(6) + 1);
  180. }
  181.  
  182. private static void sudden() {
  183. System.out.println("------------!Sudden Death!------------");
  184. while (player1 == player2) {
  185. System.out.println("Player 1: Enter any key to roll the dice");
  186. x = s.nextLine();
  187. int dice4 = Dice();
  188. System.out.println("Player 1 Score: " + dice4);
  189. System.out.println("--------------------------------------");
  190. System.out.println("Player 2: Enter any key to roll the dice");
  191. y = s.nextLine();
  192. dice4 = Dice();
  193. System.out.println("Player 2 Score: " + dice4);
  194. if (player1 >= player2) {
  195. System.out.println("The Winner is: Player 1");
  196. } else {
  197. System.out.println("The Winner is: Player 2");
  198. }
  199. }
  200. }
  201.  
  202. private static void fileReader() {
  203. try {
  204. BufferedReader br = new BufferedReader((new FileReader("N:/data.txt")));
  205. String a;
  206. while ((a = br.readLine()) != null) {
  207. String[] s = a.split(";");
  208. //System.out.println(Arrays.toString(s));
  209. int hiscore = Integer.parseInt(s[2]);
  210. users.add(new general(hiscore, s[0], s[1]));
  211. }
  212. System.out.println(users.toString());
  213. System.out.println("Data Loaded Successfully");
  214. System.out.println("");
  215. br.close();
  216. } catch (IOException e) {
  217. System.out.println("Error: " + e.getMessage());
  218. }
  219.  
  220. }
  221.  
  222. private static void fileWriter() {
  223. try {
  224. FileWriter fw = new FileWriter("N:/newFile.txt");
  225. fw.write("This is some data.");
  226. fw.close();
  227. System.out.println("Data File Saved Successfully");
  228. System.out.println("");
  229. } catch (IOException e) {
  230. System.out.println("Error: " + e.getMessage());
  231. }
  232. }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement