Advertisement
Guest User

Untitled

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