Compare commits
No commits in common. "cabd4db873cc393912c31e108dbfbc67ba2c98e3" and "1a32962ba8072bd98643a2d3ec82580c5b78b8ac" have entirely different histories.
cabd4db873
...
1a32962ba8
|
|
@ -4,54 +4,36 @@ import java.io.BufferedReader;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class CSVReader {
|
public class CSVReader {
|
||||||
|
|
||||||
public PuzzleData readPuzzleWithSolution(String csvFile) throws IOException {
|
/**
|
||||||
List<int[]> puzzleRows = new ArrayList<>();
|
* Liest die CSV-Datei ein und gibt die enthaltenen Werte als zweidimensionales int-Array zurück.
|
||||||
List<int[]> solutionCoordinates = new ArrayList<>();
|
*
|
||||||
|
* @param csvFile Pfad zur CSV-Datei
|
||||||
|
* @return zweidimensionales Array, das die Zahlen aus der CSV enthält
|
||||||
|
* @throws IOException wenn ein Fehler beim Lesen auftritt
|
||||||
|
*/
|
||||||
|
public int[][] readCsvToIntArray(String csvFile) throws IOException {
|
||||||
|
ArrayList<int[]> rows = new ArrayList<>();
|
||||||
|
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
|
||||||
String line;
|
String line;
|
||||||
boolean inSolutionPart = false;
|
|
||||||
|
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
line = line.trim();
|
|
||||||
if (line.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (line.startsWith("//Lösung") || line.startsWith("//")) {
|
|
||||||
inSolutionPart = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inSolutionPart) {
|
|
||||||
// Puzzle-Teil
|
|
||||||
String[] tokens = line.split(",");
|
String[] tokens = line.split(",");
|
||||||
int[] rowValues = new int[tokens.length];
|
int[] row = new int[tokens.length];
|
||||||
for (int i = 0; i < tokens.length; i++) {
|
for (int i = 0; i < tokens.length; i++) {
|
||||||
rowValues[i] = Integer.parseInt(tokens[i].trim());
|
row[i] = Integer.parseInt(tokens[i].trim());
|
||||||
}
|
|
||||||
puzzleRows.add(rowValues);
|
|
||||||
} else {
|
|
||||||
// Lösungsteil
|
|
||||||
String[] coords = line.split(",");
|
|
||||||
if (coords.length == 2) {
|
|
||||||
int row = Integer.parseInt(coords[0].trim());
|
|
||||||
int col = Integer.parseInt(coords[1].trim());
|
|
||||||
solutionCoordinates.add(new int[]{row, col});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
rows.add(row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// puzzleRows -> 2D-Array
|
// ArrayList in 2D-Array umwandeln
|
||||||
int[][] puzzleArray = new int[puzzleRows.size()][];
|
int[][] result = new int[rows.size()][];
|
||||||
for (int i = 0; i < puzzleRows.size(); i++) {
|
for (int i = 0; i < rows.size(); i++) {
|
||||||
puzzleArray[i] = puzzleRows.get(i);
|
result[i] = rows.get(i);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
return new PuzzleData(puzzleArray, solutionCoordinates);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,101 +1,4 @@
|
||||||
package de.deversmann.facade;
|
package de.deversmann.facade;
|
||||||
|
|
||||||
import de.deversmann.domain.*;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Facade {
|
public class Facade {
|
||||||
|
|
||||||
private final CSVReader csvReader;
|
|
||||||
private final HighscoreManager highscoreManager;
|
|
||||||
private final Zeiterfassung zeiterfassung;
|
|
||||||
|
|
||||||
private PuzzleData currentPuzzleData;
|
|
||||||
private String currentPuzzleName;
|
|
||||||
private int currentErrorCount = 0;
|
|
||||||
|
|
||||||
public Facade() {
|
|
||||||
this.csvReader = new CSVReader();
|
|
||||||
this.highscoreManager = new HighscoreManager();
|
|
||||||
this.zeiterfassung = new Zeiterfassung();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ladeSpielfeld(String csvPfad) throws IOException {
|
|
||||||
this.currentPuzzleData = csvReader.readPuzzleWithSolution(csvPfad);
|
|
||||||
this.currentPuzzleName = csvPfad;
|
|
||||||
this.currentErrorCount = 0;
|
|
||||||
|
|
||||||
zeiterfassung.stop();
|
|
||||||
zeiterfassung.start();
|
|
||||||
System.out.println("Neues Spielfeld geladen, Timer + Fehler=0");
|
|
||||||
}
|
|
||||||
|
|
||||||
public long stopZeiterfassung() {
|
|
||||||
zeiterfassung.stop();
|
|
||||||
return zeiterfassung.getElapsedTimeInSeconds();
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getElapsedTimeSoFar() {
|
|
||||||
return zeiterfassung.getElapsedTimeInSeconds();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[][] getAktuellesPuzzle() {
|
|
||||||
return (currentPuzzleData != null) ? currentPuzzleData.getPuzzle() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<int[]> getLoesungsKoordinaten() {
|
|
||||||
return (currentPuzzleData != null) ? currentPuzzleData.getSolutionCoordinates() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fehler-Tracking
|
|
||||||
public void incrementErrorCount() {
|
|
||||||
currentErrorCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCurrentErrorCount() {
|
|
||||||
return currentErrorCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Highscore
|
|
||||||
public void addHighscoreForCurrentPuzzle(String playerName, long timeSeconds, int errorCount) {
|
|
||||||
if (currentPuzzleName != null && !currentPuzzleName.isEmpty()) {
|
|
||||||
highscoreManager.addHighscore(currentPuzzleName, playerName, timeSeconds, errorCount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<HighscoreEntry> getHighscoresForCurrentPuzzle() {
|
|
||||||
if (currentPuzzleName == null) {
|
|
||||||
return List.of();
|
|
||||||
}
|
|
||||||
return highscoreManager.getHighscores(currentPuzzleName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getAverageTimeForCurrentPuzzle() {
|
|
||||||
if (currentPuzzleName == null) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return highscoreManager.getAverageTime(currentPuzzleName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCurrentPuzzleName() {
|
|
||||||
return currentPuzzleName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveCurrentPuzzleHighscoreToFile() throws IOException {
|
|
||||||
if (currentPuzzleName == null) return;
|
|
||||||
String base = currentPuzzleName.replaceAll(".*/", "");
|
|
||||||
String shortName = base.replace(".csv", "") + "_highscore.txt";
|
|
||||||
highscoreManager.saveSinglePuzzle(currentPuzzleName, shortName);
|
|
||||||
System.out.println("Highscore zu " + base + " in " + shortName + " gespeichert.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void loadCurrentPuzzleHighscoreFromFile() throws IOException {
|
|
||||||
if (currentPuzzleName == null) return;
|
|
||||||
String base = currentPuzzleName.replaceAll(".*/", "");
|
|
||||||
String shortName = base.replace(".csv", "") + "_highscore.txt";
|
|
||||||
highscoreManager.loadSinglePuzzle(currentPuzzleName, shortName);
|
|
||||||
System.out.println("Highscore zu " + base + " aus " + shortName + " geladen (falls existiert).");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue