mirror of
https://github.com/Cametendo/Cards-Gate.git
synced 2026-03-18 04:40:20 +01:00
Update Main.java
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.internal.bind.util.ISO8601Utils;
|
|
||||||
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
class CardsClass {
|
class CardsClass {
|
||||||
public String question = "";
|
public String question = "";
|
||||||
public String answer = "";
|
public String answer = "";
|
||||||
@@ -15,101 +13,156 @@ class CardsClass {
|
|||||||
this.question = question;
|
this.question = question;
|
||||||
this.answer = answer;
|
this.answer = answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Wrapper {
|
class Wrapper {
|
||||||
List<CardsClass> cards;
|
List<CardsClass> cards;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
private static final Scanner userAnswers = new Scanner(System.in);
|
||||||
|
private static final Scanner jsonPath = new Scanner(System.in);
|
||||||
|
private static final Gson gson = new Gson();
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Gson gson = new Gson();
|
printWelcome();
|
||||||
Scanner jsonPath = new Scanner(System.in);
|
mainMenu();
|
||||||
Scanner userAnswers = new Scanner(System.in);
|
}
|
||||||
System.out.println("Welcome to: Cards Gate");
|
|
||||||
System.out.println("-----------------------------------------------------------------------");
|
|
||||||
System.out.println("| ##### ##### |");
|
|
||||||
System.out.println("| # # ## ##### ##### #### ### # # ## ##### ###### |");
|
|
||||||
System.out.println("| # # # # # # # # ### # # # # # |");
|
|
||||||
System.out.println("| # # # # # # # #### # #### # # # ##### |");
|
|
||||||
System.out.println("| # ###### ##### # # # ### # # ###### # # |");
|
|
||||||
System.out.println("| # # # # # # # # # # ### # # # # # # |");
|
|
||||||
System.out.println("| ##### # # # # ##### #### # ##### # # # ###### |");
|
|
||||||
System.out.println("| # |");
|
|
||||||
System.out.println("-----------------------------------------------------------------------");
|
|
||||||
String start = "";
|
|
||||||
while (true) {
|
|
||||||
System.out.println("Would you like to start or read the manual? (y/n/m)");
|
|
||||||
start = userAnswers.nextLine();
|
|
||||||
|
|
||||||
if (start.equalsIgnoreCase("n")) {
|
private static void printWelcome() {
|
||||||
System.exit(0); // Exit program
|
System.out.println("Welcome to: Cards:Gate");
|
||||||
} else if (start.equalsIgnoreCase("m")) {
|
System.out.println("-----------------------------------------------------------------------");
|
||||||
System.out.println("The rules:");
|
System.out.println("| ##### ##### |");
|
||||||
System.out.println("You are allowed to redo a card / deck as many times as you want.");
|
System.out.println("| # # ## ##### ##### #### ### # # ## ##### ###### |");
|
||||||
System.out.println("You can skip a question, look at the answer or go back to the previous question.");
|
System.out.println("| # # # # # # # # ### # # # # # |");
|
||||||
System.out.println("You always have a re-order of your cards / deck");
|
System.out.println("| # # # # # # # #### # #### # # # ##### |");
|
||||||
System.out.println("That's it. For more details: Read the manual");
|
System.out.println("| # ###### ##### # # # ### # # ###### # # |");
|
||||||
System.out.println("");
|
System.out.println("| # # # # # # # # # # ### # # # # # # |");
|
||||||
System.out.println("Have fun!!");
|
System.out.println("| ##### # # # # ##### #### # ##### # # # ###### |");
|
||||||
// Loop back to ask again
|
System.out.println("| # |");
|
||||||
} else if (start.equalsIgnoreCase("y")) {
|
System.out.println("-----------------------------------------------------------------------");
|
||||||
System.out.println("Please enter the path of the file you would like to read: ");
|
}
|
||||||
String filePath = jsonPath.nextLine();
|
|
||||||
Wrapper wrapper = new Wrapper();
|
|
||||||
try (Reader fileReader = new FileReader(filePath)) {
|
|
||||||
wrapper = new Gson().fromJson(fileReader, Wrapper.class);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
int cardNumber = 0;
|
private static void mainMenu() {
|
||||||
while (true) {
|
while (true) {
|
||||||
String question = wrapper.cards.get(cardNumber).question;
|
System.out.println("Would you like to start or read the manual? (y/n/m)");
|
||||||
String answer = wrapper.cards.get(cardNumber).answer;
|
String start = userAnswers.nextLine();
|
||||||
|
|
||||||
System.out.println("What's: " + question + "?");
|
if (start.equalsIgnoreCase("n")) {
|
||||||
System.out.println("Available options: back, answer, skip");
|
exitGame();
|
||||||
String userInput = userAnswers.nextLine();
|
} else if (start.equalsIgnoreCase("m")) {
|
||||||
/*
|
printManual();
|
||||||
In-session Settings
|
} else if (start.equalsIgnoreCase("y")) {
|
||||||
*/
|
startGame();
|
||||||
if (userInput.equalsIgnoreCase("back")) {
|
} else {
|
||||||
cardNumber--;
|
System.out.println("Invalid choice, please enter y, n or m.");
|
||||||
} else if (userInput.equalsIgnoreCase("answer")) {
|
|
||||||
System.out.println(answer);
|
|
||||||
} else if (userInput.equalsIgnoreCase("skip")) {
|
|
||||||
cardNumber++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Deck Settings
|
|
||||||
*/
|
|
||||||
if (userInput.equalsIgnoreCase(answer)) {
|
|
||||||
cardNumber++;
|
|
||||||
if (cardNumber >= wrapper.cards.size()) {
|
|
||||||
System.out.println("You've answered all cards");
|
|
||||||
System.out.println("Redo Deck? (y/n)");
|
|
||||||
String finishedDeck = userAnswers.nextLine();
|
|
||||||
if (finishedDeck.equalsIgnoreCase("y")) {
|
|
||||||
System.out.println("Restarting Deck...");
|
|
||||||
cardNumber = 0; // Restart deck
|
|
||||||
} else if(finishedDeck.equalsIgnoreCase("n")) {
|
|
||||||
System.out.println("Another Deck? (y/n)");
|
|
||||||
String anotherDeck = userAnswers.nextLine();
|
|
||||||
if (anotherDeck.equalsIgnoreCase("y")) {
|
|
||||||
break; // Exit card loop
|
|
||||||
} else if (anotherDeck.equalsIgnoreCase("n")) {
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
System.out.println("Wrong, try again...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
System.out.println("Invalid choice, please enter y, n or m.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void printManual() {
|
||||||
|
System.out.println("The rules:");
|
||||||
|
System.out.println("You are allowed to redo a card / deck as many times as you want.");
|
||||||
|
System.out.println("You can skip a question, look at the answer or go back to the previous question.");
|
||||||
|
System.out.println("You always have a re-order of your cards / deck");
|
||||||
|
System.out.println("That's it. For more details: Read the manual");
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("Have fun!!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void startGame() {
|
||||||
|
System.out.println("Please enter the path of the file you would like to read: ");
|
||||||
|
String filePath = jsonPath.nextLine();
|
||||||
|
|
||||||
|
Wrapper wrapper = loadDeck(filePath);
|
||||||
|
if (wrapper == null || wrapper.cards == null || wrapper.cards.isEmpty()) {
|
||||||
|
System.out.println("Error: No cards found or invalid file.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
playDeck(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Wrapper loadDeck(String filePath) {
|
||||||
|
try (Reader fileReader = new FileReader(filePath)) {
|
||||||
|
return gson.fromJson(fileReader, Wrapper.class);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Could not read file: " + e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void playDeck(Wrapper wrapper) {
|
||||||
|
int cardNumber = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (cardNumber < 0) cardNumber = 0;
|
||||||
|
if (cardNumber >= wrapper.cards.size()) {
|
||||||
|
if (!handleDeckFinished(wrapper)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
cardNumber = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CardsClass card = wrapper.cards.get(cardNumber);
|
||||||
|
System.out.println("What's: " + card.question + "?");
|
||||||
|
System.out.println("Available options: back, answer, skip");
|
||||||
|
|
||||||
|
String userInput = userAnswers.nextLine();
|
||||||
|
|
||||||
|
if (handleSessionInput(userInput, card, wrapper, cardNumber)) {
|
||||||
|
cardNumber++;
|
||||||
|
} else if (userInput.equalsIgnoreCase("back")) {
|
||||||
|
cardNumber--;
|
||||||
|
} else if (userInput.equalsIgnoreCase("skip")) {
|
||||||
|
cardNumber++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean handleSessionInput(String userInput, CardsClass card, Wrapper wrapper, int cardNumber) {
|
||||||
|
if (userInput.equalsIgnoreCase("answer")) {
|
||||||
|
System.out.println(card.answer);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userInput.equalsIgnoreCase(card.answer)) {
|
||||||
|
return true; // correct answer
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userInput.equalsIgnoreCase("back") && !userInput.equalsIgnoreCase("skip")) {
|
||||||
|
System.out.println("Wrong, try again...");
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean handleDeckFinished(Wrapper wrapper) {
|
||||||
|
System.out.println("You've answered all cards");
|
||||||
|
System.out.println("Redo Deck? (y/n)");
|
||||||
|
String finishedDeck = userAnswers.nextLine();
|
||||||
|
|
||||||
|
if (finishedDeck.equalsIgnoreCase("y")) {
|
||||||
|
System.out.println("Restarting Deck...");
|
||||||
|
return true;
|
||||||
|
} else if (finishedDeck.equalsIgnoreCase("n")) {
|
||||||
|
System.out.println("Another Deck? (y/n)");
|
||||||
|
String anotherDeck = userAnswers.nextLine();
|
||||||
|
|
||||||
|
if (anotherDeck.equalsIgnoreCase("y")) {
|
||||||
|
return false; // break deck loop, go back to main menu
|
||||||
|
} else if (anotherDeck.equalsIgnoreCase("n")) {
|
||||||
|
exitGame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void exitGame() {
|
||||||
|
System.out.println("Exiting Cards:Gate...");
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user