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

View File

@ -1,5 +1,6 @@
package ui; package ui;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Scanner; import java.util.Scanner;
import facade.YahtzeeGame; import facade.YahtzeeGame;
@ -13,127 +14,138 @@ public class TUI {
System.out.println("\nYahtzee Star Wars Special!"); System.out.println("\nYahtzee Star Wars Special!");
while (!choice.equals("Stop")) { while (!choice.equalsIgnoreCase("Stop")) {
YahtzeeGame game = new YahtzeeGame() { try {
}; YahtzeeGame game = new YahtzeeGame() {
System.out.println("\nMain menu:"); };
System.out.println(">Play"); System.out.println("\nMain menu:");
System.out.println(">Highscores"); System.out.println(">Play");
System.out.println(">Stop"); System.out.println(">Highscores");
choice = sc.nextLine(); System.out.println(">Stop");
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");
choice = sc.nextLine(); choice = sc.nextLine();
if (choice.equals("Return")) {
}
else if (choice.equals("Delete")) { if (choice.equalsIgnoreCase("Highscores")) {
System.out.println( try {
"\nAre you sure you want to delete the Highscore file?\nAs a confirmation type: 'I AM SURE': "); 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(); choice = sc.nextLine();
if (choice.equals("I AM SURE")) { if (choice.equalsIgnoreCase("Return")) {
game.deleteHighscores(); }
System.out.println("\nHighscore file got deleted...\nReturning to the main menu...");
} else { else if (choice.equalsIgnoreCase("Delete")) {
System.out.println("Highscore file not deleted...\nReturning to the main menu..."); 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("\nAmount of players: ");
System.out.println(">Normal"); System.out.println(">1-6");
System.out.println(">Special8"); game.setPlayercount(Integer.parseInt(sc.nextLine()));
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]");
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++) { String playerNames[] = new String[game.playerCount];
playerNames[i] = sc.nextLine();
}
game.setPlayerNames(playerNames);
while (!game.gameOverAll()) {
for (int i = 0; i < game.playerCount; i++) { for (int i = 0; i < game.playerCount; i++) {
playerNames[i] = sc.nextLine();
}
if (game.gameOver(i)) game.setPlayerNames(playerNames);
continue;
System.out.println("\n\n\nIts your turn " + game.getPlayerName(i)); while (!game.gameOverAll()) {
System.out.println("\nThis is your Yahtzee Box: ");
System.out.println("\n" + game.selectedOptionsPrinted(i) + "\n");
while (!game.allDicesLockedIn(i) && !game.noMoreRolls(i)) { for (int i = 0; i < game.playerCount; i++) {
if (game.getLockedInDiceAmount(i) != 4) if (game.gameOver(i))
System.out.println("Rolling dices... "); continue;
else
System.out.println("Rolling the last dice...");
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)); while (!game.allDicesLockedIn(i) && !game.noMoreRolls(i)) {
System.out.println("Locked in dices: " + game.getLockedInDiceNumbers(i));
if (game.autoLockInDices(i)) { if (game.getLockedInDiceAmount(i) != 4)
System.out.println("\nNo rolls left => auto locking dices...\n"); 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( 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): "); "\n\nYour game is over, here is your total score: "
choice = sc.nextLine(); + game.returnTotalPoints(i));
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");
} }
} }
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("\n\nThe game is over, " + game.getWinnerMessage());
System.out.println("Saving all highscores to the highscore table..."); System.out.println("Saving all highscores to the highscore table...");
game.saveAllPlayersHighscores(); game.saveAllPlayersHighscores();
System.out.println( System.out.println(
"\nIf you want to stop the programm type: 'Stop', otherwise you will return to the main menu:"); "\nIf you want to stop the programm type: 'Stop', otherwise you will return to the main menu:");
choice = sc.nextLine(); choice = sc.nextLine();
}
} catch (Exception e) {
System.out.println("\nInput not accepted... Returning to the main menu...\n");
} }
} }
sc.close(); sc.close();