Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class Game {
- private static String username;
- private static String password;
- private static User currentUser;
- char selection = 'a';
- public static void main(String[] args)
- {
- printMainMenu();
- }
- public Game()
- {}
- public static void printMainMenu() {
- Scanner scan = new Scanner(System.in);
- printMenu();//prints Menu with different options
- char selection = 'a';//initialises selection
- selection = scan.next().charAt(0);
- selection = Character.toUpperCase(selection);
- switch(selection)
- {
- case 'L'://allows user to login, if registered
- login();
- printMainMenu();
- break;
- case 'R'://allows user to register
- register();
- //login();
- break;
- case 'A'://displays information about game
- printAbout();
- printMainMenu();
- break;
- case 'P':
- play();
- break;
- case 'B': //would normally display leader board
- System.out.println("\tNo content to display");
- System.exit(0);
- break;
- case 'Q':
- System.out.println("Goodbye! See you soon!\n");
- if (currentUser != null) {
- currentUser.saveData();
- }
- System.exit(0);
- break;
- default://if user typed any other character other than L,R,P,B,A,Q
- System.out.println("Invalid input");
- System.out.println("Try again: ");
- selection = scan.next().charAt(0);//scans new character for selection
- selection = Character.toUpperCase(selection);
- }
- }
- public static void printMenu()//prints the menu
- {
- printMenuPoint(1);
- printMenuPoint(2);
- printMenuPoint(3);
- printMenuPoint(4);
- printMenuPoint(5);
- printMenuPoint(6);
- printMenuPoint(7);
- printMenuPoint(8);
- }
- 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\tQuit (Q)\n");
- break;
- case 8:
- 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 ingormation there
- {
- User usercaller = new User();
- usercaller.register();
- printMainMenu();
- }
- public static boolean login()//checks if a player has registered during same run-->later will look through stored player list
- {
- 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()//will check if player is logged in and then execute code for playing the game
- {
- if (currentUser != null) {
- System.out.println("\tWelcome to the game!\n");
- 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!");
- currentUser.increaseScore();
- System.out.println("Current Score: " + currentUser.getCurrentScore());
- } else {
- System.out.println("WRONG!");
- }
- currentUser.increaseTimesPlayed();
- printMainMenu();
- } 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