gyarados96

VOCAB

Nov 22nd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.50 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. public class VocabBuilder
  5. {
  6. //boolean value used as condition in runMenu() to quit the whole game if true (will not run the main menu anymore)
  7. private boolean quit;
  8.  
  9. //holds the index value in the array for the current user logged in
  10. //it is initalised as -1, which means no user has logged in yet
  11. private int currentPlayer = -1;
  12.  
  13. //initialises the ArrayList that will hold player information (the player objects)
  14. ArrayList<Player> playerList = new ArrayList<Player>();
  15.  
  16.  
  17. public static void main(String[] args){
  18. VocabBuilder menu = new VocabBuilder(); //constructs the Main Menu
  19. menu.runMenu(); //runs the main Menu
  20. }
  21.  
  22. //This method is used to run the menu
  23. private void runMenu()
  24. {
  25. while (!quit)
  26. {
  27. //Only runs the menu if the user has not selected to quit
  28. printHeader();
  29. printMenu();
  30. char choice = getUserChoice();
  31. performMenu(choice);
  32. }
  33. }
  34.  
  35. //This method is used to print the top of the main menu
  36. private void printHeader()
  37. {
  38. if (currentPlayer==-1)
  39. {
  40. System.out.println(" Welcome to the Word Game");
  41. }
  42. else
  43. {
  44. System.out.println(" Welcome to the Word Game " + playerList.get(currentPlayer).getName() + "!");
  45. }
  46. }
  47.  
  48. //This method is used to print the content of the main menu
  49. private static void printMenu()
  50. {
  51. System.out.println(" Login (L)");
  52. System.out.println(" Register (R)");
  53. System.out.println(" About (A)");
  54. System.out.println(" Play the Game (P)");
  55. System.out.println(" Show the Leader Board (B)");
  56. System.out.println(" Quit (Q)");
  57. System.out.print("\n Please choose an option: ");
  58. }
  59.  
  60. //This method is used to validate and get the user's choice from the main menu
  61. private char getUserChoice()
  62. {
  63. Scanner scan = new Scanner(System.in);
  64. char selection = scan.next().charAt(0);
  65. char selectionUpper = Character.toUpperCase(selection);
  66.  
  67. //checks for input error
  68. while ((selectionUpper!='L') && (selectionUpper!='R')&&(selectionUpper!='A') && (selectionUpper!='P')&&(selectionUpper!='B') && (selectionUpper!='Q'))
  69. {
  70. System.out.println("\n You have entered an incorrect menu choice.");
  71. System.out.print(" Please enter one of the choices given: ");
  72. selection = scan.next().charAt(0);
  73. selectionUpper = Character.toUpperCase(selection);
  74. }
  75.  
  76. return selectionUpper; //returns a correct menu choise as an upper case letter
  77. }
  78.  
  79. private void performMenu(char choice)
  80. {
  81. switch (choice)
  82. {
  83. case 'L':
  84. Login();
  85. break;
  86. case 'R':
  87. Register();
  88. break;
  89. case 'A':
  90. printAbout();
  91. break;
  92. case 'P':
  93. Play();
  94. break;
  95. case 'B':
  96. quit = true;
  97. break;
  98. case 'Q':
  99. quit = true;
  100. break;
  101. }
  102. }
  103.  
  104. private static void printAbout(){
  105. System.out.println("\n Instructions on how to play the Word game:");
  106. System.out.println(" This game is a vocabulary builder, which aims to help you in learning the definition of certain words.");
  107. System.out.println(" Each time you play the game, a new word will appear, and you will have to select a synonym from a list");
  108. System.out.println(" of possible options. Only one word will be the correct answer each time. After selecting a word, the");
  109. System.out.println(" game will let you know whether you have chosen the correct synonym, and if not, will show the correct answer.");
  110. System.out.println(" Each game is comprised of 10 guessing stages, and during each stage you will be shown your current score.");
  111. System.out.println(" At the end of the game, you can compare your final score with those of other players in the leaderboard.");
  112. System.out.println();
  113. }
  114.  
  115. //creates temporary variables to hold the player's information during registration
  116. private void Register()
  117. {
  118. String tempName, tempSurname, tempUsername, tempPassword;
  119.  
  120. //each variable is assigned values by calling the methods responsible for getting the user's details
  121. tempName = setName();
  122. tempSurname = setSurname();
  123. tempUsername = setUsername();
  124. tempPassword = setPassword();
  125.  
  126. System.out.println("\n You have successfully registered a new player with username " + tempUsername);
  127. System.out.println(" You need to log in to play the game");
  128.  
  129. //creates a new player object using the player's details
  130. Player newPlayer = new Player(tempName,tempSurname,tempUsername,tempPassword, 0, 0);
  131.  
  132. //adds the new player's details to the array list
  133. playerList.add(newPlayer);
  134. }
  135.  
  136. private String setName()
  137. {
  138. Scanner scan = new Scanner(System.in);
  139. System.out.print("\n Enter your first name: ");
  140. String name = scan.nextLine();
  141. while (!nameValidation(name))
  142. {
  143. System.out.print("\n Please enter your first name again: ");
  144. name = scan.nextLine();
  145. }
  146. return name;
  147. }
  148.  
  149. private String setSurname()
  150. {
  151. Scanner scan = new Scanner(System.in);
  152. System.out.print("\n Enter your last name: ");
  153. String surname = scan.nextLine();
  154. while (!nameValidation(surname))
  155. {
  156. System.out.print("\n Please enter your last name again: ");
  157. surname = scan.nextLine();
  158. }
  159. return surname;
  160. }
  161.  
  162. private boolean nameValidation(String valName)
  163. {
  164. boolean valid = true;
  165. String numbers = "(.*[0-9].*)";
  166. String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
  167.  
  168. if (valName.length() <2 )
  169. {
  170. System.out.print("\n Your name must be at least 2 characters long.");
  171. valid = false;
  172. }
  173.  
  174. if (valName.matches(numbers))
  175. {
  176. System.out.print("\n Your name can not contain any numbers.");
  177. valid = false;
  178. }
  179.  
  180. if (valName.matches(specialChars))
  181. {
  182. System.out.print("\n Your name can not contain any special characters.");
  183. valid = false;
  184. }
  185.  
  186. return valid;
  187. }
  188.  
  189. private String setUsername()
  190. {
  191. Scanner scan = new Scanner(System.in);
  192. System.out.print("\n Guidelines for choosing your username: ");
  193. System.out.print("\n 1) Your player username needs to be at least 3 characters long and no more than 12.");
  194. System.out.print("\n 2) The first character can not be a number.");
  195. System.out.print("\n 3) The username can not contain any special characters. ");
  196. System.out.print("\n Enter your desired player username: ");
  197. String username = scan.nextLine();
  198. while (!usernameValidation(username))
  199. {
  200. System.out.print("\n Please enter a new username: ");
  201. username = scan.nextLine();
  202. }
  203. return username.toLowerCase();
  204. }
  205.  
  206. private boolean usernameValidation(String valUsername) {
  207. boolean valid = true;
  208. String numbers = "(.*[0-9].*)";
  209. String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
  210.  
  211. if ((valUsername.length() < 3) || (valUsername.length() > 12))
  212. {
  213. System.out.print("\n Your username is too short or too long.");
  214. valid = false;
  215. }
  216.  
  217. if ((valUsername.substring(0,1)).matches(numbers))
  218. {
  219. System.out.print("\n Your username should not begin with a number.");
  220. valid = false;
  221. }
  222.  
  223. if (valUsername.matches(specialChars))
  224. {
  225. System.out.print("\n Your username should not contain any special characters.");
  226. valid = false;
  227. }
  228.  
  229. for (int i=0; i < playerList.size(); i++)
  230. {
  231. if (valUsername.equalsIgnoreCase(playerList.get(i).getUsername()))
  232. {
  233. System.out.print("\n This username is already in use.");
  234. valid = false;
  235. break; //usernames will be unique, so we know there won't be another one, so no need to continue the loop
  236. }
  237. }
  238.  
  239. return valid;
  240. }
  241.  
  242. private String setPassword()
  243. {
  244. Scanner scan = new Scanner(System.in);
  245. System.out.print("\n Guidelines for choosing your password: ");
  246. System.out.print("\n 1) Your password needs to be at least 8 characters long but no more than 15.");
  247. System.out.print("\n 2) Your password should contain at least one uppercase letter");
  248. System.out.print("\n 3) Your password should contain at least one special character. ");
  249. System.out.print("\n Enter your desired player password: ");
  250. String password = scan.nextLine();
  251. while (!passwordValidation(password))
  252. {
  253. System.out.print("\n Please enter a new password: ");
  254. password = scan.nextLine();
  255. }
  256. return password;
  257. }
  258.  
  259. private boolean passwordValidation(String valPassword){
  260. boolean valid = true;
  261. String upperCase = "(.*[A-Z].*)";
  262. String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
  263.  
  264. if ((valPassword.length() < 8) || (valPassword.length() > 15))
  265. {
  266. System.out.print("\n Your password is too short or too long.");
  267. valid = false;
  268. }
  269.  
  270. if (!valPassword.matches(upperCase))
  271. {
  272. System.out.print("\n Your password should contain at least one uppercase letter.");
  273. valid = false;
  274. }
  275.  
  276. if (!valPassword.matches(specialChars))
  277. {
  278. System.out.print("\n Your password should contain at least one special character.");
  279. System.out.print("\n Tip: Some of the characters you can use are #,$,%,*,^,@, etc.");
  280. valid = false;
  281. }
  282.  
  283. return valid;
  284. }
  285.  
  286. private void Login()
  287. {
  288. int tempPlayer = -1;
  289. //user will only be allowed to login after they have registered
  290. //if the ArrayList is empty it means no player profiles (objects) have been created yet
  291. if (playerList.isEmpty())
  292. {
  293. System.out.println("\n You need to be registered before you can login.");
  294. System.out.println(" Register by choosing R from the main menu.");
  295. System.out.println();
  296. }
  297. else
  298. {
  299. Scanner scan = new Scanner(System.in);
  300. System.out.println("\n Enter your username: ");
  301. String verification = scan.nextLine();
  302.  
  303. //loops through the array to check whether the username entered by the user (ignoring case)
  304. //is equal to any of the usernames in the player database
  305. for (int i=0; i < playerList.size(); i++)
  306. {
  307. if (verification.equalsIgnoreCase(playerList.get(i).getUsername()))
  308. {
  309. tempPlayer = i;
  310. break; //once a match has been found no need to run the whole loop (unique usernames)
  311. }
  312. }
  313.  
  314. //if true means no username was matched (tempPlayer was initialised as -1)
  315. while(tempPlayer == -1)
  316. {
  317. System.out.println("\n No such username exists.");
  318. System.out.println(" Please enter your username again, or type 'quit' to quit the login process: ");
  319. verification = scan.nextLine();
  320. //if user wants to quit
  321. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  322. {
  323. break; //exits the username verification loop
  324. }
  325. else
  326. {
  327. for (int i=0; i < playerList.size(); i++)
  328. {
  329. if (verification.equalsIgnoreCase(playerList.get(i).getUsername()))
  330. {
  331. tempPlayer = i;
  332. break; //exits the username verification loop
  333. }
  334. }
  335. }
  336. }
  337.  
  338. //need to repeat this check in the case the user wants to quit, in order to exit the whole method
  339. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  340. {
  341. return; //will exit method as user no longer wants to continue
  342. }
  343.  
  344. //if username is correct, we continue the verification process
  345. System.out.println("\n Enter your password: ");
  346. verification = scan.nextLine();
  347. while(!playerList.get(tempPlayer).getPassword().equals(verification))
  348. {
  349. System.out.println("\n You have entered a wrong password.");
  350. System.out.println(" Please enter your password again, or type 'quit' to quit the login process: ");
  351. verification = scan.nextLine();
  352. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  353. {
  354. break; //escapes this while loop
  355. }
  356. }
  357.  
  358. //need to repeat this check in the case the user wants to quit, in order to exit the whole method
  359. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  360. {
  361. return; //will exit method as user no longer wants to continue
  362. }
  363.  
  364. //if the interpreter reaches this point it means that the user has managed to log in successfuly
  365. currentPlayer = tempPlayer; //changes the new currently logged in Player
  366. System.out.println("\n Welcome back " + playerList.get(currentPlayer).getName() + "!");
  367. System.out.println(" Your total score is " + playerList.get(currentPlayer).getTotalScore() + ".");
  368. System.out.println();
  369. }
  370. }
  371.  
  372. private void Play()
  373. {
  374. //if condition true then that means that no user is logged in
  375. if (currentPlayer == -1)
  376. {
  377. System.out.println("\n You need to be logged in before you can play the game.");
  378. System.out.println(" Login by choosing L from the main menu.");
  379. System.out.println();
  380. }
  381. else
  382. {
  383. quit = true; //guit the game, as required by the instructions for this part of the project
  384. }
  385. }
  386. }
Add Comment
Please, Sign In to add comment