Code erweitert
parent
f440ecb839
commit
cf3085b177
|
@ -34,5 +34,17 @@
|
|||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>4.5.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -23,8 +23,8 @@ public class HitoriValidator {
|
|||
public boolean validateBoard(List<String> solutionCoordinates) {
|
||||
for (String coordinate : solutionCoordinates) {
|
||||
String[] parts = coordinate.split(",");
|
||||
int row = Integer.parseInt(parts[0]);
|
||||
int col = Integer.parseInt(parts[1]);
|
||||
int row = Integer.parseInt(parts[0]) - 1;
|
||||
int col = Integer.parseInt(parts[1]) - 1;
|
||||
|
||||
if (board.getCell(row,col).getState() != HitoriCell.CellState.BLACK){
|
||||
return false;
|
||||
|
@ -34,7 +34,7 @@ public class HitoriValidator {
|
|||
for (int i = 0; i < board.getSize(); i++) {
|
||||
for (int j = 0; j < board.getSize(); j++) {
|
||||
if (board.getCell(i,j).getState() == HitoriCell.CellState.BLACK &&
|
||||
!solutionCoordinates.contains(i + "," + j)) {
|
||||
!solutionCoordinates.contains((i + 1) + "," + (j + 1))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,24 @@ import java.util.List;
|
|||
|
||||
public class BoardLoader {
|
||||
|
||||
public static class BoardData {
|
||||
private final int[][] board;
|
||||
private final List<String> solution;
|
||||
|
||||
public BoardData(int[][] board, List<String> solution) {
|
||||
this.board = board;
|
||||
this.solution = solution;
|
||||
}
|
||||
|
||||
public int[][] getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
public List<String> getSolution() {
|
||||
return solution;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> loadBoardsAsList() {
|
||||
List<String> boardFiles = new ArrayList<>();
|
||||
try {
|
||||
|
@ -30,7 +48,7 @@ public class BoardLoader {
|
|||
|
||||
|
||||
|
||||
public static int[][] loadBoard(String resourcePath) throws IOException {
|
||||
public static int[][] loadBoard(String resourcePath) throws IOException {
|
||||
List<int[]> rows = new ArrayList<>();
|
||||
|
||||
try (InputStream inputStream = BoardLoader.class.getResourceAsStream(resourcePath);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import PR2.HitoriSpiel.Domain.HitoriValidator;
|
||||
import PR2.HitoriSpiel.Domain.StateManager;
|
||||
import PR2.HitoriSpiel.Domain.HitoriBoard;
|
||||
import PR2.HitoriSpiel.Domain.HitoriCell;
|
||||
|
@ -150,9 +151,15 @@ public class GameBoard extends JPanel {
|
|||
repaint();
|
||||
}
|
||||
|
||||
// TODO: Spiel validieren
|
||||
public boolean validateCurrentBoard() {
|
||||
return true;
|
||||
HitoriValidator validator = new HitoriValidator(board);
|
||||
if (validator.validateBoard(board.getSolutionCoordinates())) {
|
||||
JOptionPane.showMessageDialog(this, "Das Spielfeld ist korrekt gelöst!", "Erfolg", JOptionPane.INFORMATION_MESSAGE);
|
||||
return true;
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Das Spielfeld enthält Fehler!", "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void showPauseMenu() {
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import PR2.HitoriSpiel.Utils.HighscoreManager;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
public class HighscoreDialog extends JDialog {
|
||||
|
||||
public HighscoreDialog(JFrame parent) {
|
||||
super(parent, "Highscoreliste", true);
|
||||
setSize(500, 400);
|
||||
setLocationRelativeTo(parent);
|
||||
|
||||
// Tabelle erstellen
|
||||
String[] columnNames = {"Name", "Zeit (Sek.)", "Spielfeld"};
|
||||
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
|
||||
|
||||
// Highscores laden
|
||||
List<HighscoreManager.Highscore> highscores = HighscoreManager.loadHighscores();
|
||||
for (HighscoreManager.Highscore highscore : highscores) {
|
||||
tableModel.addRow(new Object[]{highscore.getName(), highscore.getTime(), highscore.getBoard()});
|
||||
}
|
||||
|
||||
JTable table = new JTable(tableModel);
|
||||
table.setEnabled(false); // Tabelle nur lesbar
|
||||
table.setFillsViewportHeight(true);
|
||||
|
||||
JScrollPane scrollPane = new JScrollPane(table);
|
||||
|
||||
// Button zum Schließen des Dialogs
|
||||
JButton closeButton = new JButton("Schließen");
|
||||
closeButton.addActionListener(e -> dispose());
|
||||
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.add(closeButton);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package PR2.HitoriSpiel.Utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class HighscoreManager {
|
||||
|
||||
private static final String HIGHSCORE_FILE = "highscores.txt";
|
||||
|
||||
public static class Highscore {
|
||||
private final String name;
|
||||
private final int time;
|
||||
private final String board;
|
||||
|
||||
public Highscore(String name, int time, String board) {
|
||||
this.name = name;
|
||||
this.time = time;
|
||||
this.board = board;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public String getBoard() {
|
||||
return board;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Highscore> loadHighscores() {
|
||||
List<Highscore> highscores = new ArrayList<>();
|
||||
File file = new File(HIGHSCORE_FILE);
|
||||
|
||||
if (!file.exists()) {
|
||||
return highscores;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] parts = line.split(", ");
|
||||
if (parts.length == 3) {
|
||||
String name = parts[0];
|
||||
int time = Integer.parseInt(parts[1]);
|
||||
String board = parts[2];
|
||||
highscores.add(new Highscore(name, time, board));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Highscores nach Zeit sortieren (kürzeste Zeit zuerst)
|
||||
highscores.sort(Comparator.comparingInt(Highscore::getTime));
|
||||
return highscores;
|
||||
}
|
||||
|
||||
public static void saveHighscore(String name, int time, String board) {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(HIGHSCORE_FILE, true))) {
|
||||
writer.write(name + ", " + time + ", " + board);
|
||||
writer.newLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
Name, Zeit (Sekunden), Spielfeld
|
||||
|
||||
NameTest, 123 Sekunden, Hitori4x4_leicht.csv
|
Loading…
Reference in New Issue