Scanner Printer fixes

main
s.eser 2024-05-07 12:02:04 +02:00
parent 7af2b8d295
commit 4d068a6a5a
10 changed files with 184 additions and 104 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -406,7 +406,7 @@ public class Box {
}
public String selectedOptionsPrinted(int player) {
public String selectedOptionsPrinted() {
StringBuilder sb = new StringBuilder();

View File

@ -1,11 +1,13 @@
package domain;
import java.util.ArrayList;
public class Player {
public String name;
public Hand hand;
public Box box;
public String savedGamemode;
private String name;
private Hand hand;
private Box box;
private String savedGamemode;
public Player(String name, String gamemode) {
@ -22,5 +24,103 @@ public class Player {
}
public String getPlayername(){
return this.name;
}
public void rollDices(){
this.hand.rollDices();
}
public int getLeftRolls(){
return hand.getLeftRolls();
}
public ArrayList<Integer> getAllDiceNumbers(){
return hand.getAllDiceNumbers();
}
public ArrayList<Integer> getFreeDiceNumbers(){
return hand.getFreeDiceNumbers();
}
public ArrayList<Integer> getLockedInDiceNumbers(){
return hand.getLockedInDiceNumbers();
}
public int getLockedInDiceAmount(){
return hand.getLockedInDiceAmount();
}
public void lockInDices(String... dices){
hand.lockInDices(dices);
}
public boolean autoLockInDices(){
return hand.autoLockInDices();
}
public boolean allDicesLockedIn(){
return allDicesLockedIn();
}
public void evaluateAllOptions(){
box.evaluateBoxOptions(hand.getLockedInDiceNumbers());
}
public ArrayList<String> getAllOptions(){
return box.getAllOptions();
}
public void selectOption(String selectedOption){
box.selectOption(selectedOption);
}
public void resetOptions(){
box.resetOptions();
}
public void updateBonus(){
box.updateBonus();
}
public void updateUpperBoxScore(){
box.updateUpperBoxScore();
}
public void updateLowerBoxScore(){
box.updateLowerBoxScore();
}
public void updateTotalScore(){
box.updateTotalScore();
}
public boolean onlyOneOption(){
return box.onlyOneOption();
}
public String selectedOptionsPrinted(){
return box.selectedOptionsPrinted();
}
public boolean gameOver(){
return box.gameOver();
}
public int returnTotalPoints(){
return box.returnTotalPoints();
}
}

View File

@ -37,7 +37,7 @@ public class YahtzeeGame {
}
public String getPlayerName(int player) {
return players.get(player).name;
return players.get(player).getPlayername();
}
public void setGamemode(String gamemode) {
@ -47,56 +47,56 @@ public class YahtzeeGame {
public void rollDices(int player) {
players.get(player).hand.rollDices();
players.get(player).rollDices();
}
public ArrayList<Integer> getAllDiceNumbers(int player) {
return players.get(player).hand.getAllDiceNumbers();
return players.get(player).getAllDiceNumbers();
}
public ArrayList<Integer> getFreeDiceNumbers(int player) {
return players.get(player).hand.getFreeDiceNumbers();
return players.get(player).getFreeDiceNumbers();
}
public ArrayList<Integer> getLockedInDiceNumbers(int player) {
return players.get(player).hand.getLockedInDiceNumbers();
return players.get(player).getLockedInDiceNumbers();
}
public int getLockedInDiceAmount(int player) {
return players.get(player).hand.getLockedInDiceAmount();
return players.get(player).getLockedInDiceAmount();
}
public void lockInDices(int player, String... dices) {
if (dices[0].equalsIgnoreCase("none"))
return;
players.get(player).hand.lockInDices(dices);
players.get(player).lockInDices(dices);
}
public boolean autoLockInDices(int player) {
return players.get(player).hand.autoLockInDices();
return players.get(player).autoLockInDices();
}
public boolean allDicesLockedIn(int player) {
return players.get(player).hand.getLockedInDiceAmount() == 5;
return players.get(player).getLockedInDiceAmount() == 5;
}
public int getLeftRolls(int player) {
return players.get(player).hand.getLeftRolls();
return players.get(player).getLeftRolls();
}
@ -108,25 +108,25 @@ public class YahtzeeGame {
public void evaluateAllOptions(int player) {
players.get(player).box.evaluateBoxOptions(players.get(player).hand.getLockedInDiceNumbers());
players.get(player).evaluateAllOptions();
}
public ArrayList<String> getAllOptions(int player) {
return players.get(player).box.getAllOptions();
return players.get(player).getAllOptions();
}
public void selectOption(int player, String selectedOption) {
players.get(player).box.selectOption(selectedOption);
players.get(player).box.resetOptions();
players.get(player).selectOption(selectedOption);
players.get(player).resetOptions();
players.get(player).getNewHand();
players.get(player).box.updateBonus();
players.get(player).box.updateUpperBoxScore();
players.get(player).box.updateLowerBoxScore();
players.get(player).box.updateTotalScore();
players.get(player).updateBonus();
players.get(player).updateUpperBoxScore();
players.get(player).updateLowerBoxScore();
players.get(player).updateTotalScore();
}
@ -146,18 +146,18 @@ public class YahtzeeGame {
public boolean onlyOneOption(int player) {
return players.get(player).box.onlyOneOption();
return players.get(player).onlyOneOption();
}
public String selectedOptionsPrinted(int player) {
return players.get(player).box.selectedOptionsPrinted(player);
return players.get(player).selectedOptionsPrinted();
}
public boolean gameOver(int player) {
return players.get(player).box.gameOver();
return players.get(player).gameOver();
}
@ -165,7 +165,7 @@ public class YahtzeeGame {
int counter = 0;
for (int i = 0; i < playerCount; i++) {
if (players.get(i).box.gameOver())
if (players.get(i).gameOver())
counter++;
}
@ -174,8 +174,8 @@ public class YahtzeeGame {
public int returnTotalPoints(int player) {
if (players.get(player).box.gameOver())
return players.get(player).box.returnTotalPoints();
if (players.get(player).gameOver())
return players.get(player).returnTotalPoints();
else
return 0;
}
@ -183,49 +183,35 @@ public class YahtzeeGame {
private String[][] refreshHighscoreList() throws FileNotFoundException {
String highscores[][] = new String[10][3];
Scanner sc = new Scanner(new File("src/csv/highscores.csv"));
Scanner scSp = new Scanner(new File("src/csv/highscoresSpecial8.csv"));
String mem[] = new String[3];
String source;
if (savedGamemode.equalsIgnoreCase("Special8"))
for (int i = 0; i < 10; i++) {
if (scSp.hasNextLine()) {
mem = scSp.nextLine().split(" ");
highscores[i][0] = mem[0];
highscores[i][1] = mem[1];
highscores[i][2] = mem[2];
} else {
highscores[i][0] = "___";
highscores[i][1] = "___";
highscores[i][2] = "0";
}
}
if(savedGamemode.equalsIgnoreCase("Special8"))
source = "src/csv/highscoresSpecial8.csv";
else
source = "src/csv/highscores.csv";
for (int i = 0; i < 10; i++) {
Scanner sc = new Scanner(new File(source));
if (sc.hasNextLine()) {
mem = sc.nextLine().split(" ");
highscores[i][0] = mem[0];
highscores[i][1] = mem[1];
highscores[i][2] = mem[2];
for (int i = 0; i < 10; i++) {
} else {
highscores[i][0] = "___";
highscores[i][1] = "___";
highscores[i][2] = "0";
}
if (sc.hasNextLine()) {
mem = sc.nextLine().split(" ");
highscores[i][0] = mem[0];
highscores[i][1] = mem[1];
highscores[i][2] = mem[2];
} else {
highscores[i][0] = "___";
highscores[i][1] = "___";
highscores[i][2] = "0";
}
}
sc.close();
scSp.close();
return highscores;
}
@ -252,7 +238,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[0][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[0][i] = "" + players.get(player).name;
highscores[0][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[0][i] = "" + LocalDate.now();
highscoreMem2 = highscores[1][i];
@ -281,7 +267,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[1][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[1][i] = "" + players.get(player).name;
highscores[1][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[1][i] = "" + LocalDate.now();
highscoreMem1 = highscores[2][i];
@ -307,7 +293,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[2][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[2][i] = "" + players.get(player).name;
highscores[2][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[2][i] = "" + LocalDate.now();
highscoreMem2 = highscores[3][i];
@ -331,7 +317,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[3][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[3][i] = "" + players.get(player).name;
highscores[3][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[3][i] = "" + LocalDate.now();
highscoreMem1 = highscores[4][i];
@ -353,7 +339,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[4][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[4][i] = "" + players.get(player).name;
highscores[4][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[4][i] = "" + LocalDate.now();
highscoreMem2 = highscores[5][i];
@ -373,7 +359,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[5][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[5][i] = "" + players.get(player).name;
highscores[5][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[5][i] = "" + LocalDate.now();
highscoreMem1 = highscores[6][i];
@ -391,7 +377,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[6][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[6][i] = "" + players.get(player).name;
highscores[6][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[6][i] = "" + LocalDate.now();
highscoreMem2 = highscores[7][i];
@ -407,7 +393,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[7][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[7][i] = "" + players.get(player).name;
highscores[7][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[7][i] = "" + LocalDate.now();
highscoreMem1 = highscores[8][i];
@ -421,7 +407,7 @@ public class YahtzeeGame {
if (i == 2)
highscores[8][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[8][i] = "" + players.get(player).name;
highscores[8][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[8][i] = "" + LocalDate.now();
highscores[9][i] = highscoreMem1;
@ -432,34 +418,31 @@ public class YahtzeeGame {
if (i == 2)
highscores[9][i] = "" + returnTotalPoints(player);
else if (i == 1)
highscores[9][i] = "" + players.get(player).name;
highscores[9][i] = "" + players.get(player).getPlayername();
else if (i == 0)
highscores[9][i] = "" + LocalDate.now();
}
}
PrintWriter out = new PrintWriter(new FileWriter("src/csv/highscores.csv", false));
PrintWriter outSp = new PrintWriter(new FileWriter("src/csv/highscoresSpecial8.csv", false));
String source;
if (savedGamemode.equalsIgnoreCase("Special8"))
for (int i = 0; i < 10; i++) {
outSp.print(highscores[i][0] + " " + highscores[i][1] + " "
+ highscores[i][2]);
outSp.println();
}
if(savedGamemode.equalsIgnoreCase("Special8"))
source = "src/csv/highscoresSpecial8.csv";
else
for (int i = 0; i < 10; i++) {
source = "src/csv/highscores.csv";
out.print(highscores[i][0] + " " + highscores[i][1] + " "
+ highscores[i][2]);
PrintWriter out = new PrintWriter(new FileWriter(source));
out.println();
}
for (int i = 0; i < 10; i++) {
out.print(highscores[i][0] + " " + highscores[i][1] + " "
+ highscores[i][2]);
out.println();
}
out.close();
outSp.close();
} catch (Exception e) {
}
@ -492,16 +475,18 @@ public class YahtzeeGame {
public void deleteHighscores() throws IOException {
PrintWriter out = new PrintWriter(new FileWriter("src/csv/highscores.csv", false));
PrintWriter outSp = new PrintWriter(new FileWriter("src/csv/highscoresSpecial8.csv", false));
String source;
if (savedGamemode.equalsIgnoreCase("Special8"))
outSp.print("");
if(savedGamemode.equalsIgnoreCase("Special8"))
source = "src/csv/highscoresSpecial8.csv";
else
out.print("");
source = "src/csv/highscores.csv";
PrintWriter out = new PrintWriter(new FileWriter(source));
out.print("");
out.close();
outSp.close();
}

View File

@ -32,12 +32,7 @@ public class TUI {
System.out.println("\nChoose your gamemode:");
System.out.println(">Normal");
System.out.println(">Special8");
choice = sc.nextLine();
if(choice.equalsIgnoreCase("Special8"))
game.setGamemode("Special8");
else
game.setGamemode("Normal");
System.out.println("\n" + game.showHighscores(choice));
System.out.println("\n" + game.showHighscores(sc.nextLine()));
} catch (FileNotFoundException e) {
System.out.println("\nHighscore file not found!\n");
notFound = true;