fixed pathing and gui, added tests,

main
Berat Kocak 2025-01-02 20:24:12 +01:00
parent 39f106f1bd
commit f4de80f801
25 changed files with 408 additions and 289 deletions

View File

@ -29,7 +29,7 @@
</classpathentry>
<!-- Test Resources -->
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/main/java/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
@ -65,7 +65,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/main/java/de/hs_mannheim/informatik/mbvn/resources">
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/main/java/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>

View File

@ -118,7 +118,7 @@
<directory>src/test/java</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<directory>src/main/java/resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>

View File

@ -5,66 +5,73 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.time.DateTimeException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Stack;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import de.hs_mannheim.informatik.mvn.gui.*;
public class HitoriMain2 extends JFrame implements ActionListener{
private static String[] filepath = {"", ""};
private static byte[] gameData;
public static void main(String[] args) throws FileNotFoundException{
System.out.println("HERE");
new MenuGUI();
}
public static void ablauf(CardLayout cl, JPanel main, String[] filepath) throws FileNotFoundException {
Stack<String> madeMoves = new Stack<>();
String[][] data = getData(filepath[0], Integer.parseInt(filepath[1]));
String[][] colors = makeColorArray(data.length);
JButton[][] buttons = makeButtonArray(data);
GameGUI.paintGame(cl, main, filepath, buttons, colors, madeMoves, data);
}
public static void ablauf(CardLayout cl, JPanel main, InputStream inputStream, int rows, String path) throws IOException {
Stack<String> madeMoves = new Stack<>();
gameData = inputStream.readAllBytes();
InputStream newStream = new ByteArrayInputStream(gameData);
String[][] data = getData(newStream, rows);
String[][] colors = makeColorArray(data.length);
JButton[][] buttons = makeButtonArray(data);
InputStream newStream1 = new ByteArrayInputStream(gameData);
GameGUI.paintGame(newStream1, cl, main, buttons, colors, madeMoves, data, path);
}
public static ArrayList<String> readFromFile(String path){
public static ArrayList<String> readFromFile(InputStream inputStream){
ArrayList<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
e.getMessage();
}
return lines;
}
public static String[][] getData(String filepath, int rows) throws FileNotFoundException{
Scanner sc = new Scanner(new File(filepath));
public static String[][] getData(InputStream inputStream, int rows) throws FileNotFoundException{
String[][] data = new String[rows][rows];
int rowInt = 0;
while (sc.hasNextLine() && rowInt < rows) {
String line = sc.nextLine();
data[rowInt] = line.split(",");
rowInt++;
}
sc.close();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
int rowInt = 0;
while ((line = reader.readLine()) != null && rowInt < rows) {
data[rowInt] = line.split(",");
rowInt++;
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static String[][] makeColorArray(int size){
String[][] colors = new String[size][size];
for(int i=0;i<size;i++){
@ -87,26 +94,31 @@ public class HitoriMain2 extends JFrame implements ActionListener{
return buttons;
}
public static boolean abgabeMöglich(String path, String[][] data, String[][] colors) throws FileNotFoundException{
boolean abgabeMöglich = false;
String[][] result = getResult(data, colors);
ArrayList<String> filteredData = getSolution(path, result);
String[][] ergebnis1 = getErgebnisArray(result, filteredData);
int count = 0;
public static boolean checkArraySame(String[][] ergebnis,String[][] result){
int count = 0;
for(int i=0;i<result.length;i++){
for(int j=0;j<result.length;j++){
if(result[i][j].equals(ergebnis1[i][j])){
if(result[i][j].equals(ergebnis[i][j])){
continue;
} else {
count--;
}
}
}
if(count <0){
abgabeMöglich = false;
boolean arrayNotSame = false;
return arrayNotSame;
} else {
abgabeMöglich = true;
boolean arraySame = true;
return arraySame;
}
}
public static boolean abgabeMöglich(InputStream inputStream, String[][] data, String[][] colors) throws FileNotFoundException{
String[][] result = getResult(data, colors);
ArrayList<String> filteredData = getSolution(inputStream, result);
String[][] ergebnis = getErgebnisArray(result, filteredData);
boolean abgabeMöglich = checkArraySame(result, ergebnis);
return abgabeMöglich;
}
@ -117,7 +129,7 @@ public class HitoriMain2 extends JFrame implements ActionListener{
return pos;
}
public static void totalResetButton(CardLayout cl, JPanel main, JButton[][] buttons, String[][] colors,Stack<String> madeMoves,String[][] data) throws FileNotFoundException{
public static void totalResetButton(InputStream inputStream, CardLayout cl, JPanel main, JButton[][] buttons, String[][] colors,Stack<String> madeMoves,String[][] data,String path) throws FileNotFoundException{
madeMoves.clear();
for(int i = 0; i<data.length;i++){
for(int j = 0; j<data.length;j++){
@ -125,7 +137,11 @@ public class HitoriMain2 extends JFrame implements ActionListener{
buttons[i][j] = null;
}
JButton[][] buttons0 = makeButtonArray(data);
GameGUI.paintGame(cl, main, filepath, buttons0, colors, madeMoves, data);
try {
GameGUI.paintGame(inputStream, cl, main, buttons0, colors, madeMoves, data, path);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@ -144,27 +160,30 @@ public class HitoriMain2 extends JFrame implements ActionListener{
return result;
}
public static ArrayList<String> getSolution(String path, String[][] result) throws FileNotFoundException{
Scanner sc = new Scanner(new File(path));
public static ArrayList<String> getSolution(InputStream inputStream, String[][] result) throws FileNotFoundException{
ArrayList<String> filteredData = new ArrayList<>();
boolean isUnderComment = false;
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.equals("//Lösung (schwarze Felder)")) {
isUnderComment = true;
continue;
}
if (isUnderComment && !line.isEmpty()) {
String[] lineArray = line.split(",");
int num1 = Integer.parseInt(lineArray[0]);
int num2 = Integer.parseInt(lineArray[1]);
String newNum1 = String.valueOf(num1-1);
String newNum2 = String.valueOf(num2-1);
String newLine = newNum1+","+newNum2;
filteredData.add(newLine);
}
}
sc.close();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.equals("//Lösung (schwarze Felder)")) {
isUnderComment = true;
continue;
}
if (isUnderComment && !line.isEmpty()) {
String[] lineArray = line.split(",");
int num1 = Integer.parseInt(lineArray[0]);
int num2 = Integer.parseInt(lineArray[1]);
String newNum1 = String.valueOf(num1 - 1);
String newNum2 = String.valueOf(num2 - 1);
String newLine = newNum1 + "," + newNum2;
filteredData.add(newLine);
}
}
} catch (IOException e) {
e.getMessage();
}
return filteredData;
}
@ -186,41 +205,61 @@ public class HitoriMain2 extends JFrame implements ActionListener{
return ergebnis;
}
public static void sortByTime(String path) throws FileNotFoundException, IOException{
File file = new File(path);
public static void sortByTime(byte[] data, String filename) throws IOException {
ArrayList<HighscoreEintrag> highscores = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(data), StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(" ", 2);
String timeStr = parts[0];
String name = parts.length > 1 ? parts[1] : "";
String[] timeParts = timeStr.split(":");
int hour = Integer.parseInt(timeParts[0]);
int minute = Integer.parseInt(timeParts[1]);
LocalTime time = LocalTime.of(hour, minute);
highscores.add(new HighscoreEintrag(time, name));
while ((line = reader.readLine()) != null) {
try {
String[] parts = line.split(" ", 2);
String timeStr = parts[0];
String name = (parts.length > 1) ? parts[1].trim() : "";
String[] timeParts = timeStr.split(":");
if (timeParts.length != 2) {
System.err.println("Invalid time format: " + timeStr);
continue;
}
int hour = Integer.parseInt(timeParts[0]);
int minute = Integer.parseInt(timeParts[1]);
if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
System.err.println("Invalid time values: " + timeStr);
continue;
}
LocalTime time = LocalTime.of(hour, minute);
highscores.add(new HighscoreEintrag(time, name));
} catch (NumberFormatException | DateTimeException ex) {
System.err.println("Error parsing line: " + line);
System.err.println("Error details: " + ex.getMessage());
}
}
}
highscores.sort(Comparator.comparing(e -> e.time));
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
for (HighscoreEintrag e : highscores) {
String formattedTime = String.format("%02d:%02d", e.time.getHour(), e.time.getMinute());
bw.write(formattedTime + " " + e.name);
bw.newLine();
highscores.sort(Comparator.comparing(HighscoreEintrag::getTime));
ArrayList<String> formattedHighscores = new ArrayList<>();
for (HighscoreEintrag e : highscores) {
String formattedTime = String.format("%02d:%02d", e.getTime().getHour(), e.getTime().getMinute());
String line = formattedTime + " " + e.getName(); // Keep the name
formattedHighscores.add(line);
}
String fileName = filename.substring(filename.lastIndexOf('/') + 1);
File file = new File(fileName);
overwriteContentOfFile(formattedHighscores, file);
}
private static void overwriteContentOfFile(ArrayList<String> formattedHighscores, File file) throws FileNotFoundException, IOException {
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
for (String entry : formattedHighscores) {
writer.write(entry);
writer.newLine();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
public void actionPerformed(ActionEvent e) {
}
}
@ -232,4 +271,12 @@ class HighscoreEintrag {
this.time = time;
this.name = name;
}
public LocalTime getTime() {
return time;
}
public String getName() {
return name;
}
}

View File

@ -1,39 +1,47 @@
package de.hs_mannheim.informatik.mvn.domain;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Logger;
import java.io.*;
public class LogHighscores {
private static final Logger logger = LogManager.getLogger(LogHighscores.class);
public static void newRecord(String path, String username, String time) throws FileNotFoundException {
public static void newRecord(String resourcePath, String username, String time) throws IOException {
String timePart = time.substring("Zeit: ".length()).trim();
String[] parts = path.split("/");
String filename = parts[parts.length - 1];
int dotIndex = filename.lastIndexOf(".");
String result = filename.substring(0, dotIndex);
String ordner = "Hitori_Highscores/";
String filetype = ".txt";
String filepath = ordner + result + filetype;
System.out.println("A: " + filepath);
Scanner sc = new Scanner(filepath);
while (sc.hasNextLine()) {
sc.nextLine();
}
sc.close();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filepath, true))) {
String eintrag = timePart + " " + username;
logger.info("Neuer Eintrag: {}", eintrag);
String[] parts = resourcePath.split("/");
String jarFileName = parts[parts.length - 1];
String newName = jarFileName.replace(".csv", ".txt");
writer.write(eintrag);
File localFolder = new File("resources/Hitori_Highscores");
if (!localFolder.exists() && !localFolder.mkdirs()) {
logger.error("Failed to create output folder: {}", localFolder.getAbsolutePath());
throw new IOException("Failed to create output folder: " + localFolder);
}
File localFile = new File(localFolder, newName);
try (InputStream inputStream = LogHighscores.class.getResourceAsStream(resourcePath)) {
if (inputStream == null) {
logger.warn("Resource not found in JAR: {}", resourcePath);
} else {
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
logger.debug("Existing JAR record: {}", line);
}
}
}
}
String entry = timePart + " " + username;
logger.info("Adding new record: {} to file: {}", entry, localFile.getAbsolutePath());
try (BufferedWriter writer = new BufferedWriter(new FileWriter(localFile, true))) {
writer.write(entry);
writer.newLine();
} catch (IOException e) {
logger.error("Fehler beim Schreiben in die Datei: " + filepath, e);
logger.error("Error handling local highscore file: {}", localFile.getAbsolutePath(), e);
throw e;
}
}
}

View File

@ -1,23 +1,13 @@
package de.hs_mannheim.informatik.mvn.gui;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.awt.*;
import java.awt.event.*;
import java.util.EmptyStackException;
import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.util.TimerTask;
import java.io.*;
import javax.swing.*;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain2;
import de.hs_mannheim.informatik.mvn.domain.LogHighscores;
@ -26,9 +16,13 @@ public class GameGUI extends JFrame implements ActionListener {
private static Timer timer = new Timer();
private static long startTime;
private static long elapsedSeconds;
public static void paintGame(CardLayout cl, JPanel main, String[] filepath, JButton[][] buttons, String[][] colors, Stack<String> madeMoves, String[][] data) throws FileNotFoundException{
private static byte[] gameData;
public static void paintGame(InputStream inputStream, CardLayout cl, JPanel main, JButton[][] buttons, String[][] colors, Stack<String> madeMoves, String[][] data, String path0) throws IOException{
elapsedSeconds = 0;
gameData = inputStream.readAllBytes();
String[] filepath = new String[2];
int num = buttons.length;
JPanel gameGrid = new JPanel(new GridLayout(num,num,0,0));
for(int i=0;i<num;i++){
@ -57,12 +51,11 @@ public class GameGUI extends JFrame implements ActionListener {
b1.addActionListener(e -> {backOneStep(cl, main, madeMoves, buttons, colors, gameGrid);});
JButton b2 = new JButton("Zurücksetzen");
b2.addActionListener(e -> {try {
HitoriMain2.totalResetButton(cl, main, buttons, colors, madeMoves, data);
HitoriMain2.totalResetButton(inputStream, cl, main, buttons, colors, madeMoves, data, path0);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}});
JButton b3 = new JButton("Abgeben");
String path = filepath[0];
boolean[] levelFinished = {false};
JPanel topGrid = new JPanel(new GridLayout(1,2,10,10));
mainPanel.add(topGrid, BorderLayout.NORTH);
@ -70,7 +63,7 @@ public class GameGUI extends JFrame implements ActionListener {
topGrid.add(timeLabel);
topGrid.revalidate();
topGrid.repaint();
boolean isOkay = HitoriMain2.abgabeMöglich(path, data, colors);
boolean isOkay = HitoriMain2.abgabeMöglich(inputStream, data, colors);
levelFinished[0] = isOkay;
Timer timer = new Timer();
startTimer();
@ -78,7 +71,7 @@ public class GameGUI extends JFrame implements ActionListener {
@Override
public void run() {
elapsedSeconds = (System.currentTimeMillis() - startTime) / 1000;
long minutes = (elapsedSeconds % 3600) / 60;
long minutes = elapsedSeconds % 3600 / 60;
long seconds = elapsedSeconds % 60;
String newTime = String.format("Zeit: %02d:%02d", minutes, seconds);
@ -95,22 +88,20 @@ public class GameGUI extends JFrame implements ActionListener {
b3.addActionListener(e -> {
try {
levelFinished[0] = HitoriMain2.abgabeMöglich(path, data, colors);
System.out.println("0000: " + levelFinished[0]);
InputStream newStream = new ByteArrayInputStream(gameData);
levelFinished[0] = HitoriMain2.abgabeMöglich(newStream, data, colors);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
if (levelFinished[0] == true) {
System.out.println("1111: " + levelFinished[0]);
String endtime = stopTimer();
timer.cancel();
try {
finish(cl, main, endtime, filepath, path, data, colors);
finish(cl, main, endtime, filepath, path0);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
} else {
System.out.println("2222: " + levelFinished[0]);
luecke.setText("Abgabe nicht richtig!");
mainPanel.revalidate();
mainPanel.repaint();
@ -218,7 +209,7 @@ public class GameGUI extends JFrame implements ActionListener {
gridUpdate(grid, buttons);
}
} catch(EmptyStackException e) {
System.out.println("Fehler: " + e.getMessage());
e.getStackTrace();
}
}
@ -242,14 +233,14 @@ public class GameGUI extends JFrame implements ActionListener {
public static String stopTimer() {
timer.cancel();
long minutes = (elapsedSeconds % 3600) / 60;
long minutes = elapsedSeconds % 3600 / 60;
long seconds = elapsedSeconds % 60;
String endtime = String.format("Zeit: %02d:%02d", minutes, seconds);
elapsedSeconds = 0; // Reset so the next start begins at zero
return endtime;
}
public static void finish(CardLayout cl, JPanel main, String endtime, String[] filepath, String path, String[][] data, String[][] colors) throws FileNotFoundException{
public static void finish(CardLayout cl, JPanel main, String endtime, String[] filepath, String path) throws FileNotFoundException{
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel text = new JLabel("Geben Sie unten Ihren Namen an um sich in der Highscore Liste einzutragen.");
mainPanel.add(text, BorderLayout.NORTH);
@ -261,13 +252,13 @@ public class GameGUI extends JFrame implements ActionListener {
mainPanel.setSize(600,600);
b.addActionListener(e -> {
String username = field.getText();
try {
try {
LogHighscores.newRecord(path, username, endtime);
filepath[0] = "";
filepath[1] = "";
cl.show(main, "HAUPT");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.getMessage();
}
});
main.add(mainPanel, "HIGHSCORENEU");
@ -275,8 +266,6 @@ public class GameGUI extends JFrame implements ActionListener {
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
public void actionPerformed(ActionEvent e) {
}
}

View File

@ -1,4 +1,3 @@
package de.hs_mannheim.informatik.mvn.gui;
import java.awt.BorderLayout;
@ -6,11 +5,18 @@ import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
@ -19,102 +25,135 @@ import de.hs_mannheim.informatik.mvn.domain.HitoriMain2;
public class HighscoreGUI extends JFrame implements ActionListener {
public static void highscoreScreen(CardLayout cl, JPanel main){
final String[] paths = {
"resources/Hitori_Highscores/Hitori4x4_leicht.txt",
"resources/Hitori_Highscores/Hitori5x5leicht.txt",
"resources/Hitori_Highscores/Hitori8x8leicht.txt",
"resources/Hitori_Highscores/Hitori8x8medium.txt",
"resources/Hitori_Highscores/Hitori10x10medium.txt",
"resources/Hitori_Highscores/Hitori15x15_medium.txt"
};
final String[] buttons = {
"Highscore 4x4 - leicht",
"Highscore 5x5 - leicht",
"Highscore 8x8 - leicht",
"Highscore 8x8 - medium",
"Highscore 10x10 - medium",
"Highscore 15x15 - medium"
};
public HighscoreGUI(CardLayout cl, JPanel main) {
JPanel highscorePanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new GridLayout(7,1,10,10));
String[] buttons = {
"Highscore 4x4 - leicht",
"Highscore 5x5 - leicht",
"Highscore 8x8 - leicht",
"Highscore 8x8 - medium",
"Highscore 10x10 - medium",
"Highscore 15x15 - medium"
};
for(int i=0;i<buttons.length;i++){
JPanel buttonPanel = new JPanel(new GridLayout(7, 1, 10, 10));
for (int i = 0; i < buttons.length; i++) {
JButton b0 = new JButton(buttons[i]);
buttonPanel.add(b0);
int[] count = {i};
final int index = i;
b0.addActionListener(e -> {
int j = count[0];
String[] paths = {
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Highscores/Hitori4x4_leicht.txt",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Highscores/Hitori5x5leicht.txt",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Highscores/Hitori8x8leicht.txt",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Highscores/Hitori8x8medium.txt",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Highscores/Hitori10x10medium.txt",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Highscores/Hitori15x15_medium.txt"
};
String resourcePathInJar = paths[index];
String localFileName = resourcePathInJar.substring(resourcePathInJar.lastIndexOf('/') + 1);
File outFile = new File("resources/Hitori_Highscores", localFileName);
try {
showHighscores(cl, main, paths[j]);
} catch (FileNotFoundException e0) {
e0.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
copyResourceIfNotExists(resourcePathInJar, outFile);
byte[] data = Files.readAllBytes(outFile.toPath());
showHighscores(cl, main, data, outFile.getPath());
} catch (IOException ex1) {
ex1.printStackTrace();
empty(cl, main);
}
});
}
JButton b = new JButton("Zurück");
b.addActionListener(e -> {
cl.show(main, "HAUPT");
});
b.addActionListener(e -> cl.show(main, "HAUPT"));
buttonPanel.add(b);
highscorePanel.setVisible(true);
highscorePanel.setSize(600,600);
highscorePanel.setSize(600, 600);
highscorePanel.add(buttonPanel, BorderLayout.CENTER);
JLabel text0 = new JLabel("Level für Highscore Liste auswählen!");
highscorePanel.add(text0, BorderLayout.NORTH);
main.add(highscorePanel, "HIGHSCORES");
main.add(highscorePanel, "HIGHSCORES");
cl.show(main, "HIGHSCORES");
}
public static void showHighscores(CardLayout cl, JPanel main, String path) throws FileNotFoundException, IOException{
HitoriMain2.sortByTime(path);
JPanel mainPanel = new JPanel(new BorderLayout());
File file = new File(path);
int i = 0;
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
if(scanner.nextLine() != "") {
i++;
private static void copyResourceIfNotExists(String resourcePathInJar, File outFile) throws IOException {
if (outFile.exists()) {
return;
}
outFile.getParentFile().mkdirs();
try (InputStream in = HighscoreGUI.class.getResourceAsStream(resourcePathInJar);
FileOutputStream fos = new FileOutputStream(outFile)) {
if (in == null) {
throw new FileNotFoundException("Resource not found in JAR: " + resourcePathInJar);
}
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
}
}
public static void empty(CardLayout cl, JPanel main) {
JPanel panel = new JPanel(new BorderLayout());
JButton b = new JButton("Zurück");
JLabel text = new JLabel("Noch kein Highscore eingetragen.");
panel.add(text, BorderLayout.CENTER);
panel.add(b, BorderLayout.SOUTH);
b.addActionListener(e -> cl.show(main, "HIGHSCORES"));
main.add(panel, "EMPTY");
cl.show(main, "EMPTY");
}
public static void showHighscores(CardLayout cl, JPanel main, byte[] data, String filename) throws IOException {
HitoriMain2.sortByTime(data, filename);
data = Files.readAllBytes(new File(filename).toPath());
JPanel highscorePanel1 = new JPanel(new BorderLayout());
List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(data), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
boolean hasContent = false;
for (String l : lines) {
if (!l.trim().isEmpty()) {
hasContent = true;
break;
}
}
if (hasContent) {
JPanel entryPanel = new JPanel(new GridLayout(lines.size(), 1, 10, 10));
for (String s : lines) {
if (!s.trim().isEmpty()) {
JLabel text = new JLabel(s);
entryPanel.add(text);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
JPanel highscorePanel = new JPanel(new GridLayout(i,1,10,10));
if(i>0){
ArrayList<String> lines = HitoriMain2.readFromFile(path);
for(String s: lines){
JLabel text = new JLabel(s);
highscorePanel.add(text);
}
JButton b = new JButton("Zurück");
b.addActionListener(e -> {
cl.show(main, "HIGHSCORES");
highscoreScreen(cl, main);
});
mainPanel.add(b, BorderLayout.SOUTH);
highscorePanel1.add(entryPanel, BorderLayout.CENTER);
} else {
JLabel text = new JLabel("Noch kein Highscore eingetragen.");
highscorePanel.add(text);
JButton b = new JButton("Zurück");
b.addActionListener(e -> {
cl.show(main, "HIGHSCORES");
highscoreScreen(cl, main);
});
mainPanel.add(b, BorderLayout.SOUTH);
highscorePanel1.add(text, BorderLayout.CENTER);
}
mainPanel.add(highscorePanel, BorderLayout.CENTER);
mainPanel.setVisible(true);
mainPanel.setSize(600,600);
main.add(mainPanel, "HIGHSCORES");
cl.show(main, "HIGHSCORES");
JButton b = new JButton("OK");
highscorePanel1.add(b, BorderLayout.SOUTH);
b.addActionListener(e -> cl.show(main, "HIGHSCORES"));
highscorePanel1.setVisible(true);
highscorePanel1.setSize(600, 600);
main.add(highscorePanel1, "HIGHSCOREEINTRAG");
cl.show(main, "HIGHSCOREEINTRAG");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
}
}

View File

@ -5,9 +5,9 @@ import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
@ -23,7 +23,7 @@ public class MenuGUI extends JFrame implements ActionListener {
public MenuGUI(){
JPanel menuPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new GridLayout(7,1,10,10));
String[] buttons = {
final String[] buttons = {
"4x4 - leicht",
"5x5 - leicht",
"8x8 - leicht",
@ -31,6 +31,14 @@ public class MenuGUI extends JFrame implements ActionListener {
"10x10 - medium",
"15x15 - medium"
};
final String[] paths = {
"/Hitori_Spielfelder/Hitori4x4_leicht.csv",
"/Hitori_Spielfelder/Hitori5x5leicht.csv",
"/Hitori_Spielfelder/Hitori8x8leicht.csv",
"/Hitori_Spielfelder/Hitori8x8medium.csv",
"/Hitori_Spielfelder/Hitori10x10medium.csv",
"/Hitori_Spielfelder/Hitori15x15_medium.csv"
};
for(int i=0;i<buttons.length;i++){
JButton b0 = new JButton(buttons[i]);
buttonPanel.add(b0);
@ -38,41 +46,23 @@ public class MenuGUI extends JFrame implements ActionListener {
String[] num = buttons[i].split("x");
b0.addActionListener(e -> {
int j = count[0];
String currentDirectory = Paths.get("").toAbsolutePath().toString();
System.out.println("Current Working Directory: " + currentDirectory);
String[] paths = {
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori4x4_leicht.csv",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori5x5leicht.csv",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori8x8leicht.csv",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori8x8medium.csv",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori10x10medium.csv",
"src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori15x15_medium.csv"
};
// /Users/beratkocak/git/PR2Projekt/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn
for (String path : paths) {
File file = new File(path);
if (file.exists()) {
System.out.println("File found: " + file.getAbsolutePath());
} else {
System.out.println("File not found: " + file.getAbsolutePath());
}
}
filepath[0] = paths[j];
filepath[1] = num[0];
try{
HitoriMain2.ablauf(cl, main, filepath);
} catch (FileNotFoundException s){
System.out.println("Fehler: " + s.getMessage());
}
});
filepath[1] = num[0];
try (InputStream inputStream = getClass().getResourceAsStream(filepath[0])) {
if (inputStream != null) {
String path=filepath[0];
HitoriMain2.ablauf(cl, main, inputStream, Integer.parseInt(filepath[1]), path);
} else {
throw new FileNotFoundException("Resource not found: " + filepath[0]);
}
} catch (IOException e1) {
e1.getMessage();
}
});
}
JButton b = new JButton("Highscores");
b.addActionListener(e -> {
HighscoreGUI.highscoreScreen(cl, main);
new HighscoreGUI(cl, main);
});
buttonPanel.add(b);
menuPanel.add(buttonPanel, BorderLayout.CENTER);
@ -87,8 +77,6 @@ public class MenuGUI extends JFrame implements ActionListener {
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
public void actionPerformed(ActionEvent e) {
}
}

View File

@ -0,0 +1,12 @@
00:02 paul
00:02 paul
00:02 thomas
00:02 thomas
00:02 dfopwahgfa
00:02 dfopwahgfa
00:02 fwagfawgagagas
00:02 fwagfawgagagas
00:03 fafaadxc
00:03 fafaadxc
00:04 haveaqniucelife
00:04 haveaqniucelife

View File

@ -7,6 +7,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
@ -52,13 +54,7 @@ class HitoriTest{
{"W", "W", "W", "B"},
{"W", "B", "W", "W"},
{"B", "W", "W", "B"}};
String path = "src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori4x4_leicht.csv";
try {
assertFalse(HitoriMain2.abgabeMöglich(path, data, colors));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assertFalse(HitoriMain2.checkArraySame(data, colors));
}
@Test
@ -73,12 +69,8 @@ class HitoriTest{
{"W", "W", "W", "B"},
{"W", "B", "W", "W"},
{"B", "W", "W", "B"}};
String path = "src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori4x4_leicht.csv";
try {
assertTrue(HitoriMain2.abgabeMöglich(path, data, colors));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assertTrue(HitoriMain2.checkArraySame(data, colors));
}
@Test
@ -95,12 +87,7 @@ class HitoriTest{
{"W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W"}};
String path = "src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori5x5leicht.csv";
try {
assertFalse(HitoriMain2.abgabeMöglich(path, data, colors));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assertFalse(HitoriMain2.checkArraySame(data, colors));
}
@Test
@ -117,12 +104,7 @@ class HitoriTest{
{"B", "W", "W", "B", "W"},
{"W", "W", "B", "W", "W"},
{"B", "W", "W", "W", "B"}};
String path = "src/main/java/de/hs_mannheim/informatik/mvn/resources/Hitori_Spielfelder/Hitori5x5leicht.csv";
try {
assertTrue(HitoriMain2.abgabeMöglich(path, data, colors));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assertTrue(HitoriMain2.checkArraySame(data, colors));
}
@Test
@ -136,4 +118,58 @@ class HitoriTest{
String[] array = {"3", "4"};
assertTrue(Arrays.deepEquals(array, HitoriMain2.getCords(3, 4)));
}
@Test
void test10() throws FileNotFoundException {
String[][] data = {
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "B", "W", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "B", "W"},
{"W", "W", "W", "W", "W", "W", "W", "B", "W", "B"},
{"B", "W", "B", "W", "B", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "W", "B", "W", "W", "W", "B", "W"},
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "W"},
{"W", "W", "W", "B", "W", "W", "B", "W", "B", "W"},
{"B", "W", "B", "W", "W", "W", "W", "W", "W", "B"}};
String[][] colors = {
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "B", "W", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "B", "W"},
{"W", "W", "W", "W", "W", "W", "W", "B", "W", "B"},
{"B", "W", "B", "W", "B", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "W", "B", "W", "W", "W", "B", "W"},
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "W"},
{"W", "W", "W", "B", "W", "W", "B", "W", "B", "W"},
{"B", "W", "B", "W", "W", "W", "W", "W", "W", "B"}};
assertTrue(HitoriMain2.checkArraySame(data, colors));
}
@Test
void test11() {
String[][] data = {
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "B", "W", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "B", "W"},
{"W", "W", "W", "W", "W", "W", "W", "B", "W", "B"},
{"B", "W", "B", "W", "B", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "W", "B", "W", "W", "W", "B", "W"},
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "W"},
{"W", "W", "W", "B", "W", "W", "B", "W", "B", "W"},
{"B", "W", "B", "W", "W", "W", "W", "W", "W", "B"}};
String[][] colors = {
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "B", "W", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "B", "W"},
{"W", "W", "W", "W", "W", "W", "W", "B", "W", "B"},
{"B", "W", "B", "W", "B", "W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "W", "B", "W", "W", "W", "B", "W"},
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "W"},
{"W", "W", "W", "B", "W", "W", "B", "W", "B", "W"},
{"B", "W", "B", "W", "W", "W", "W", "W", "W", "W"}};
assertFalse(HitoriMain2.checkArraySame(data, colors));
}
}