Some Exception handling

main
selim 2024-05-04 23:47:01 +02:00
parent 432ec54e1c
commit 38d1587405
6 changed files with 124 additions and 112 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -260,33 +260,33 @@ public class Box {
public void selectOption(String option) {
if (option.equals("One"))
if (option.equalsIgnoreCase("One"))
setCategoryOne = categoryOne;
else if (option.equals("Two"))
else if (option.equalsIgnoreCase("Two"))
setCategoryTwo = categoryTwo;
else if (option.equals("Three"))
else if (option.equalsIgnoreCase("Three"))
setCategoryThree = categoryThree;
else if (option.equals("Four"))
else if (option.equalsIgnoreCase("Four"))
setCategoryFour = categoryFour;
else if (option.equals("Five"))
else if (option.equalsIgnoreCase("Five"))
setCategoryFive = categoryFive;
else if (option.equals("Six"))
else if (option.equalsIgnoreCase("Six"))
setCategorySix = categorySix;
else if (option.equals("Seven"))
else if (option.equalsIgnoreCase("Seven"))
setCategorySeven = categorySeven;
else if (option.equals("Eight"))
else if (option.equalsIgnoreCase("Eight"))
setCategoryEight = categoryEight;
else if (option.equals("TripleMatch"))
else if (option.equalsIgnoreCase("TripleMatch"))
setCategoryTripleMatch = categoryTripleMatch;
else if (option.equals("FourOfAKind"))
else if (option.equalsIgnoreCase("FourOfAKind"))
setCategoryFourOfAKind = categoryFourOfAKind;
else if (option.equals("FullHouse"))
else if (option.equalsIgnoreCase("FullHouse"))
setCategoryFullHouse = categoryFullHouse;
else if (option.equals("SmallStreet"))
else if (option.equalsIgnoreCase("SmallStreet"))
setCategorySmallStreet = categorySmallStreet;
else if (option.equals("BigStreet"))
else if (option.equalsIgnoreCase("BigStreet"))
setCategoryBigStreet = categoryBigStreet;
else if (option.equals("Yahtzee")) {
else if (option.equalsIgnoreCase("Yahtzee")) {
if (setCategoryYahtzee != null) {
setCategoryYahtzee += 50;
if (counter[0] == 5)
@ -308,13 +308,13 @@ public class Box {
} else
setCategoryYahtzee = categoryYahtzee;
} else if (option.equals("categoryChance"))
} else if (option.equalsIgnoreCase("categoryChance"))
setCategoryChance = categoryChance;
else if (option.equals("Chance"))
else if (option.equalsIgnoreCase("Chance"))
setCategoryChance = categoryChance;
else if (option.equals("StarWarsDay"))
else if (option.equalsIgnoreCase("StarWarsDay"))
setCategoryStarWarsDay = categoryStarWarsDay;
else if (option.equals("R2D2"))
else if (option.equalsIgnoreCase("R2D2"))
setCategoryR2D2 = categoryR2D2;
}

View File

@ -1,5 +1,6 @@
package ui;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import facade.YahtzeeGame;
@ -13,127 +14,138 @@ public class TUI {
System.out.println("\nYahtzee Star Wars Special!");
while (!choice.equals("Stop")) {
while (!choice.equalsIgnoreCase("Stop")) {
YahtzeeGame game = new YahtzeeGame() {
};
System.out.println("\nMain menu:");
System.out.println(">Play");
System.out.println(">Highscores");
System.out.println(">Stop");
choice = sc.nextLine();
if (choice.equals("Highscores")) {
System.out.println("\nChoose your gamemode:");
System.out.println(">Normal");
System.out.println(">Special8");
System.out.println("\n" + game.showHighscores(sc.nextLine()));
System.out.println("\n>Return");
System.out.println(">Delete");
try {
YahtzeeGame game = new YahtzeeGame() {
};
System.out.println("\nMain menu:");
System.out.println(">Play");
System.out.println(">Highscores");
System.out.println(">Stop");
choice = sc.nextLine();
if (choice.equals("Return")) {
}
else if (choice.equals("Delete")) {
System.out.println(
"\nAre you sure you want to delete the Highscore file?\nAs a confirmation type: 'I AM SURE': ");
if (choice.equalsIgnoreCase("Highscores")) {
try {
System.out.println("\nChoose your gamemode:");
System.out.println(">Normal");
System.out.println(">Special8");
System.out.println("\n" + game.showHighscores(sc.nextLine()));
} catch (FileNotFoundException e) {
System.out.println("\nHighscore file not found!\n");
}
System.out.println("\n>Return");
System.out.println(">Delete");
choice = sc.nextLine();
if (choice.equals("I AM SURE")) {
game.deleteHighscores();
System.out.println("\nHighscore file got deleted...\nReturning to the main menu...");
} else {
System.out.println("Highscore file not deleted...\nReturning to the main menu...");
if (choice.equalsIgnoreCase("Return")) {
}
else if (choice.equalsIgnoreCase("Delete")) {
System.out.println(
"\nAre you sure you want to delete the Highscore file?\nAs a confirmation type: 'I AM SURE': ");
choice = sc.nextLine();
if (choice.equals("I AM SURE")) {
game.deleteHighscores();
System.out.println("\nHighscore file got deleted...\nReturning to the main menu...");
} else {
System.out.println("Highscore file not deleted...\nReturning to the main menu...");
}
}
}
}
else if (choice.equalsIgnoreCase("Play")) {
else if (choice.equals("Play")) {
System.out.println("\nChoose your gamemode: ");
System.out.println(">Normal");
System.out.println(">Special8");
game.setGamemode(sc.nextLine());
System.out.println("\nChoose your gamemode: ");
System.out.println(">Normal");
System.out.println(">Special8");
game.setGamemode(sc.nextLine());
System.out.println("\nAmount of players: ");
System.out.println(">1-6");
game.setPlayercount(Integer.parseInt(sc.nextLine()));
System.out.println("\nType in the player names, lock in each name with Enter: ");
System.out.println(">ex. Lucas [Enter] William [Enter] Lena [Enter]");
System.out.println("\nAmount of players: ");
System.out.println(">1-6");
game.setPlayercount(Integer.parseInt(sc.nextLine()));
String playerNames[] = new String[game.playerCount];
System.out.println("\nType in the player names, lock in each name with Enter: ");
System.out.println(">ex. Lucas [Enter] William [Enter] Lena [Enter]");
for (int i = 0; i < game.playerCount; i++) {
playerNames[i] = sc.nextLine();
}
game.setPlayerNames(playerNames);
while (!game.gameOverAll()) {
String playerNames[] = new String[game.playerCount];
for (int i = 0; i < game.playerCount; i++) {
playerNames[i] = sc.nextLine();
}
if (game.gameOver(i))
continue;
game.setPlayerNames(playerNames);
System.out.println("\n\n\nIts your turn " + game.getPlayerName(i));
System.out.println("\nThis is your Yahtzee Box: ");
System.out.println("\n" + game.selectedOptionsPrinted(i) + "\n");
while (!game.gameOverAll()) {
while (!game.allDicesLockedIn(i) && !game.noMoreRolls(i)) {
for (int i = 0; i < game.playerCount; i++) {
if (game.getLockedInDiceAmount(i) != 4)
System.out.println("Rolling dices... ");
else
System.out.println("Rolling the last dice...");
if (game.gameOver(i))
continue;
game.rollDices(i);
System.out.println("\n\n\nIts your turn " + game.getPlayerName(i));
System.out.println("\nThis is your Yahtzee Box: ");
System.out.println("\n" + game.selectedOptionsPrinted(i) + "\n");
System.out.println("Free dices: " + game.getFreeDiceNumbers(i));
System.out.println("Locked in dices: " + game.getLockedInDiceNumbers(i));
while (!game.allDicesLockedIn(i) && !game.noMoreRolls(i)) {
if (game.autoLockInDices(i)) {
System.out.println("\nNo rolls left => auto locking dices...\n");
if (game.getLockedInDiceAmount(i) != 4)
System.out.println("Rolling dices... ");
else
System.out.println("Rolling the last dice...");
game.rollDices(i);
System.out.println("Free dices: " + game.getFreeDiceNumbers(i));
System.out.println("Locked in dices: " + game.getLockedInDiceNumbers(i));
if (game.autoLockInDices(i)) {
System.out.println("\nNo rolls left => auto locking dices...\n");
}
if (!game.noMoreRolls(i)) {
System.out.println(
"Type in which free dices you want to keep \n(seperated by space, type 'none' if you dont want to keep any): ");
choice = sc.nextLine();
game.lockInDices(i, choice.split(" "));
if ((game.getLeftRolls(i)) != 1 && !game.allDicesLockedIn(i))
System.out.println("\n" + game.getLeftRolls(i) + " rolls left");
else if ((game.getLeftRolls(i)) == 1 && !game.allDicesLockedIn(i))
System.out.println("\n" + game.getLeftRolls(i) + " roll left");
}
}
if (!game.noMoreRolls(i)) {
System.out.println("\n\nLocked in dices: " + game.getLockedInDiceNumbers(i));
System.out.println("Done locking in... Here are your options: ");
game.evaluateAllOptions(i);
System.out.println("\n" + game.optionsPrinted(i));
if (game.onlyOneOption(i)) {
game.selectOption(i, game.getAllOptions(i).get(0).split(":")[0]);
System.out.println("Only one option possible => auto selecting option...");
} else {
System.out.println("Which option you want to select?: ");
game.selectOption(i, sc.nextLine());
}
if (game.gameOver(i)) {
System.out.println(
"Type in which free dices you want to keep \n(seperated by space, type 'none' if you dont want to keep any): ");
choice = sc.nextLine();
game.lockInDices(i, choice.split(" "));
if ((game.getLeftRolls(i)) != 1 && !game.allDicesLockedIn(i))
System.out.println("\n" + game.getLeftRolls(i) + " rolls left");
else if ((game.getLeftRolls(i)) == 1 && !game.allDicesLockedIn(i))
System.out.println("\n" + game.getLeftRolls(i) + " roll left");
"\n\nYour game is over, here is your total score: "
+ game.returnTotalPoints(i));
}
}
System.out.println("\n\nLocked in dices: " + game.getLockedInDiceNumbers(i));
System.out.println("Done locking in... Here are your options: ");
game.evaluateAllOptions(i);
System.out.println("\n" + game.optionsPrinted(i));
if (game.onlyOneOption(i)) {
game.selectOption(i, game.getAllOptions(i).get(0).split(":")[0]);
System.out.println("Only one option possible => auto selecting option...");
} else {
System.out.println("Which option you want to select?: ");
game.selectOption(i, sc.nextLine());
}
if (game.gameOver(i)) {
System.out.println(
"\n\nYour game is over, here is your total score: " + game.returnTotalPoints(i));
}
}
}
System.out.println("\n\nThe game is over, " + game.getWinnerMessage());
System.out.println("Saving all highscores to the highscore table...");
game.saveAllPlayersHighscores();
System.out.println(
"\nIf you want to stop the programm type: 'Stop', otherwise you will return to the main menu:");
choice = sc.nextLine();
System.out.println("\n\nThe game is over, " + game.getWinnerMessage());
System.out.println("Saving all highscores to the highscore table...");
game.saveAllPlayersHighscores();
System.out.println(
"\nIf you want to stop the programm type: 'Stop', otherwise you will return to the main menu:");
choice = sc.nextLine();
}
} catch (Exception e) {
System.out.println("\nInput not accepted... Returning to the main menu...\n");
}
}
sc.close();