nearly done

main
selim 2024-04-29 16:18:10 +02:00
parent b207c6ab88
commit 0dc39592db
10 changed files with 232 additions and 99 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -348,4 +348,53 @@ public class Box {
} }
public String selectedOptionsPrinted(int player) {
StringBuilder sb = new StringBuilder();
if (savedGamemode.equals("Special8")) {
sb.append("categoryOne: " + nullCheck(setCategoryOne) + "\n");
sb.append("categoryTwo: " + nullCheck(setCategoryTwo) + "\n");
sb.append("categoryThree: " + nullCheck(setCategoryThree) + "\n");
sb.append("categoryFour: " + nullCheck(setCategoryFour) + "\n");
sb.append("categoryFive: " + nullCheck(setCategoryFive) + "\n");
sb.append("categorySix: " + nullCheck(setCategorySix) + "\n");
sb.append("categorySeven: " + nullCheck(setCategorySeven) + "\n");
sb.append("categoryEight: " + nullCheck(setCategoryEight) + "\n");
sb.append("\n--------------------------\n\n");
sb.append("categoryTripleMatch: " + nullCheck(setCategoryTripleMatch) + "\n");
sb.append("categoryFourOfAKind: " + nullCheck(setCategoryFourOfAKind) + "\n");
sb.append("categoryFullHouse: " + nullCheck(setCategoryFullHouse) + "\n");
sb.append("categorySmallStreet: " + nullCheck(setCategorySmallStreet) + "\n");
sb.append("categoryBigStreet: " + nullCheck(setCategoryBigStreet) + "\n");
sb.append("categoryYahtzee: " + nullCheck(setCategoryYahtzee) + "\n");
sb.append("categoryChance: " + nullCheck(setCategoryChance) + "\n");
sb.append("categoryR2D2: " + nullCheck(setCategoryR2D2) + "\n");
} else {
sb.append("categoryOne: " + nullCheck(setCategoryOne) + "\n");
sb.append("categoryTwo: " + nullCheck(setCategoryTwo) + "\n");
sb.append("categoryThree: " + nullCheck(setCategoryThree) + "\n");
sb.append("categoryFour: " + nullCheck(setCategoryFour) + "\n");
sb.append("categoryFive: " + nullCheck(setCategoryFive) + "\n");
sb.append("categorySix: " + nullCheck(setCategorySix) + "\n");
sb.append("\n--------------------------\n\n");
sb.append("categoryTripleMatch: " + nullCheck(setCategoryTripleMatch) + "\n");
sb.append("categoryFourOfAKind: " + nullCheck(setCategoryFourOfAKind) + "\n");
sb.append("categoryFullHouse: " + nullCheck(setCategoryFullHouse) + "\n");
sb.append("categorySmallStreet: " + nullCheck(setCategorySmallStreet) + "\n");
sb.append("categoryBigStreet: " + nullCheck(setCategoryBigStreet) + "\n");
sb.append("categoryYahtzee: " + nullCheck(setCategoryYahtzee) + "\n");
sb.append("categoryChance: " + nullCheck(setCategoryChance) + "\n");
}
return sb.toString();
}
public String nullCheck(Integer i) {
if (i == null)
return "___";
else
return "" + i;
}
} }

View File

@ -33,13 +33,19 @@ public class Hand {
} }
public void lockInDices(int... dices) { public void lockInDices(String... dices) {
for (int i = 0; i < dices.length; i++) { for (int i = 0; i < dices.length; i++) {
lockInDice(dices[i]); lockInDice(Integer.parseInt(dices[i]));
} }
} }
public int getLockedInDiceAmount() {
return lockedInDices.size();
}
public void rollDices() { public void rollDices() {
if (this.leftRolls > 0) if (this.leftRolls > 0)
@ -96,4 +102,5 @@ public class Hand {
return this.leftRolls; return this.leftRolls;
} }
} }

View File

@ -9,42 +9,39 @@ import java.util.Scanner;
import domain.Player; import domain.Player;
public class YahtzeeGame { public class YahtzeeGame {
public int playerCount; public int playerCount;
public ArrayList<Player> players = new ArrayList<>(); public ArrayList<Player> players = new ArrayList<>();
public String savedGamemode; public String savedGamemode;
public YahtzeeGame() { public YahtzeeGame() {
} }
public void setPlayercount(int playerCount){ public void setPlayercount(int playerCount) {
this.playerCount = playerCount; this.playerCount = playerCount;
} }
public void setPlayerNames(String... names){ public void setPlayerNames(String... names) {
for (int i = 0; i < this.playerCount; i++) { for (int i = 0; i < this.playerCount; i++) {
this.players.add(new Player(names[i], this.savedGamemode)); this.players.add(new Player(names[i], this.savedGamemode));
} }
} }
public String getPlayerName(int player){ public String getPlayerName(int player) {
return players.get(player).name; return players.get(player).name;
} }
public void setGamemode(String gamemode){ public void setGamemode(String gamemode) {
this.savedGamemode = gamemode; this.savedGamemode = gamemode;
} }
public void rollDices(int player) { public void rollDices(int player) {
players.get(player).hand.rollDices(); players.get(player).hand.rollDices();
@ -69,21 +66,35 @@ public class YahtzeeGame {
} }
public void lockInDices(int player, int... dices) { public void lockInDices(int player, String... dices) {
if (dices[0].equals("none"))
return;
players.get(player).hand.lockInDices(dices); players.get(player).hand.lockInDices(dices);
} }
public boolean allDicesLockedIn(int player) {
return players.get(player).hand.getLockedInDiceAmount() == 5;
}
public int getLeftRolls(int player) { public int getLeftRolls(int player) {
return players.get(player).hand.getLeftRolls(); return players.get(player).hand.getLeftRolls();
} }
public boolean noMoreRolls(int player) {
return 0 == getLeftRolls(player);
}
public ArrayList<String> getAllOptionsEvaluated(int player) { public ArrayList<String> getAllOptionsEvaluated(int player) {
players.get(player).box.evaluateBoxOptions(players.get(player - 1).hand.getAllDiceNumbers()); players.get(player).box.evaluateBoxOptions(players.get(player).hand.getAllDiceNumbers());
return players.get(player).box.getAllOptions(); return players.get(player).box.getAllOptions();
} }
@ -96,23 +107,42 @@ public class YahtzeeGame {
} }
public String optionsPrinted(int player) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < getAllOptionsEvaluated(player).size(); i++) {
sb.append(getAllOptionsEvaluated(player).get(i) + "\n");
}
return sb.toString();
}
public String selectedOptionsPrinted(int player) {
return players.get(player).box.selectedOptionsPrinted(player);
}
public boolean gameOver(int player) { public boolean gameOver(int player) {
return players.get(player).box.gameOver(); return players.get(player).box.gameOver();
} }
public boolean gameOverAll(){ public boolean gameOverAll() {
int counter = 0; int counter = 0;
for(int i = 0; i<playerCount; i++){ for (int i = 0; i < playerCount; i++) {
if(players.get(i).box.gameOver()) if (players.get(i).box.gameOver())
counter++; counter++;
} }
return counter == playerCount; return counter == playerCount;
} }
public int returnTotalPoints(int player) { public int returnTotalPoints(int player) {
if (players.get(player).box.gameOver()) if (players.get(player).box.gameOver())
@ -121,7 +151,7 @@ public class YahtzeeGame {
return 0; return 0;
} }
private String[][] refreshHighscoreList(){ private String[][] refreshHighscoreList() {
String highscores[][] = new String[10][3]; String highscores[][] = new String[10][3];
Scanner sc = new Scanner("src/csv/highscores.csv"); Scanner sc = new Scanner("src/csv/highscores.csv");
@ -138,7 +168,7 @@ public class YahtzeeGame {
sc.close(); sc.close();
return highscores; return highscores;
} }
public void setScoreToHighscores(int player, String highscores[][]) throws IOException { public void setScoreToHighscores(int player, String highscores[][]) throws IOException {
String highscoreMem1 = ""; String highscoreMem1 = "";
@ -147,7 +177,6 @@ public class YahtzeeGame {
try { try {
if (returnTotalPoints(player) < Integer.parseInt(highscores[9][2])) { if (returnTotalPoints(player) < Integer.parseInt(highscores[9][2])) {
return; return;
} }
@ -353,7 +382,7 @@ public class YahtzeeGame {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
for (int s = 0; s < 3; s++) { for (int s = 0; s < 3; s++) {
out.print(i + ": " + LocalDate.now() + ": " + players.get(player).name + ": " out.print((i + 1) + ": " + LocalDate.now() + ": " + players.get(player).name + ": "
+ returnTotalPoints(player)); + returnTotalPoints(player));
} }
out.println(); out.println();
@ -362,40 +391,56 @@ public class YahtzeeGame {
out.close(); out.close();
} catch (Exception e) { } catch (Exception e) {
} }
} }
public void saveAllPlayersHighscores() throws IOException{ public void saveAllPlayersHighscores() throws IOException {
for(int i = 0; i<playerCount; i++){ for (int i = 0; i < playerCount; i++) {
setScoreToHighscores(i,refreshHighscoreList()); setScoreToHighscores(i, refreshHighscoreList());
} }
} }
public String showHighscores(){ public String showHighscores() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for(int i = 0; i<10; i++){ for (int i = 0; i < 10; i++) {
sb.append(i+": "+refreshHighscoreList()[i][0]); sb.append((i + 1) + ": " + refreshHighscoreList()[i][0]);
sb.append(": " + refreshHighscoreList()[i][1]); sb.append(": " + refreshHighscoreList()[i][1]);
sb.append(": " + refreshHighscoreList()[i][2] + "\n"); sb.append(": " + refreshHighscoreList()[i][2] + "\n");
} }
return sb.toString(); return sb.toString();
} }
public void deleteHighscores() throws IOException{ public void deleteHighscores() throws IOException {
PrintWriter out = new PrintWriter(new FileWriter("src/csv/highscores.csv", false)); PrintWriter out = new PrintWriter(new FileWriter("src/csv/highscores.csv", false));
out.println(""); out.println("");
out.close(); out.close();
} }
public String getWinnerMessage() {
int winnerScore = returnTotalPoints(0);
String winnerName = getPlayerName(0);
for (int i = 0; i < this.playerCount; i++) {
if (winnerScore < returnTotalPoints(i)) {
winnerScore = returnTotalPoints(i);
winnerName = getPlayerName(i);
}
}
return winnerName + " is the winner, with a total score of: " + winnerScore;
}
} }

View File

@ -1,6 +1,7 @@
package ui; package ui;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
import facade.YahtzeeGame; import facade.YahtzeeGame;
@ -9,77 +10,108 @@ public class TUI {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
String choice =""; String choice = "";
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
boolean newGame = false; boolean newGame = false;
System.out.println("Yahtzee Star Wars Special!"); System.out.println("Yahtzee Star Wars Special!");
while(newGame){ while (newGame) {
YahtzeeGame game = new YahtzeeGame(){}; YahtzeeGame game = new YahtzeeGame() {
};
while (!choice.equals("stop")) { while (!choice.equals("stop")) {
System.out.println("Main menu:\n"); System.out.println("Main menu:\n");
System.out.println(">Play"); System.out.println(">Play");
System.out.println(">Highscores"); System.out.println(">Highscores");
choice = sc.nextLine();
if (choice.equals("Highscores")) {
System.out.println(game.showHighscores());
System.out.println(">Return");
System.out.println(">Delete");
choice = sc.nextLine(); choice = sc.nextLine();
if (choice.equals("Return"))
if(choice.equals("Highscores")){ break;
System.out.println(game.showHighscores()); else if (choice.equals("Delete")) {
System.out.println(">Return"); System.out.println(
System.out.println(">Delete"); "Are you sure you want to delete the Highscore file?\nAs a confirmation type: 'I AM SURE': ");
choice = sc.nextLine(); choice = sc.nextLine();
if(choice.equals("Return")) if (choice.equals("I AM SURE")) {
break; game.deleteHighscores();
else if(choice.equals("Delete")){ System.out.println("Highscore file got deleted...\nReturning to the main menu...");
System.out.println("Are you sure you want to delete the Highscore file?\nAs a confirmation type: 'I AM SURE': "); } else {
choice = sc.nextLine(); System.out.println("Highscore file not deleted...\nReturning to the main menu...");
if(choice.equals("I AM SURE")){
game.deleteHighscores();
System.out.println("Highscore file got deleted...\nReturning to the main menu...");
}
else{
System.out.println("Highscore file not deleted...\nReturning to the main menu...");
}
} }
} }
else if(choice.equals("Play")){
System.out.println("Choose your gamemode: ");
System.out.println(">Normal");
System.out.println(">StarWarsDay");
System.out.println(">Special8");
game.setGamemode(sc.nextLine());
System.out.println("Amount of players: ");
System.out.println(">1-6");
game.setPlayercount(sc.nextInt());
System.out.println("Type 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];
for(int i = 0; i < game.playerCount; i++){
playerNames[i] = sc.nextLine();
}
game.setPlayerNames(playerNames);
while (!game.gameOverAll()) {
for(int i = 0; i<game.playerCount; i++){
System.out.println("Its your turn "+game.getPlayerName(i));
}
}
}
} }
else if (choice.equals("Play")) {
System.out.println("Choose your gamemode: ");
System.out.println(">Normal");
System.out.println(">StarWarsDay");
System.out.println(">Special8");
game.setGamemode(sc.nextLine());
System.out.println("Amount of players: ");
System.out.println(">1-6");
game.setPlayercount(sc.nextInt());
System.out.println("Type 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];
for (int i = 0; i < game.playerCount; i++) {
playerNames[i] = sc.nextLine();
}
game.setPlayerNames(playerNames);
while (!game.gameOverAll()) {
for (int i = 0; i < game.playerCount; i++) {
if (game.gameOver(i))
continue;
System.out.println("Its your turn " + game.getPlayerName(i));
System.out.println("This is your Yahtzee Box: ");
System.out.println(game.selectedOptionsPrinted(i) + "\n");
while (!game.allDicesLockedIn(i) || !game.noMoreRolls(i)) {
System.out.println("Your dices: ");
System.out.println("Free dices: " + game.getFreeDiceNumbers(i));
System.out.println("Locked in dices: " + game.getLockedInDiceNumbers(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(" "));
game.rollDices(i);
System.out.println(game.getLeftRolls(i) + " rolls left");
System.out.println("Rerolling dices... ");
}
System.out.println("Done locking in... Here are your options: ");
System.out.println(game.optionsPrinted(i));
System.out.println("Which option you want to select?: ");
game.selectOption(i, sc.nextLine());
if (game.gameOver(i)) {
System.out.println(
"Your game is over, here is your total score: " + game.returnTotalPoints(i));
}
}
}
System.out.println("The game is over, saving all highscores to the highscore table...");
game.saveAllPlayersHighscores();
}
}
} }
} }