Advertisement
Guest User

Untitled

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