main
unknown
plain_text
3 years ago
14 kB
2
Indexable
Never
package millionaire; import java.awt.Color; import java.io.File; import java.util.*; import enigma.core.Enigma; public class Main { public static void main(String[] args) throws InterruptedException { enigma.console.Console cn = Enigma.getConsole("The Game", 200, 50, 15); EnigmaWrapper wrapper = new EnigmaWrapper(cn); wrapper.consoleColor(Color.ORANGE, Color.black); InfoBox box = new InfoBox(cn); Thread thread = new Thread(box); String[] statics = {}; String selection = ""; boolean next_contestant = true; int answered = 0; Scanner scanner = new Scanner(System.in); Question[] questions = new Question[0]; Participant[] participants = new Participant[0]; String contestant = ""; String[] word_cloud; int id = -1, random_contestant = -1; int money_won = 0; //wrapper.splashScreen(); wrapper.clearConsole(); thread.start(); File result_file = new File("result.txt"); result_file.delete(); while (true) { System.out.print("***** Menu *****\r\n" + "1.Load questions\r\n" + "2.Load participants\r\n" + "3.Start competition \r\n" + "4.Show statistics\r\n" + "5.Exit\r\n" + "\n" + "Please make a choice: "); String option = scanner.nextLine(); wrapper.clearConsole(); if (option.equalsIgnoreCase("1")) { System.out.print("Please enter the file name to load for questions: "); //String fileName_1 = scanner.nextLine(); String fileName_1 = "questions.txt"; System.out.println(); System.out.println(); questions = Millionaire.get_questions("stop_words.txt", fileName_1); if (questions.length != 0) { wrapper.clearConsole(); System.out.println("\nThe questions file is loaded."); String[] categories = new String[questions.length]; int index = 0; for (int i = 0; i < questions.length; i++) { categories[index] = questions[i].getCategory(); for (int j = 0; j < categories.length; j++) { if (j != index && categories[index].equals(categories[j])) { categories[j] = null; } } index++; } System.out.println("Category\t\t\tThe Number of Questions"); System.out.println("--------\t\t\t-----------------------"); int count; for (String category : categories) { count = 0; if (category == null) continue; for (Question quest : questions) { if (quest.getCategory().equals(category)) count++; } System.out.print(category); for (int i = 0; i < 30-category.length(); i++) { System.out.print(" "); } System.out.println(count); } System.out.println(); int diffcounter1 = 0; int diffcounter2 = 0; int diffcounter3 = 0; int diffcounter4 = 0; int diffcounter5 = 0; for (int i = 0; i < questions.length; i++) { if (questions[i].getDifficulty() == 1) { diffcounter1 ++; }else if (questions[i].getDifficulty() == 2) { diffcounter2 ++; } else if (questions[i].getDifficulty() == 3) { diffcounter3 ++; } else if (questions[i].getDifficulty() == 4) { diffcounter4 ++; } else if (questions[i].getDifficulty() == 5) { diffcounter5 ++; } } System.out.println(); System.out.println("Difficulty level \t The number of questions"); System.out.println("---------------- \t -----------------------"); System.out.println("\t\t1 \t\t\t\t " + diffcounter1); System.out.println("\t\t2 \t\t\t\t " + diffcounter2); System.out.println("\t\t3 \t\t\t\t " + diffcounter3); System.out.println("\t\t4 \t\t\t\t " + diffcounter4); System.out.println("\t\t5 \t\t\t\t " + diffcounter5); System.out.println(); System.out.println(); } } else if (option.equalsIgnoreCase("2")) { System.out.print("Please enter the file name to load for participants: "); //String fileName_2 = scanner.nextLine(); String fileName_2 = "participants.txt"; participants = Millionaire.get_participants(fileName_2); if (participants.length != 0) { wrapper.clearConsole(); System.out.println("\nThe participants file is loaded."); System.out.println(); } } else if (option.equalsIgnoreCase("3")) { if (questions.length == 0 || participants.length == 0) { System.out.println("First load the files, please.\n"); continue; } wrapper.clearConsole(); money_won = 0; if (next_contestant) { for (int i = 0; i < 1; i++) { random_contestant = Millionaire.randomInt(0, participants.length); if (participants[random_contestant].getisPlayed() == false) { contestant = participants[random_contestant].getName(); participants[random_contestant].isPlayed = true; } else { i--; } } } box.setIs50Used(false); box.setIsDoubleDipUsed(false); File file = new File("temp.txt"); file.delete(); System.out.println("\nContestant: " + contestant); System.out.println("-----------------------------------------------------------"); for (int i = 1; i < 6; i++) { box.setMoney(money_won); word_cloud = Millionaire.generate_word_cloud(i, questions); System.out.println("\nQ" + i + " Word Cloud:\n"); for (int j = 0; j < word_cloud.length; j++) { System.out.print(word_cloud[j] + "\t"); } while (true) { System.out.println(); System.out.println("> Enter your selection: "); selection = scanner.nextLine(); wrapper.clearConsole(); if (Millionaire.isStringInArray(selection, word_cloud)) { break; } else { System.out.println("Please enter correct word"); } } id = Millionaire.get_question_id(i, selection, questions); System.out.println("Q" + i + ") " + questions[id].getText()); System.out.println("A) " + questions[id].getChoiceA()); System.out.println("B) " + questions[id].getChoiceB()); System.out.println("C) " + questions[id].getChoiceC()); System.out.println("D) " + questions[id].getChoiceD()); System.out.println("Enter your choice (E:Exit): "); box.setThreadInterrupt(true); String answer = scanner.nextLine(); box.setThreadInterrupt(false); boolean is_time_run_out = false; String temps[][] = FileOperations.arrayDismantler("temp.txt"); for (int j = 0; j < temps.length; j++) { if(temps[j][0].equals("is_time_run_out")){ is_time_run_out = true; } } if(is_time_run_out){ System.out.println("Time has run out"); System.out.println("Correct Answer is " + questions[id].getCorrectAnswer()); System.out.println("You won: $" + Millionaire.get_money_tier(i)); answered = 0; Thread.sleep(1000); break; } String text = ""; if (answer.equalsIgnoreCase("50") && !box.getIs50Used() == true) { // 50% wrapper.clearConsole(); text = Millionaire.j_50(questions[id].getChoiceA(), questions[id].getChoiceB(), questions[id].getChoiceC(), questions[id].getChoiceD(), questions[id].getCorrectAnswer()); System.out.println("Q" + i + ") " + questions[id].getText()); System.out.println(text); box.setIs50Used(true); System.out.println("Enter your choice (E:Exit): "); answer = scanner.nextLine(); } if (answer.equalsIgnoreCase("double")) { //double box.setIsDoubleDipUsed(true); String selected_choices = ""; if (box.getIs50Used() == true && !questions[id].getIsAsked()) { wrapper.clearConsole(); System.out.println("Q" + i + ") " + questions[id].getText()); System.out.println(text); String[] parts = text.split("\n"); for (int j = 0; j < parts.length; j++) { if (parts[j].length()<4) { selected_choices += parts[j].substring(0, 1); } } } else { wrapper.clearConsole(); System.out.println("Q" + i + ") " + questions[id].getText()); System.out.println("A) " + questions[id].getChoiceA()); System.out.println("B) " + questions[id].getChoiceB()); System.out.println("C) " + questions[id].getChoiceC()); System.out.println("D) " + questions[id].getChoiceD()); } System.out.println("Enter your choice (E:Exit): "); answer = scanner.nextLine(); if (answer.equalsIgnoreCase(questions[id].getCorrectAnswer())) { System.out.println("Correct Answer!"); money_won = Millionaire.get_money_won(i); System.out.println("You have: $" + money_won); questions[i].setIsAsked(true); answered = 1; continue; } else{ selected_choices += answer.toUpperCase(); wrapper.clearConsole(); System.out.println("Wrong Answer"); System.out.println("Q" + i + ") " + questions[id].getText()); if (selected_choices.toUpperCase().contains("A")) { System.out.println("A) "); } else { System.out.println("A) " + questions[id].getChoiceA()); } if (selected_choices.toUpperCase().contains("B")) { System.out.println("B) "); } else { System.out.println("B) " + questions[id].getChoiceB()); } if (selected_choices.toUpperCase().contains("C")) { System.out.println("C) "); } else { System.out.println("C) " + questions[id].getChoiceC()); } if (selected_choices.toUpperCase().contains("D")) { System.out.println("D) "); } else { System.out.println("D) " + questions[id].getChoiceD()); } System.out.println("Enter your choice (E:Exit): "); answer = scanner.nextLine(); if (answer.equalsIgnoreCase(questions[id].getCorrectAnswer())) { System.out.println("Correct Answer!"); money_won = Millionaire.get_money_won(i); System.out.println("You have: $" + money_won); questions[i].setIsAsked(true); answered = 1; continue; } else{ System.out.println("Wrong Answer"); System.out.println("Correct Answer is " + questions[id].getCorrectAnswer()); System.out.println("You won: $" + money_won); questions[i].setIsAsked(true); answered = 0; break; } } } else if (answer.equalsIgnoreCase("e")) { System.out.println("You won: $" + money_won); System.out.println("Correct Answer is " + questions[id].getCorrectAnswer()); questions[i].setIsAsked(true); answered = 0; break; } else if (answer.equalsIgnoreCase(questions[id].getCorrectAnswer())) { System.out.println("Correct Answer!"); money_won = Millionaire.get_money_won(i); System.out.println("You have: $" + money_won); questions[i].setIsAsked(true); answered = 1; continue; } else if(!answer.equalsIgnoreCase(questions[id].getCorrectAnswer())){ System.out.println("Wrong Answer"); System.out.println("Correct Answer is " + questions[id].getCorrectAnswer()); System.out.println("You won: $" + money_won); questions[i].setIsAsked(true); answered = 0; break; } } System.out.println("Do you want to skip to next contestant? (y/n)"); String next = scanner.nextLine(); if (next.equalsIgnoreCase("n")) { next_contestant = false; } else { next_contestant = true; } FileOperations.writeToFile("result.txt", id + "#" + random_contestant + "#" + answered); } else if (option.equalsIgnoreCase("4")) { if (questions.length == 0 || participants.length == 0) { System.out.println("First load the files, please.\n"); continue; } statics = FileOperations.arrayCreater("result.txt"); Millionaire.print_statics(statics, questions, participants); } else if (option.equalsIgnoreCase("5")) { System.out.println("The game closed."); scanner.close(); break; } else { System.out.println("\nUnknown option."); } } } }