Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class game {
- public static void main(String[] args) throws IOException {
- Scanner input = new Scanner(System.in);
- BufferedReader questionReader = new BufferedReader(new FileReader("questions.txt"));
- //questionReader.readLine();
- System.out.println(questionReader.readLine());
- PrintWriter questionWriter = new PrintWriter(new BufferedWriter(new FileWriter("questions.txt")));
- BufferedReader scoresReader = new BufferedReader(new FileReader("scores.txt"));
- //scoresReader.readLine();
- System.out.println(scoresReader.readLine());
- PrintWriter scoresWriter = new PrintWriter(new BufferedWriter(new FileWriter("scores.txt")));
- admin database = new admin();
- leaderboard lead = new leaderboard();
- String option, attempt, question, answer, answer1, answer2, answer3, line;
- String username;
- int numCorrect, numAnswered;
- int tries = 2;
- String password = "hawks";
- while ((line = questionReader.readLine()) != null) {
- question = line;
- answer = questionReader.readLine();
- answer1 = questionReader.readLine();
- answer2 = questionReader.readLine();
- answer3 = questionReader.readLine();
- database.addQandA(question, answer, answer1, answer2, answer3);
- }
- while ((line = scoresReader.readLine()) != null) {
- username = line;
- numCorrect = Integer.parseInt(scoresReader.readLine());
- numAnswered = Integer.parseInt(scoresReader.readLine());
- lead.addLeaderboard(username, numCorrect, numAnswered);
- }
- while (true) {
- System.out.println("Welcome to my Trivia Game!\n");
- System.out.println("1) Play Game");
- System.out.println("2) Leaderboard");
- System.out.println("3) Administrator");
- System.out.println("4) exit");
- System.out.println("\nChoose an Option");
- option = input.nextLine();
- if (option.equals("1")) {//play game option.
- System.out.println("Do you wish to play?");
- option = input.nextLine();
- if (option.equalsIgnoreCase("yes")) {
- System.out.println("Enter your username: ");
- username = input.nextLine();
- }
- database.createGame();
- database.display2dArray();
- //starts the game
- } else if (option.equals("2")) {//leaderboard option
- while (true) {
- System.out.println("1) View top 10 players\n2) Search for a player score\n3) View entire leaderboard\n4) exit");
- option = input.nextLine();
- if (option.equals("1")) {
- } else if (option.equals("2")) {
- System.out.println(lead.displayPlayer());
- } else if (option.equals("3")) {
- } else if (option.equals("4")) {
- break;
- } else {
- System.out.println("Please choose an option available");
- }
- }
- } else if (option.equals("3")) {//admin option
- System.out.println("Please enter the password or type \"exit\" to leave: ");
- attempt = input.nextLine();
- if (!attempt.equals(password)) {
- while (!attempt.equals(password)) {
- tries--;
- System.out.println("Incorrect password. Try again.\nYou have: " + (tries + 1) + " attempts left.");
- attempt = input.nextLine();
- if(tries == 0){
- questionWriter.println();
- scoresWriter.println();
- questionWriter.println(database.displayArray());
- scoresWriter.println(lead.displayArray1());
- questionWriter.close();
- scoresWriter.close();
- System.exit(0);
- }
- }
- }
- tries = 3;
- while (true) {
- System.out.println("1) Add a question?");
- System.out.println("2) Remove a question?");
- System.out.println("3) View the database?");
- System.out.println("4) Exit");
- option = input.nextLine();
- if (option.equalsIgnoreCase("add") || option.equals("1")) {
- do {
- System.out.println("Enter your question: ");
- question = input.nextLine();
- System.out.println("Enter correct answer: ");
- answer = input.nextLine();
- System.out.println("Enter 3 incorrect answers: ");
- answer1 = input.nextLine();
- answer2 = input.nextLine();
- answer3 = input.nextLine();
- database.addQandA(question, answer, answer1, answer2, answer3);
- System.out.println("Do you want to enter new question?");
- option = input.nextLine();
- } while (option.equalsIgnoreCase("yes"));
- } else if (option.equalsIgnoreCase("view") || option.equals("3")) {
- System.out.println(database.displayArray());
- } else if (option.equalsIgnoreCase("remove") || option.equals("2")) {
- } else if (!(option.equals("4")) || !(option.equalsIgnoreCase("exit"))) {
- break;
- } else {
- }
- }
- } else if (option.equals("4") || option.equalsIgnoreCase("exit")) {
- System.out.println("Thanks for playing!");
- questionWriter.println();
- //scoresWriter.println();
- questionWriter.println(database.displayArray());
- // scoresWriter.println(lead.displayArray1());
- break;
- } else {//catch user error
- System.out.println("Please choose a number available");
- }
- }
- questionWriter.close();
- //scoresWriter.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement