Guest User

Untitled

a guest
Oct 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4.  
  5. public class SarahRyuGame
  6. {
  7. public static void main(String[] arguments)
  8. {
  9. //declare main variables
  10. int choice=0;
  11. Scanner input = new Scanner(System.in);
  12. String[] usernames = new String[0];
  13. String[] passwords = new String[0];
  14. int[] scores = new int[0];
  15. String enteredUsername = "", enteredPassword = "", verifiedPassword ="";
  16. int recentScore;
  17.  
  18.  
  19.  
  20. //main loop until choice = 4
  21. do
  22. {
  23. printMenu();
  24. choice = getChoice();
  25.  
  26. if(choice == 1) //register - get username and password
  27. {
  28. System.out.println("\nREGISTER");
  29.  
  30. //get username
  31. System.out.print("Enter username: ");
  32. enteredUsername = input.next();
  33.  
  34. //check to see if username already exists
  35. while(findUser(enteredUsername, usernames) != -1)
  36. {
  37. System.out.print("Username already exists. Enter new username: ");
  38. enteredUsername = input.next();
  39. }
  40.  
  41. //call method createPassword to get password and verify entered the same one twice.
  42. createPassword();
  43.  
  44. //add the new user info to all arrays - creates a copy that is one larger than before
  45. usernames = addToArray(enteredUsername, usernames);
  46. passwords = addToArray(enteredPassword, passwords);
  47. scores = addToArray(0,scores);
  48.  
  49. System.out.printf("%nWelcome %s. You have been registered.%n%n",enteredUsername);
  50.  
  51. }
  52. else if(choice == 2) //login to account and play game
  53. {
  54. int currentUserIndex;
  55.  
  56. if(usernames.length == 0)//no users yet, return to main menu
  57. {
  58. System.out.println("No account exists. Please register first.");
  59. }
  60. else //have registered so now can login with username and password
  61. {
  62. System.out.println("\nLOGIN");
  63.  
  64. //get username
  65. System.out.print("Enter username: ");
  66. enteredUsername = input.next();
  67. currentUserIndex = findUser(enteredUsername, usernames);
  68. while(currentUserIndex == -1)
  69. {
  70. System.out.print("User does not exist.");
  71. System.out.print("Enter username: ");
  72. enteredUsername = input.next();
  73. currentUserIndex = findUser(enteredUsername, usernames);
  74. }
  75.  
  76. //get password
  77. System.out.print("Enter password: ");
  78. enteredPassword = input.next();
  79. while(!passwords[currentUserIndex].equals(enteredPassword))
  80. {
  81. System.out.print("Invalid password. ");
  82. System.out.print("Enter password: ");
  83. enteredPassword = input.next();
  84. }
  85.  
  86. //success!
  87. System.out.printf("%nWelcome %s. You have successfully logged in.%n%n",usernames[currentUserIndex]);
  88. System.out.println("Now let's play!");
  89.  
  90. //play game, get score from game
  91. recentScore = playGame(usernames[currentUserIndex]);
  92.  
  93. //TASK 10! update score in array
  94. scores[currentUserIndex] = recentScore;
  95.  
  96. }
  97. }
  98. else if(choice == 3) //display scoreboard
  99. {
  100. displayScoreboard(usernames, scores);
  101. }
  102.  
  103. }while(choice != 4);
  104.  
  105. }//end main
  106.  
  107.  
  108. //methods
  109.  
  110. public static String createPassword()
  111. {
  112. Scanner input = new Scanner(System.in);
  113. String enteredPassword, verifiedPassword;
  114. do
  115. {
  116. System.out.print("Enter password: ");
  117. enteredPassword = input.next();
  118. System.out.print("Reenter password: ");
  119. verifiedPassword = input.next();
  120. if(!enteredPassword.equals(verifiedPassword))
  121. {
  122. System.out.println("Your passwords do not match! Please try again. ");
  123. }
  124. }while(!enteredPassword.equals(verifiedPassword));
  125. return verifiedPassword;
  126. }
  127. //add comments here...addToArray() does this...
  128. public static String[] addToArray(String newWord, String[] oldArray)
  129. {
  130. //create new array that is 1 bigger than old array
  131. String[] newArray = new String[oldArray.length+1];
  132.  
  133. //copy everything from oldArray to newArray
  134. for(int i = 0; i < oldArray.length; i++)
  135. {
  136. newArray[i] = oldArray[i];
  137. }
  138.  
  139. //in the last spot, add the newWord
  140. newArray[newArray.length-1] = newWord;
  141.  
  142. //now return the pointer to the newArray
  143. return newArray;
  144. }
  145.  
  146. //add comments here...addToArray() does this...
  147. public static int[] addToArray(int newNum, int[] oldArray)
  148. {
  149. //create new array that is 1 bigger than old array
  150. int[] newArray = new int[oldArray.length+1];
  151.  
  152. //copy everything from oldArray to newArray
  153. for(int i = 0; i < oldArray.length; i++)
  154. {
  155. newArray[i] = oldArray[i];
  156. }
  157.  
  158. //in the last spot, add the newWord
  159. newArray[newArray.length-1] = newNum;
  160.  
  161. //now return the pointer to the newArray
  162. return newArray;
  163. }
  164.  
  165. //add comments here...displayScoreboard() does this...
  166. public static void displayScoreboard(String[] usernames, int[] scores)
  167. {
  168. System.out.println("USER SCORE");
  169. for(int i = 0; i < usernames.length; i++)
  170. {
  171. System.out.printf("%s: %d%n", usernames[i], scores[i]);
  172. }
  173. }
  174.  
  175. //add comments here...getChoice() does this...
  176. public static int getChoice()
  177. {
  178. Scanner input = new Scanner(System.in);
  179. System.out.print("Enter choice: ");
  180. int choice = 0;
  181. try
  182. {
  183. choice = input.nextInt();
  184. while (choice < 1 || choice > 4)
  185. {
  186. System.out.println("Invalid choice.");
  187. System.out.print("Enter choice: ");
  188. choice = input.nextInt();
  189. }
  190. }
  191.  
  192. //add checks for invalid input - try catch? and check for 0-3 valid or not
  193. catch(InputMismatchException e)
  194. {
  195.  
  196.  
  197. }
  198. return choice;
  199.  
  200. }
  201.  
  202. //add comments here...findUser() does this...
  203. public static int findUser(String newUsername, String[] usernames)
  204. {
  205. //check to see if newUsername already exists in the usernames array
  206. for(int i = 0; i < usernames.length; i++)
  207. {
  208. //if exists, return index of user in the array
  209. if(newUsername.equals(usernames[i]))
  210. return i;
  211. }
  212.  
  213. //if never found, return -1
  214. return -1;
  215. }
  216.  
  217. //add comments here...playGame() does this...
  218. public static int playGame(String name){
  219. //create the objects
  220. Scanner input = new Scanner(System.in); //used for input
  221. Random randomNumbers = new Random(); //used to generate a random number
  222.  
  223. //declare the variables
  224. int theNumber = randomNumbers.nextInt(100) + 1; //pick a random integer from 1 to 100
  225. int numberGuesses = 0; //keep track of how many guessed
  226. boolean correctGuess = false; //has the number been guessed yet?
  227. int guess; //the guess by user
  228.  
  229. System.out.printf("%nWelcome %s to Professor Steck's Number Guessing Game.%n", name);
  230. System.out.println("I am thinking of a number between 1 and 100...");
  231.  
  232. //cause a short delay while "thinking", 1000 is one second
  233. try
  234. {
  235. Thread.sleep(2000);
  236. }
  237. catch(InterruptedException ex)
  238. {
  239. Thread.currentThread().interrupt();
  240. }
  241.  
  242.  
  243. //ask for a guess until it is correct
  244. while(correctGuess == false) //could also write this as !correctGuess
  245. {
  246. //get a guess from the user
  247. System.out.printf("%nEnter your guess: ");
  248. guess = input.nextInt();
  249. System.out.println();
  250. numberGuesses++; //increment the number of guesses by 1
  251.  
  252. //determine if the number guessed is correct
  253. if(guess == theNumber)
  254. {
  255. System.out.print("Yes! You are correct! ");
  256. System.out.printf("It took you %d guesses. %n%n", numberGuesses);
  257. correctGuess = true;
  258. }
  259. else
  260. {
  261. //if not correct, determine whether guess was too high or too low
  262. if(guess > theNumber) //guess too high
  263. {
  264. System.out.print("Sorry, you are not correct.");
  265. System.out.printf(" Your guess was too high. %n");
  266. }
  267. else //guess too low
  268. {
  269. System.out.print("Sorry, you are not correct.");
  270. System.out.printf(" Your guess was too low. %n");
  271. }
  272. }
  273. } //end while loop
  274. return numberGuesses;
  275. }
  276.  
  277. //add comments here...printMenu() does this...
  278. public static void printMenu()
  279. {
  280. System.out.println("\nMAIN MENU");
  281. System.out.println("1. Register");
  282. System.out.println("2. Login");
  283. System.out.println("3. Display scoreboard");
  284. System.out.println("4. Exit");
  285. }
  286.  
  287. }//end class
Add Comment
Please, Sign In to add comment