Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.54 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class game {
  6.   public static void main(String[] args) throws IOException {
  7.     Scanner input = new Scanner(System.in);
  8.  
  9.     BufferedReader questionReader = new BufferedReader(new FileReader("questions.txt"));
  10.     //questionReader.readLine();
  11.     System.out.println(questionReader.readLine());
  12.     PrintWriter questionWriter = new PrintWriter(new BufferedWriter(new FileWriter("questions.txt")));
  13.  
  14.     BufferedReader scoresReader = new BufferedReader(new FileReader("scores.txt"));
  15.     //scoresReader.readLine();
  16.     System.out.println(scoresReader.readLine());
  17.     PrintWriter scoresWriter = new PrintWriter(new BufferedWriter(new FileWriter("scores.txt")));
  18.  
  19.  
  20.     admin database = new admin();
  21.     leaderboard lead = new leaderboard();
  22.     String option, attempt, question, answer, answer1, answer2, answer3, line;
  23.     String username;
  24.     int numCorrect, numAnswered;
  25.     int tries = 2;
  26.     String password = "hawks";
  27.  
  28.     while ((line = questionReader.readLine()) != null) {
  29.       question = line;
  30.       answer = questionReader.readLine();
  31.       answer1 = questionReader.readLine();
  32.       answer2 = questionReader.readLine();
  33.       answer3 = questionReader.readLine();
  34.       database.addQandA(question, answer, answer1, answer2, answer3);
  35.     }
  36.  
  37.   while ((line = scoresReader.readLine()) != null) {
  38.       username = line;
  39.       numCorrect = Integer.parseInt(scoresReader.readLine());
  40.       numAnswered = Integer.parseInt(scoresReader.readLine());
  41.       lead.addLeaderboard(username, numCorrect, numAnswered);
  42.     }
  43.  
  44.  
  45.     while (true) {
  46.       System.out.println("Welcome to my Trivia Game!\n");
  47.       System.out.println("1) Play Game");
  48.       System.out.println("2) Leaderboard");
  49.       System.out.println("3) Administrator");
  50.       System.out.println("4) exit");
  51.       System.out.println("\nChoose an Option");
  52.       option = input.nextLine();
  53.       if (option.equals("1")) {//play game option.
  54.         System.out.println("Do you wish to play?");
  55.         option = input.nextLine();
  56.         if (option.equalsIgnoreCase("yes")) {
  57.           System.out.println("Enter your username: ");
  58.           username = input.nextLine();
  59.         }
  60.         database.createGame();
  61.         database.display2dArray();
  62.  
  63.         //starts the game
  64.  
  65.       } else if (option.equals("2")) {//leaderboard option
  66.         while (true) {
  67.           System.out.println("1) View top 10 players\n2) Search for a player score\n3) View entire leaderboard\n4) exit");
  68.           option = input.nextLine();
  69.           if (option.equals("1")) {
  70.  
  71.           } else if (option.equals("2")) {
  72.  
  73.             System.out.println(lead.displayPlayer());
  74.           } else if (option.equals("3")) {
  75.  
  76.           } else if (option.equals("4")) {
  77.             break;
  78.           } else {
  79.             System.out.println("Please choose an option available");
  80.           }
  81.         }
  82.       } else if (option.equals("3")) {//admin option
  83.         System.out.println("Please enter the password or type \"exit\" to leave: ");
  84.         attempt = input.nextLine();
  85.         if (!attempt.equals(password)) {
  86.           while (!attempt.equals(password)) {
  87.             tries--;
  88.             System.out.println("Incorrect password. Try again.\nYou have: " + (tries + 1) + " attempts left.");
  89.             attempt = input.nextLine();
  90.             if(tries == 0){
  91.               questionWriter.println();
  92.               scoresWriter.println();
  93.               questionWriter.println(database.displayArray());
  94.               scoresWriter.println(lead.displayArray1());
  95.               questionWriter.close();
  96.               scoresWriter.close();
  97.               System.exit(0);
  98.             }
  99.           }
  100.         }
  101.         tries = 3;
  102.         while (true) {
  103.           System.out.println("1) Add a question?");
  104.           System.out.println("2) Remove a question?");
  105.           System.out.println("3) View the database?");
  106.           System.out.println("4) Exit");
  107.           option = input.nextLine();
  108.           if (option.equalsIgnoreCase("add") || option.equals("1")) {
  109.             do {
  110.               System.out.println("Enter your question: ");
  111.               question = input.nextLine();
  112.               System.out.println("Enter correct answer: ");
  113.               answer = input.nextLine();
  114.               System.out.println("Enter 3 incorrect answers: ");
  115.               answer1 = input.nextLine();
  116.               answer2 = input.nextLine();
  117.               answer3 = input.nextLine();
  118.               database.addQandA(question, answer, answer1, answer2, answer3);
  119.               System.out.println("Do you want to enter new question?");
  120.               option = input.nextLine();
  121.             } while (option.equalsIgnoreCase("yes"));
  122.           } else if (option.equalsIgnoreCase("view") || option.equals("3")) {
  123.             System.out.println(database.displayArray());
  124.           } else if (option.equalsIgnoreCase("remove") || option.equals("2")) {
  125.  
  126.           } else if (!(option.equals("4")) || !(option.equalsIgnoreCase("exit"))) {
  127.             break;
  128.           } else {
  129.  
  130.           }
  131.         }
  132.       } else if (option.equals("4") || option.equalsIgnoreCase("exit")) {
  133.         System.out.println("Thanks for playing!");
  134.         questionWriter.println();
  135.         //scoresWriter.println();
  136.         questionWriter.println(database.displayArray());
  137.        // scoresWriter.println(lead.displayArray1());
  138.         break;
  139.       } else {//catch user error
  140.         System.out.println("Please choose a number available");
  141.       }
  142.     }
  143.     questionWriter.close();
  144.     //scoresWriter.close();
  145.   }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement