Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class VocabBuilder {
- //boolean value used as condition in runMenu() to quit the whole game if true (will not run the main menu anymore)
- private boolean quit;
- //holds the index value in the array for the current user logged in
- //it is initalised as -1, which means no user has logged in yet
- private int currentPlayer = -1;
- //initialises the ArrayList that will hold player information (the player objects)
- //at this stage we don't need to save this ArrayList to a file as there is only one player playing the game
- ArrayList<Player> playerList = new ArrayList<Player>();
- public static void main(String[] args){
- VocabBuilder menu = new VocabBuilder(); //constructs the Main Menu
- menu.runMenu(); //runs the main Menu
- }
- //This method is used to run the menu
- private void runMenu(){
- while (!quit){ //Only runs the menu if the user has not selected to quit
- printHeader();
- printMenu();
- char choice = getUserChoice();
- performMenu(choice);
- }
- }
- //This method is used to print the top of the main menu
- private void printHeader() {
- if (currentPlayer==-1)
- System.out.println(" Welcome to the Word Game");
- else
- System.out.println(" Welcome to the Word Game " + playerList.get(currentPlayer).getName() + "!");
- }
- //This method is used to print the content of the main menu
- private static void printMenu() {
- System.out.println(" Login (L)");
- System.out.println(" Register (R)");
- System.out.println(" About (A)");
- System.out.println(" Play the Game (P)");
- System.out.println(" Show the Leader Board (B)");
- System.out.println(" Quit (Q)");
- System.out.print("\n Please choose an option: ");
- }
- //This method is used to validate and get the user's choice from the main menu
- private char getUserChoice(){
- Scanner scan = new Scanner(System.in);
- char selection = scan.next().charAt(0);
- char selectionUpper = Character.toUpperCase(selection);
- //checks for input error
- while ((selectionUpper!='L') && (selectionUpper!='R')&&(selectionUpper!='A') && (selectionUpper!='P')&&(selectionUpper!='B') && (selectionUpper!='Q'))
- {
- System.out.println("\n You have entered an incorrect menu choice.");
- System.out.print(" Please enter one of the choices given: ");
- selection = scan.next().charAt(0);
- selectionUpper = Character.toUpperCase(selection);
- }
- return selectionUpper; //returns a correct menu choise as an upper case letter
- }
- private void performMenu(char choice) {
- switch (choice)
- {
- case 'L':
- Login();
- break;
- case 'R':
- Register();
- break;
- case 'A':
- printAbout();
- break;
- case 'P':
- Play();
- break;
- case 'B':
- quit = true;
- break;
- case 'Q':
- quit = true;
- break;
- }
- }
- private static void printAbout(){
- System.out.println("\n Instructions on how to play the Word game:");
- System.out.println(" This game is a vocabulary builder, which aims to help you in learning the definition of certain words.");
- 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");
- System.out.println(" of possible options. Only one word will be the correct answer each time. After selecting a word, the");
- System.out.println(" game will let you know whether you have chosen the correct synonym, and if not, will show the correct answer.");
- System.out.println(" Each game is comprised of 10 guessing stages, and during each stage you will be shown your current score.");
- System.out.println(" At the end of the game, you can compare your final score with those of other players in the leaderboard.");
- System.out.println();
- }
- private void Register() {
- //creates temporary variables to hold the player's information during registration
- String tempName, tempSurname, tempUsername, tempPassword;
- //each variable is assigned values by calling the methods responsible for getting the user's details
- tempName = setName();
- tempSurname = setSurname();
- tempUsername = setUsername();
- tempPassword = setPassword();
- System.out.println("\n You have successfully registered a new player with username " + tempUsername);
- System.out.println(" You need to log in to play the game");
- //creates a new player object using the player's details
- Player newPlayer = new Player(tempName,tempSurname,tempUsername,tempPassword, 0, 0);
- //adds the new player's details to the array list
- playerList.add(newPlayer);
- }
- private String setName() {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Enter your first name: ");
- String name = scan.nextLine();
- while (!nameValidation(name)) {
- System.out.print("\n Please enter your first name again: ");
- name = scan.nextLine();
- }
- return name;
- }
- private String setSurname() {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Enter your last name: ");
- String surname = scan.nextLine();
- while (!nameValidation(surname)) {
- System.out.print("\n Please enter your last name again: ");
- surname = scan.nextLine();
- }
- return surname;
- }
- private boolean nameValidation(String valName){
- boolean valid = true;
- String numbers = "(.*[0-9].*)";
- String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
- if (valName.length() <2 ) {
- System.out.print("\n Your name must be at least 2 characters long.");
- valid = false;
- }
- if (valName.matches(numbers)) {
- System.out.print("\n Your name can not contain any numbers.");
- valid = false;
- }
- if (valName.matches(specialChars)) {
- System.out.print("\n Your name can not contain any special characters.");
- valid = false;
- }
- return valid;
- }
- private String setUsername() {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Guidelines for choosing your username: ");
- System.out.print("\n 1) Your player username needs to be at least 3 characters long and no more than 12.");
- System.out.print("\n 2) The first character can not be a number.");
- System.out.print("\n 3) The username can not contain any special characters. ");
- System.out.print("\n Enter your desired player username: ");
- String username = scan.nextLine();
- while (!usernameValidation(username)) {
- System.out.print("\n Please enter a new username: ");
- username = scan.nextLine();
- }
- return username;
- }
- private boolean usernameValidation(String valUsername) {
- boolean valid = true;
- String numbers = "(.*[0-9].*)";
- String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
- if ((valUsername.length() < 3) || (valUsername.length() > 12)) {
- System.out.print("\n Your username is too short or too long.");
- valid = false;
- }
- if ((valUsername.substring(0,1)).matches(numbers)) {
- System.out.print("\n Your username should not begin with a number.");
- valid = false;
- }
- if (valUsername.matches(specialChars)) {
- System.out.print("\n Your username should not contain any special characters.");
- valid = false;
- }
- for (int i=0; i < playerList.size(); i++){
- if (valUsername.equals(playerList.get(i).getUsername())) {
- System.out.print("\n This username is already in use.");
- valid = false;
- break; //usernames will be unique, so we know there won't be another one, so no need to continue the loop
- }
- }
- return valid;
- }
- private String setPassword() {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Guidelines for choosing your password: ");
- System.out.print("\n 1) Your password needs to be at least 8 characters long but no more than 15.");
- System.out.print("\n 2) Your password should contain at least one uppercase letter");
- System.out.print("\n 3) Your password should contain at least one special character. ");
- System.out.print("\n Enter your desired player password: ");
- String password = scan.nextLine();
- while (!passwordValidation(password)) {
- System.out.print("\n Please enter a new password: ");
- password = scan.nextLine();
- }
- return password;
- }
- private boolean passwordValidation(String valPassword){
- boolean valid = true;
- String upperCase = "(.*[A-Z].*)";
- String specialChars = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
- if ((valPassword.length() < 8) || (valPassword.length() > 15)) {
- System.out.print("\n Your password is too short or too long.");
- valid = false;
- }
- if (!valPassword.matches(upperCase)) {
- System.out.print("\n Your password should contain at least one uppercase letter.");
- valid = false;
- }
- if (!valPassword.matches(specialChars)) {
- System.out.print("\n Your password should contain at least one special character.");
- System.out.print("\n Tip: Some of the characters you can use are #,$,%,*,^,@, etc.");
- valid = false;
- }
- return valid;
- }
- private void Login() {
- int tempPlayer = -1;
- if (playerList.isEmpty()){ //user will only be allowed to login after they have registered
- //if the ArrayList is empty it means no player profiles (objects) have been created yet
- System.out.println("\n You need to be registered before you can login.");
- System.out.println(" Register by choosing R from the main menu.");
- System.out.println();
- }
- else {
- Scanner scan = new Scanner(System.in);
- System.out.println("\n Enter your username: ");
- String verification = scan.nextLine();
- //loops through the array to check whether the username entered by the user (ignoring case)
- //is equal to any of the usernames in the player database
- for (int i=0; i < playerList.size(); i++){
- if (verification.equalsIgnoreCase(playerList.get(i).getUsername())) {
- tempPlayer = i;
- break; //once a match has been found no need to run the whole loop (unique usernames)
- }
- }
- while(tempPlayer == -1) { //if true means no username was matched (tempPlayer was initialised as -1)
- System.out.println("\n No such username exists.");
- System.out.println(" Please enter your username again, or type 'quit' to quit the login process: ");
- verification = scan.nextLine();
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'")) //if user wants to quit
- break; //escapes this while loop
- else {
- for (int i=0; i < playerList.size(); i++){
- if (verification.equalsIgnoreCase(playerList.get(i).getUsername())) {
- tempPlayer = i;
- break; //exits the loop
- }
- }
- }
- }
- //need to repeat this check in the case the user wants to quit, in order to exit the whole method
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
- return; //will exit method as user no longer wants to continue
- //if username is correct, we continue the verification process
- System.out.println("\n Enter your password: ");
- verification = scan.nextLine();
- while(!playerList.get(tempPlayer).getPassword().equals(verification)) {
- System.out.println("\n You have entered a wrong password.");
- System.out.println(" Please enter your password again, or type 'quit' to quit the login process: ");
- verification = scan.nextLine();
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'")) //if user wants to quit
- break; //escapes this while loop
- }
- //need to repeat this check in the case the user wants to quit, in order to exit the whole method
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
- return; //will exit method as user no longer wants to continue
- //if the interpreter reaches this point it means that the user has managed to log in successfuly
- currentPlayer = tempPlayer; //changes the new currently logged in Player
- System.out.println("\n Welcome back " + playerList.get(currentPlayer).getName() + "!");
- System.out.println(" Your total score is " + playerList.get(currentPlayer).getTotalScore() + ".");
- System.out.println();
- }
- }
- private void Play() {
- //if condition true then that means that no user is logged in
- if (currentPlayer == -1) {
- System.out.println("\n You need to be logged in before you can play the game.");
- System.out.println(" Login by choosing L from the main menu.");
- System.out.println();
- }
- else {
- quit = true; //guit the game, as required by the instructions for this part of the project
- }
- }
- }
Add Comment
Please, Sign In to add comment