Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class GameController {
- private static String username;
- private static String password;
- private static User currentUser;
- private static char selection = 'a';
- public static void main(String[] args)
- {
- printMainMenu(); //prints the main menu with all the options
- }
- public void Game()
- {}
- public static void printMainMenu()
- {
- boolean inputError=true;
- Scanner scan = new Scanner(System.in);
- while (inputError)
- {
- printMenu();//prints Menu with different options
- selection = scan.next().charAt(0);
- selection = Character.toUpperCase(selection);//converts selection to upper case for code simplicity
- if ((selection == 'L')||(selection == 'R')||(selection =='A')||(selection == 'P')||(selection == 'B')||(selection == 'Q')||(selection == 'O'))
- {
- switch(selection)
- {
- case 'L'://allows user to login, if registered. Only one user can be logged in at a given time.
- login();
- printMainMenu();
- break;
- case 'R'://allows user to register
- register();
- break;
- case 'A'://displays information about game
- printAbout();
- printMainMenu();
- break;
- case 'P'://allows user to play one game at a time (of 10 questions)
- play();
- break;
- case 'B': //will display leader board
- System.out.println("\tNo content to display");
- printMainMenu();
- break;
- case 'O'://allows user to logout, if logged in
- if (currentUser !=null)
- {
- currentUser=null;
- System.out.println("You have logged out!");
- }
- printMainMenu();
- break;
- case 'Q': //allows user to quit and saves information
- System.out.println("Goodbye! See you soon!\n");
- if (currentUser != null)
- {
- currentUser.saveData();
- }
- System.exit(0);
- break;
- }
- inputError=false;
- }
- else //if user typed any other character other than L,R,P,B,A,Q,O
- System.out.println("Invalid input!");
- }
- }
- public static void printMenu()//prints the menu
- {
- printMenuPoint(1);
- printMenuPoint(2);
- printMenuPoint(3);
- printMenuPoint(4);
- printMenuPoint(5);
- printMenuPoint(6);
- printMenuPoint(7);
- printMenuPoint(8);
- printMenuPoint(9);
- }
- public static void printMenuPoint(int x)//prints each point on the menu
- {
- Scanner scan = new Scanner(System.in);
- switch(x)
- {
- case 1:
- System.out.println("Welcome to the Word Game");
- break;
- case 2:
- System.out.println("\t\tLogin (L)");
- break;
- case 3:
- System.out.println("\t\tRegister (R)");
- break;
- case 4:
- System.out.println("\t\tAbout (A)");
- break;
- case 5:
- System.out.println("\t\tPlay the Game (P)");
- break;
- case 6:
- System.out.println("\t\tShow the Leader Board (B)");
- break;
- case 7:
- System.out.println("\t\tLogout (if already logged in) (O)\n");
- break;
- case 8:
- System.out.println("\t\tQuit (Q)\n");
- break;
- case 9:
- System.out.println("Please choose an option: ");
- break;
- }
- }
- public static void register()//allows new players to register by invoking mehtods in User class and storing information there
- {
- User usercaller = new User();
- usercaller.register();
- printMainMenu();
- }
- public static boolean login()//checks if a player has registered and validates credentials. Only one player can be logged in at a given time.
- {
- while (currentUser != null)
- {
- System.out.println("\nUser already logged in!");
- printMainMenu();
- }
- Scanner scan = new Scanner(System.in);
- System.out.println("\n\tEnter your username: ");
- username=scan.nextLine();
- System.out.println("\n\tEnter your password: ");
- password=scan.nextLine();
- User loginUser = new User(username, password);
- if (loginUser.isLoginCorrect())
- {
- currentUser = loginUser;
- return true;
- } else
- {
- System.out.println("Incorrect credentials, try again: ");
- return false;
- }
- }
- private static void play()//Allows user to play if he is logged in.
- //Displays question and possible answers and keeps track of current score
- {
- if (currentUser != null)
- {
- System.out.println("\tWelcome to the game!\n");
- for (int i = 0; i < 10; i++)
- {
- PlayGame playGame = new PlayGame();
- String[] question = playGame.getRandomQuestion();
- playGame.printQuestionDetails(question);
- System.out.println("Type in your answer between 1-4:");
- Scanner scan = new Scanner(System.in);
- int choice = scan.nextInt();
- boolean isCorrect = playGame.checkAnswer(choice);
- if (isCorrect)
- {
- System.out.println("Correct!\n");
- System.out.println (playGame.Feedback());//gives feedback on question
- currentUser.increaseScore();//increments the current score
- currentUser.printResults();
- }
- else if (choice==5)
- {
- currentUser.increaseQuestionsSkipped();//increments questions skipped
- currentUser.printResults();
- }
- else
- {
- System.out.println("WRONG!");
- currentUser.increaseWrongAnswers();//increments wrong answers
- currentUser.printResults();
- }
- currentUser.increaseTimesPlayed();//increases times played (using counter)
- }
- System.out.println("Game over!\n");
- currentUser.printResults();
- currentUser.reset();
- printMainMenu();//returns to main menu where user can choose to play another game
- }
- else
- {
- System.out.println("\tYou must be logged in to play.\n");
- printMainMenu();
- }
- }
- private static void printAbout()//prints information about the game
- {
- System.out.println("This is a Vocabulary Builder game.");
- 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.");
- System.out.println("Itβs a fun way to learn English and you can compete against your friends and compare your high scores!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement