Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Scanner;
- import java.util.Random;
- public class SarahRyuGame
- {
- public static void main(String[] arguments)
- {
- //declare main variables
- int choice=0;
- Scanner input = new Scanner(System.in);
- String[] usernames = new String[0];
- String[] passwords = new String[0];
- int[] scores = new int[0];
- String enteredUsername = "", enteredPassword = "", verifiedPassword ="";
- int recentScore;
- //main loop until choice = 4
- do
- {
- printMenu();
- choice = getChoice();
- if(choice == 1) //register - get username and password
- {
- System.out.println("\nREGISTER");
- //get username
- System.out.print("Enter username: ");
- enteredUsername = input.next();
- //check to see if username already exists
- while(findUser(enteredUsername, usernames) != -1)
- {
- System.out.print("Username already exists. Enter new username: ");
- enteredUsername = input.next();
- }
- //call method createPassword to get password and verify entered the same one twice.
- createPassword();
- //add the new user info to all arrays - creates a copy that is one larger than before
- usernames = addToArray(enteredUsername, usernames);
- passwords = addToArray(enteredPassword, passwords);
- scores = addToArray(0,scores);
- System.out.printf("%nWelcome %s. You have been registered.%n%n",enteredUsername);
- }
- else if(choice == 2) //login to account and play game
- {
- int currentUserIndex;
- if(usernames.length == 0)//no users yet, return to main menu
- {
- System.out.println("No account exists. Please register first.");
- }
- else //have registered so now can login with username and password
- {
- System.out.println("\nLOGIN");
- //get username
- System.out.print("Enter username: ");
- enteredUsername = input.next();
- currentUserIndex = findUser(enteredUsername, usernames);
- while(currentUserIndex == -1)
- {
- System.out.print("User does not exist.");
- System.out.print("Enter username: ");
- enteredUsername = input.next();
- currentUserIndex = findUser(enteredUsername, usernames);
- }
- //get password
- System.out.print("Enter password: ");
- enteredPassword = input.next();
- while(!passwords[currentUserIndex].equals(enteredPassword))
- {
- System.out.print("Invalid password. ");
- System.out.print("Enter password: ");
- enteredPassword = input.next();
- }
- //success!
- System.out.printf("%nWelcome %s. You have successfully logged in.%n%n",usernames[currentUserIndex]);
- System.out.println("Now let's play!");
- //play game, get score from game
- recentScore = playGame(usernames[currentUserIndex]);
- //TASK 10! update score in array
- scores[currentUserIndex] = recentScore;
- }
- }
- else if(choice == 3) //display scoreboard
- {
- displayScoreboard(usernames, scores);
- }
- }while(choice != 4);
- }//end main
- //methods
- public static String createPassword()
- {
- Scanner input = new Scanner(System.in);
- String enteredPassword, verifiedPassword;
- do
- {
- System.out.print("Enter password: ");
- enteredPassword = input.next();
- System.out.print("Reenter password: ");
- verifiedPassword = input.next();
- if(!enteredPassword.equals(verifiedPassword))
- {
- System.out.println("Your passwords do not match! Please try again. ");
- }
- }while(!enteredPassword.equals(verifiedPassword));
- return verifiedPassword;
- }
- //add comments here...addToArray() does this...
- public static String[] addToArray(String newWord, String[] oldArray)
- {
- //create new array that is 1 bigger than old array
- String[] newArray = new String[oldArray.length+1];
- //copy everything from oldArray to newArray
- for(int i = 0; i < oldArray.length; i++)
- {
- newArray[i] = oldArray[i];
- }
- //in the last spot, add the newWord
- newArray[newArray.length-1] = newWord;
- //now return the pointer to the newArray
- return newArray;
- }
- //add comments here...addToArray() does this...
- public static int[] addToArray(int newNum, int[] oldArray)
- {
- //create new array that is 1 bigger than old array
- int[] newArray = new int[oldArray.length+1];
- //copy everything from oldArray to newArray
- for(int i = 0; i < oldArray.length; i++)
- {
- newArray[i] = oldArray[i];
- }
- //in the last spot, add the newWord
- newArray[newArray.length-1] = newNum;
- //now return the pointer to the newArray
- return newArray;
- }
- //add comments here...displayScoreboard() does this...
- public static void displayScoreboard(String[] usernames, int[] scores)
- {
- System.out.println("USER SCORE");
- for(int i = 0; i < usernames.length; i++)
- {
- System.out.printf("%s: %d%n", usernames[i], scores[i]);
- }
- }
- //add comments here...getChoice() does this...
- public static int getChoice()
- {
- Scanner input = new Scanner(System.in);
- System.out.print("Enter choice: ");
- int choice = 0;
- try
- {
- choice = input.nextInt();
- while (choice < 1 || choice > 4)
- {
- System.out.println("Invalid choice.");
- System.out.print("Enter choice: ");
- choice = input.nextInt();
- }
- }
- //add checks for invalid input - try catch? and check for 0-3 valid or not
- catch(InputMismatchException e)
- {
- }
- return choice;
- }
- //add comments here...findUser() does this...
- public static int findUser(String newUsername, String[] usernames)
- {
- //check to see if newUsername already exists in the usernames array
- for(int i = 0; i < usernames.length; i++)
- {
- //if exists, return index of user in the array
- if(newUsername.equals(usernames[i]))
- return i;
- }
- //if never found, return -1
- return -1;
- }
- //add comments here...playGame() does this...
- public static int playGame(String name){
- //create the objects
- Scanner input = new Scanner(System.in); //used for input
- Random randomNumbers = new Random(); //used to generate a random number
- //declare the variables
- int theNumber = randomNumbers.nextInt(100) + 1; //pick a random integer from 1 to 100
- int numberGuesses = 0; //keep track of how many guessed
- boolean correctGuess = false; //has the number been guessed yet?
- int guess; //the guess by user
- System.out.printf("%nWelcome %s to Professor Steck's Number Guessing Game.%n", name);
- System.out.println("I am thinking of a number between 1 and 100...");
- //cause a short delay while "thinking", 1000 is one second
- try
- {
- Thread.sleep(2000);
- }
- catch(InterruptedException ex)
- {
- Thread.currentThread().interrupt();
- }
- //ask for a guess until it is correct
- while(correctGuess == false) //could also write this as !correctGuess
- {
- //get a guess from the user
- System.out.printf("%nEnter your guess: ");
- guess = input.nextInt();
- System.out.println();
- numberGuesses++; //increment the number of guesses by 1
- //determine if the number guessed is correct
- if(guess == theNumber)
- {
- System.out.print("Yes! You are correct! ");
- System.out.printf("It took you %d guesses. %n%n", numberGuesses);
- correctGuess = true;
- }
- else
- {
- //if not correct, determine whether guess was too high or too low
- if(guess > theNumber) //guess too high
- {
- System.out.print("Sorry, you are not correct.");
- System.out.printf(" Your guess was too high. %n");
- }
- else //guess too low
- {
- System.out.print("Sorry, you are not correct.");
- System.out.printf(" Your guess was too low. %n");
- }
- }
- } //end while loop
- return numberGuesses;
- }
- //add comments here...printMenu() does this...
- public static void printMenu()
- {
- System.out.println("\nMAIN MENU");
- System.out.println("1. Register");
- System.out.println("2. Login");
- System.out.println("3. Display scoreboard");
- System.out.println("4. Exit");
- }
- }//end class
Add Comment
Please, Sign In to add comment