Advertisement
Guest User

Game Class

a guest
Dec 3rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class Game {
  5. private static String username;
  6. private static String password;
  7. private static User currentUser;
  8. char selection = 'a';
  9.  
  10. public static void main(String[] args)
  11. {
  12. printMainMenu();
  13. }
  14.  
  15. public Game()
  16. {}
  17.  
  18. public static void printMainMenu() {
  19. Scanner scan = new Scanner(System.in);
  20. printMenu();//prints Menu with different options
  21.  
  22. char selection = 'a';//initialises selection
  23. selection = scan.next().charAt(0);
  24. selection = Character.toUpperCase(selection);
  25.  
  26. switch(selection)
  27. {
  28. case 'L'://allows user to login, if registered
  29. login();
  30. printMainMenu();
  31. break;
  32. case 'R'://allows user to register
  33. register();
  34. //login();
  35. break;
  36. case 'A'://displays information about game
  37. printAbout();
  38. printMainMenu();
  39. break;
  40. case 'P':
  41. play();
  42. break;
  43. case 'B': //would normally display leader board
  44. System.out.println("\tNo content to display");
  45. System.exit(0);
  46. break;
  47. case 'Q':
  48. System.out.println("Goodbye! See you soon!\n");
  49. if (currentUser != null) {
  50. currentUser.saveData();
  51. }
  52. System.exit(0);
  53. break;
  54. default://if user typed any other character other than L,R,P,B,A,Q
  55. System.out.println("Invalid input");
  56. System.out.println("Try again: ");
  57. selection = scan.next().charAt(0);//scans new character for selection
  58. selection = Character.toUpperCase(selection);
  59. }
  60. }
  61.  
  62. public static void printMenu()//prints the menu
  63. {
  64. printMenuPoint(1);
  65. printMenuPoint(2);
  66. printMenuPoint(3);
  67. printMenuPoint(4);
  68. printMenuPoint(5);
  69. printMenuPoint(6);
  70. printMenuPoint(7);
  71. printMenuPoint(8);
  72.  
  73. }
  74. public static void printMenuPoint(int x)//prints each point on the menu
  75. {
  76. Scanner scan = new Scanner(System.in);
  77. switch(x)
  78. {
  79. case 1:
  80. System.out.println("Welcome to the Word Game");
  81. break;
  82. case 2:
  83. System.out.println("\t\tLogin (L)");
  84. break;
  85. case 3:
  86. System.out.println("\t\tRegister (R)");
  87. break;
  88. case 4:
  89. System.out.println("\t\tAbout (A)");
  90. break;
  91. case 5:
  92. System.out.println("\t\tPlay the Game (P)");
  93. break;
  94. case 6:
  95. System.out.println("\t\tShow the Leader Board (B)");
  96. break;
  97. case 7:
  98. System.out.println("\t\tQuit (Q)\n");
  99. break;
  100. case 8:
  101. System.out.println("Please choose an option: ");
  102. break;
  103.  
  104. }
  105. }
  106.  
  107. public static void register()//allows new players to register by invoking mehtods in User class and storing ingormation there
  108. {
  109. User usercaller = new User();
  110. usercaller.register();
  111. printMainMenu();
  112. }
  113.  
  114. public static boolean login()//checks if a player has registered during same run-->later will look through stored player list
  115. {
  116. Scanner scan = new Scanner(System.in);
  117. System.out.println("\n\tEnter your username: ");
  118. username=scan.nextLine();
  119.  
  120. System.out.println("\n\tEnter your password: ");
  121. password=scan.nextLine();
  122.  
  123. User loginUser = new User(username, password);
  124.  
  125. if (loginUser.isLoginCorrect()) {
  126. currentUser = loginUser;
  127. return true;
  128. } else {
  129. System.out.println("Incorrect credentials, try again: ");
  130. return false;
  131. }
  132. }
  133.  
  134. private static void play()//will check if player is logged in and then execute code for playing the game
  135. {
  136. if (currentUser != null) {
  137. System.out.println("\tWelcome to the game!\n");
  138.  
  139. PlayGame playGame = new PlayGame();
  140. String[] question = playGame.getRandomQuestion();
  141. playGame.printQuestionDetails(question);
  142.  
  143. System.out.println("Type in your answer between 1-4:");
  144.  
  145. Scanner scan = new Scanner(System.in);
  146. int choice = scan.nextInt();
  147. boolean isCorrect = playGame.checkAnswer(choice);
  148.  
  149. if (isCorrect) {
  150. System.out.println("Correct!");
  151. currentUser.increaseScore();
  152. System.out.println("Current Score: " + currentUser.getCurrentScore());
  153. } else {
  154. System.out.println("WRONG!");
  155. }
  156. currentUser.increaseTimesPlayed();
  157. printMainMenu();
  158. } else {
  159. System.out.println("\tYou must be logged in to play.\n");
  160. printMainMenu();
  161. }
  162. }
  163. private static void printAbout()//prints information about the game
  164. {
  165. System.out.println("This is a Vocabulary Builder game.");
  166. 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.");
  167. System.out.println("It’s a fun way to learn English and you can compete against your friends and compare your high scores!");
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement