Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. public class GameController {
  6. private static String username;
  7. private static String password;
  8. private static User currentUser;
  9. private static char selection = 'a';
  10.  
  11. public static void main(String[] args)
  12. {
  13. printMainMenu(); //prints the main menu with all the options
  14. }
  15.  
  16. public void Game()
  17. {}
  18.  
  19. public static void printMainMenu()
  20. {
  21. boolean inputError=true;
  22. Scanner scan = new Scanner(System.in);
  23. while (inputError)
  24. {
  25. printMenu();//prints Menu with different options
  26. selection = scan.next().charAt(0);
  27. selection = Character.toUpperCase(selection);//converts selection to upper case for code simplicity
  28. if ((selection == 'L')||(selection == 'R')||(selection =='A')||(selection == 'P')||(selection == 'B')||(selection == 'Q')||(selection == 'O'))
  29. {
  30. switch(selection)
  31. {
  32. case 'L'://allows user to login, if registered. Only one user can be logged in at a given time.
  33. login();
  34. printMainMenu();
  35. break;
  36. case 'R'://allows user to register
  37. register();
  38. break;
  39. case 'A'://displays information about game
  40. printAbout();
  41. printMainMenu();
  42. break;
  43. case 'P'://allows user to play one game at a time (of 10 questions)
  44. play();
  45. break;
  46. case 'B': //will display leader board
  47. System.out.println("\tNo content to display");
  48. printMainMenu();
  49. break;
  50. case 'O'://allows user to logout, if logged in
  51. if (currentUser !=null)
  52. {
  53. currentUser=null;
  54. System.out.println("You have logged out!");
  55. }
  56. printMainMenu();
  57. break;
  58. case 'Q': //allows user to quit and saves information
  59. System.out.println("Goodbye! See you soon!\n");
  60. if (currentUser != null)
  61. {
  62. currentUser.saveData();
  63. }
  64. System.exit(0);
  65. break;
  66. }
  67. inputError=false;
  68. }
  69. else //if user typed any other character other than L,R,P,B,A,Q,O
  70. System.out.println("Invalid input!");
  71. }
  72. }
  73.  
  74. public static void printMenu()//prints the menu
  75. {
  76. printMenuPoint(1);
  77. printMenuPoint(2);
  78. printMenuPoint(3);
  79. printMenuPoint(4);
  80. printMenuPoint(5);
  81. printMenuPoint(6);
  82. printMenuPoint(7);
  83. printMenuPoint(8);
  84. printMenuPoint(9);
  85.  
  86. }
  87. public static void printMenuPoint(int x)//prints each point on the menu
  88. {
  89. Scanner scan = new Scanner(System.in);
  90. switch(x)
  91. {
  92. case 1:
  93. System.out.println("Welcome to the Word Game");
  94. break;
  95. case 2:
  96. System.out.println("\t\tLogin (L)");
  97. break;
  98. case 3:
  99. System.out.println("\t\tRegister (R)");
  100. break;
  101. case 4:
  102. System.out.println("\t\tAbout (A)");
  103. break;
  104. case 5:
  105. System.out.println("\t\tPlay the Game (P)");
  106. break;
  107. case 6:
  108. System.out.println("\t\tShow the Leader Board (B)");
  109. break;
  110. case 7:
  111. System.out.println("\t\tLogout (if already logged in) (O)\n");
  112. break;
  113. case 8:
  114. System.out.println("\t\tQuit (Q)\n");
  115. break;
  116. case 9:
  117. System.out.println("Please choose an option: ");
  118. break;
  119.  
  120. }
  121. }
  122.  
  123. public static void register()//allows new players to register by invoking mehtods in User class and storing information there
  124. {
  125. User usercaller = new User();
  126. usercaller.register();
  127. printMainMenu();
  128. }
  129.  
  130. public static boolean login()//checks if a player has registered and validates credentials. Only one player can be logged in at a given time.
  131. {
  132. while (currentUser != null)
  133. {
  134. System.out.println("\nUser already logged in!");
  135. printMainMenu();
  136. }
  137.  
  138. Scanner scan = new Scanner(System.in);
  139. System.out.println("\n\tEnter your username: ");
  140. username=scan.nextLine();
  141.  
  142. System.out.println("\n\tEnter your password: ");
  143. password=scan.nextLine();
  144.  
  145. User loginUser = new User(username, password);
  146.  
  147. if (loginUser.isLoginCorrect())
  148. {
  149. currentUser = loginUser;
  150. return true;
  151. } else
  152. {
  153. System.out.println("Incorrect credentials, try again: ");
  154. return false;
  155. }
  156. }
  157.  
  158. private static void play()//Allows user to play if he is logged in.
  159. //Displays question and possible answers and keeps track of current score
  160. {
  161. if (currentUser != null)
  162. {
  163. System.out.println("\tWelcome to the game!\n");
  164. for (int i = 0; i < 10; i++)
  165. {
  166. PlayGame playGame = new PlayGame();
  167. String[] question = playGame.getRandomQuestion();
  168. playGame.printQuestionDetails(question);
  169.  
  170. System.out.println("Type in your answer between 1-4:");
  171.  
  172. Scanner scan = new Scanner(System.in);
  173. int choice = scan.nextInt();
  174. boolean isCorrect = playGame.checkAnswer(choice);
  175.  
  176. if (isCorrect)
  177. {
  178. System.out.println("Correct!\n");
  179. System.out.println (playGame.Feedback());//gives feedback on question
  180. currentUser.increaseScore();//increments the current score
  181. currentUser.printResults();
  182. }
  183. else if (choice==5)
  184. {
  185. currentUser.increaseQuestionsSkipped();//increments questions skipped
  186. currentUser.printResults();
  187. }
  188. else
  189. {
  190. System.out.println("WRONG!");
  191. currentUser.increaseWrongAnswers();//increments wrong answers
  192. currentUser.printResults();
  193. }
  194. currentUser.increaseTimesPlayed();//increases times played (using counter)
  195. }
  196. System.out.println("Game over!\n");
  197. currentUser.printResults();
  198. currentUser.reset();
  199. printMainMenu();//returns to main menu where user can choose to play another game
  200. }
  201. else
  202. {
  203. System.out.println("\tYou must be logged in to play.\n");
  204. printMainMenu();
  205. }
  206. }
  207. private static void printAbout()//prints information about the game
  208. {
  209. System.out.println("This is a Vocabulary Builder game.");
  210. System.out.println("The purpose of this game is to help players learn the definition of a word by selection a synonym from a list of words offered to them.");
  211. System.out.println("It’s a fun way to learn English and you can compete against your friends and compare your high scores!");
  212. }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement