highscore set Exception handling

main
selim 2024-04-28 16:48:48 +02:00
parent 0081a61c11
commit 549dc0a399
4 changed files with 263 additions and 14 deletions

View File

Binary file not shown.

View File

View File

@ -1,6 +1,11 @@
package facade;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;
import domain.Player;
@ -25,66 +30,310 @@ public class YahtzeeGame {
public void rollDices(int player) {
players.get(player - 1).hand.rollDices();
players.get(player).hand.rollDices();
}
public ArrayList<Integer> getAllDiceNumbers(int player) {
return players.get(player - 1).hand.getAllDiceNumbers();
return players.get(player).hand.getAllDiceNumbers();
}
public ArrayList<Integer> getFreeDiceNumbers(int player) {
return players.get(player - 1).hand.getFreeDiceNumbers();
return players.get(player).hand.getFreeDiceNumbers();
}
public ArrayList<Integer> getLockedInDiceNumbers(int player) {
return players.get(player - 1).hand.getLockedInDiceNumbers();
return players.get(player).hand.getLockedInDiceNumbers();
}
public void lockInDices(int player, int... dices) {
players.get(player - 1).hand.lockInDices(dices);
players.get(player).hand.lockInDices(dices);
}
public int getLeftRolls(int player) {
return players.get(player - 1).hand.getLeftRolls();
return players.get(player).hand.getLeftRolls();
}
public ArrayList<String> getAllOptionsEvaluated(int player) {
players.get(player - 1).box.evaluateBoxOptions(players.get(player - 1).hand.getAllDiceNumbers());
return players.get(player - 1).box.getAllOptions();
players.get(player).box.evaluateBoxOptions(players.get(player - 1).hand.getAllDiceNumbers());
return players.get(player).box.getAllOptions();
}
public void selectOption(int player, String selectedOption) {
players.get(player - 1).box.selectOption(selectedOption);
players.get(player - 1).box.resetOptions();
players.get(player - 1).getNewHand();
players.get(player).box.selectOption(selectedOption);
players.get(player).box.resetOptions();
players.get(player).getNewHand();
}
public boolean gameOver(int player) {
return players.get(player - 1).box.gameOver();
return players.get(player).box.gameOver();
}
public int returnTotalPoints(int player) {
if (players.get(player - 1).box.gameOver())
return players.get(player - 1).box.returnTotalPoints();
if (players.get(player).box.gameOver())
return players.get(player).box.returnTotalPoints();
else
return 0;
}
public void setScoreToHighscores(int player) throws FileNotFoundException {
String highscores[][] = new String[10][3];
String highscoreMem1 = "";
String highscoreMem2 = "";
Scanner sc = new Scanner("src/csv/highscores.csv");
for (int i = 0; i < 10; i++) {
if (sc.hasNextLine()) {
highscores[i][0] = sc.next();
highscores[i][1] = sc.next();
highscores[i][2] = sc.nextLine();
}
}
for (int i = 0; i < 10; i++) {
if (highscores[i][2] == null) {
highscores[i][0] = sc.next();
highscores[i][1] = sc.next();
highscores[i][2] = sc.nextLine();
}
}
try {
if (returnTotalPoints(player) < Integer.parseInt(highscores[9][2])) {
return;
}
} catch (Exception e) {
}
try {
if (returnTotalPoints(player) > Integer.parseInt(highscores[0][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem1 = highscores[0][i];
if (i == 2)
highscores[0][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[0][i] = "" + players.get(player).name;
else if (i == 0)
highscores[0][i] = "" + LocalDate.now();
highscoreMem2 = highscores[1][i];
highscores[1][i] = highscoreMem1;
highscoreMem1 = highscores[2][i];
highscores[2][i] = highscoreMem2;
highscoreMem2 = highscores[3][i];
highscores[3][i] = highscoreMem1;
highscoreMem1 = highscores[4][i];
highscores[4][i] = highscoreMem2;
highscoreMem2 = highscores[5][i];
highscores[5][i] = highscoreMem1;
highscoreMem1 = highscores[6][i];
highscores[6][i] = highscoreMem2;
highscoreMem2 = highscores[7][i];
highscores[7][i] = highscoreMem1;
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[1][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem2 = highscores[1][i];
if (i == 2)
highscores[1][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[1][i] = "" + players.get(player).name;
else if (i == 0)
highscores[1][i] = "" + LocalDate.now();
highscoreMem1 = highscores[2][i];
highscores[2][i] = highscoreMem2;
highscoreMem2 = highscores[3][i];
highscores[3][i] = highscoreMem1;
highscoreMem1 = highscores[4][i];
highscores[4][i] = highscoreMem2;
highscoreMem2 = highscores[5][i];
highscores[5][i] = highscoreMem1;
highscoreMem1 = highscores[6][i];
highscores[6][i] = highscoreMem2;
highscoreMem2 = highscores[7][i];
highscores[7][i] = highscoreMem1;
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[2][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem1 = highscores[2][i];
if (i == 2)
highscores[2][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[2][i] = "" + players.get(player).name;
else if (i == 0)
highscores[2][i] = "" + LocalDate.now();
highscoreMem2 = highscores[3][i];
highscores[3][i] = highscoreMem1;
highscoreMem1 = highscores[4][i];
highscores[4][i] = highscoreMem2;
highscoreMem2 = highscores[5][i];
highscores[5][i] = highscoreMem1;
highscoreMem1 = highscores[6][i];
highscores[6][i] = highscoreMem2;
highscoreMem2 = highscores[7][i];
highscores[7][i] = highscoreMem1;
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[3][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem2 = highscores[3][i];
if (i == 2)
highscores[3][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[3][i] = "" + players.get(player).name;
else if (i == 0)
highscores[3][i] = "" + LocalDate.now();
highscoreMem1 = highscores[4][i];
highscores[4][i] = highscoreMem2;
highscoreMem2 = highscores[5][i];
highscores[5][i] = highscoreMem1;
highscoreMem1 = highscores[6][i];
highscores[6][i] = highscoreMem2;
highscoreMem2 = highscores[7][i];
highscores[7][i] = highscoreMem1;
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[4][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem1 = highscores[4][i];
if (i == 2)
highscores[4][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[4][i] = "" + players.get(player).name;
else if (i == 0)
highscores[4][i] = "" + LocalDate.now();
highscoreMem2 = highscores[5][i];
highscores[5][i] = highscoreMem1;
highscoreMem1 = highscores[6][i];
highscores[6][i] = highscoreMem2;
highscoreMem2 = highscores[7][i];
highscores[7][i] = highscoreMem1;
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[5][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem2 = highscores[5][i];
if (i == 2)
highscores[5][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[5][i] = "" + players.get(player).name;
else if (i == 0)
highscores[5][i] = "" + LocalDate.now();
highscoreMem1 = highscores[6][i];
highscores[6][i] = highscoreMem2;
highscoreMem2 = highscores[7][i];
highscores[7][i] = highscoreMem1;
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[6][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem1 = highscores[6][i];
if (i == 2)
highscores[6][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[6][i] = "" + players.get(player).name;
else if (i == 0)
highscores[6][i] = "" + LocalDate.now();
highscoreMem2 = highscores[7][i];
highscores[7][i] = highscoreMem1;
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[7][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem2 = highscores[7][i];
if (i == 2)
highscores[7][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[7][i] = "" + players.get(player).name;
else if (i == 0)
highscores[7][i] = "" + LocalDate.now();
highscoreMem1 = highscores[8][i];
highscores[8][i] = highscoreMem2;
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[8][2])) {
for (int i = 2; i >= 0; i--) {
highscoreMem1 = highscores[8][i];
if (i == 2)
highscores[8][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[8][i] = "" + players.get(player).name;
else if (i == 0)
highscores[8][i] = "" + LocalDate.now();
highscores[9][i] = highscoreMem1;
}
} else if (returnTotalPoints(player) > Integer.parseInt(highscores[9][2])) {
for (int i = 2; i >= 0; i--) {
if (i == 2)
highscores[9][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[9][i] = "" + players.get(player).name;
else if (i == 0)
highscores[9][i] = "" + LocalDate.now();
}
}
PrintWriter out = new PrintWriter("src/csv/highscores.csv");
for (int i = 0; i < 10; i++) {
for (int s = 0; s < 3; s++) {
out.print(i + ": " + LocalDate.now() + ": " + players.get(player).name + ": "
+ returnTotalPoints(player));
}
out.println();
}
out.close();
} catch (Exception e) {
}
}
}