updated file name, gave variable verbose names, simplified code

main
Berat Kocak 2025-01-10 10:40:07 +01:00
commit a45a306cb9
8 changed files with 260 additions and 330 deletions

View File

@ -1,40 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<!-- JRE System Library -->
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<!-- JUnit Library -->
<!-- JUnit 5 Library -->
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<!-- Main Source Folder -->
<classpathentry kind="src" output="target/classes" path="src/main/java">
<!-- Source Folders -->
<classpathentry kind="src" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
<output>target/classes</output>
</classpathentry>
<!-- Test Source Folder -->
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<classpathentry kind="src" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
<output>target/test-classes</output>
</classpathentry>
<!-- Test Resources -->
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/main/java/resources">
<classpathentry kind="src" path="src/main/java/resources" excluding="**">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
<output>target/test-classes</output>
</classpathentry>
<!-- Maven Dependencies -->
@ -44,42 +43,6 @@
</attributes>
</classpathentry>
<!-- Output Folder -->
<!-- Output Directory -->
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<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"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="lib" path="src/main/java/de/hs_mannheim/informatik/mvn/domain/HitoriMain2.java"/>
<classpathentry kind="output" path="bin"/>
</classpathentry>
</classpath>

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB

View File

@ -50,11 +50,6 @@
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<archive>
<manifest>
<mainClass>de.hs_mannheim.informatik.mvn.HitoriMain2</mainClass>
</manifest>
</archive>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
@ -66,7 +61,7 @@
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>src.main.java</mainClass>
<mainClass>de.hs_mannheim.informatik.mvn.domain.HitoriMain</mainClass>
</transformer>
</transformers>
</configuration>
@ -118,7 +113,7 @@
<directory>src/test/java</directory>
</resource>
<resource>
<directory>src/main/java/resources</directory>
<directory>src/main/resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>

View File

@ -1,8 +1,6 @@
package de.hs_mannheim.informatik.mvn.domain;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
@ -28,7 +26,7 @@ import javax.swing.JPanel;
import java.time.format.DateTimeFormatterBuilder;
import de.hs_mannheim.informatik.mvn.gui.*;
public class HitoriMain2 extends JFrame implements ActionListener{
public class HitoriMain {
private static byte[] gameData;
@ -43,8 +41,8 @@ public class HitoriMain2 extends JFrame implements ActionListener{
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);
InputStream gameStream = new ByteArrayInputStream(gameData);
GameGUI.paintGame(gameStream, cl, main, buttons, colors, madeMoves, data, path);
}
public static ArrayList<String> readFromFile(InputStream inputStream){
@ -90,8 +88,8 @@ public class HitoriMain2 extends JFrame implements ActionListener{
for(int i=0;i<data.length;i++){
for(int j=0;j<data.length;j++){
String number = data[i][j];
JButton b = new JButton(number);
buttons[i][j] = b;
JButton button = new JButton(number);
buttons[i][j] = button;
}
}
return buttons;
@ -128,20 +126,20 @@ public class HitoriMain2 extends JFrame implements ActionListener{
public static String[] getCords(int i, int j){
String yKoordinate = String.valueOf(i);
String xKoordinate = String.valueOf(j);
String[] pos = {yKoordinate, xKoordinate};
return pos;
String[] position = {yKoordinate, xKoordinate};
return position;
}
public static void totalResetButton(InputStream inputStream, CardLayout cl, JPanel main, JButton[][] buttons, String[][] colors,Stack<String> madeMoves,String[][] data,String path) throws FileNotFoundException{
public static void totalResetButton(InputStream inputStream, CardLayout cl, JPanel main, JButton[][] buttonArray, 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++){
colors[i][j] = "W";
buttons[i][j] = null;
buttonArray[i][j] = null;
}
JButton[][] buttons0 = makeButtonArray(data);
JButton[][] buttons = makeButtonArray(data);
try {
GameGUI.paintGame(inputStream, cl, main, buttons0, colors, madeMoves, data, path);
GameGUI.paintGame(inputStream, cl, main, buttons, colors, madeMoves, data, path);
} catch (IOException e) {
e.printStackTrace();
}
@ -176,11 +174,11 @@ public class HitoriMain2 extends JFrame implements ActionListener{
}
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;
int firstNumn = Integer.parseInt(lineArray[0]);
int secondNum = Integer.parseInt(lineArray[1]);
String firstNumAsString = String.valueOf(firstNumn - 1);
String secondNumAsString = String.valueOf(secondNum - 1);
String newLine = firstNumAsString + "," + secondNumAsString;
filteredData.add(newLine);
}
}
@ -208,6 +206,15 @@ public class HitoriMain2 extends JFrame implements ActionListener{
return ergebnis;
}
public static void printArray(String[][] ergebnis) {
for(int i=0;i<ergebnis.length;i++) {
for(int j=0;j<ergebnis.length;j++) {
System.out.print(" " + ergebnis[i][j]);
}
System.out.println();
}
}
public static void sortByTime(byte[] data, String filename) throws IOException {
ArrayList<HighscoreEintrag> highscores = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(
@ -260,21 +267,16 @@ public class HitoriMain2 extends JFrame implements ActionListener{
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
}
public static JLabel getAvgTime(byte[] data, String filename) throws IOException {
String text = "Durchschnittszeit: ";
public static String getAvgTime(byte[] data, String filename) throws IOException {
int totalSeconds = 0;
int count = 0;
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(data), StandardCharsets.UTF_8))) {
String line;
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("mm:ss") // we only have minutes and seconds
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0) // default hour = 0
.appendPattern("mm:ss")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.toFormatter();
while ((line = reader.readLine()) != null) {
line = line.trim();
@ -291,9 +293,8 @@ public class HitoriMain2 extends JFrame implements ActionListener{
int timeInt = totalSeconds/count;
int minutes = timeInt/60;
int seconds = timeInt%60;
String time= minutes+":"+seconds;
JLabel avgTime = new JLabel(text + time);
return avgTime;
String timeString= "Durchschnittszeit: " + minutes+":"+seconds;
return timeString;
}
}
}

View File

@ -7,8 +7,9 @@ import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import javax.swing.*;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain2;
import javax.swing.*;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain;
import de.hs_mannheim.informatik.mvn.domain.LogHighscores;
public class GameGUI extends JFrame implements ActionListener {
@ -34,28 +35,28 @@ public class GameGUI extends JFrame implements ActionListener {
b.setFocusPainted(false);
b.setBackground(Color.WHITE);
gameGrid.add(b);
String[] pos = HitoriMain2.getCords(i,j);
String[] pos = HitoriMain.getCords(i,j);
b.addActionListener(e -> {paintButton(cl, main, b, pos, colors , madeMoves);});
}
}
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(gameGrid, BorderLayout.CENTER);
JPanel buttonGrid = new JPanel(new GridLayout(1,4,10,10));
JButton b0 = new JButton("Aufgeben");
b0.addActionListener(e -> {
JButton aufgebenButton = new JButton("Aufgeben");
aufgebenButton.addActionListener(e -> {
filepath[0] = "";
filepath[1] = "";
cl.show(main, "HAUPT");
});
JButton b1 = new JButton("Zurück");
b1.addActionListener(e -> {backOneStep(cl, main, madeMoves, buttons, colors, gameGrid);});
JButton b2 = new JButton("Zurücksetzen");
b2.addActionListener(e -> {try {
HitoriMain2.totalResetButton(inputStream, cl, main, buttons, colors, madeMoves, data, path0);
JButton zurückButton = new JButton("Zurück");
zurückButton.addActionListener(e -> {backOneStep(cl, main, madeMoves, buttons, colors, gameGrid);});
JButton resetButton = new JButton("Zurücksetzen");
resetButton.addActionListener(e -> {try {
HitoriMain.totalResetButton(inputStream, cl, main, buttons, colors, madeMoves, data, path0);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}});
JButton b3 = new JButton("Abgeben");
JButton abgebenButton = new JButton("Abgeben");
boolean[] levelFinished = {false};
JPanel topGrid = new JPanel(new GridLayout(1,2,10,10));
mainPanel.add(topGrid, BorderLayout.NORTH);
@ -63,7 +64,7 @@ public class GameGUI extends JFrame implements ActionListener {
topGrid.add(timeLabel);
topGrid.revalidate();
topGrid.repaint();
boolean isOkay = HitoriMain2.abgabeMöglich(inputStream, data, colors);
boolean isOkay = HitoriMain.abgabeMöglich(inputStream, data, colors);
levelFinished[0] = isOkay;
Timer timer = new Timer();
startTimer();
@ -82,14 +83,12 @@ public class GameGUI extends JFrame implements ActionListener {
});
}
}, 0, 1000);
JLabel luecke = new JLabel("");
topGrid.add(luecke);
b3.addActionListener(e -> {
JLabel platzhalter = new JLabel("");
topGrid.add(platzhalter);
abgebenButton.addActionListener(e -> {
try {
InputStream newStream = new ByteArrayInputStream(gameData);
levelFinished[0] = HitoriMain2.abgabeMöglich(newStream, data, colors);
levelFinished[0] = HitoriMain.abgabeMöglich(newStream, data, colors);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
@ -102,115 +101,81 @@ public class GameGUI extends JFrame implements ActionListener {
e1.printStackTrace();
}
} else {
luecke.setText("Abgabe nicht richtig!");
platzhalter.setText("Abgabe nicht richtig!");
mainPanel.revalidate();
mainPanel.repaint();
}
});
buttonGrid.add(b0);buttonGrid.add(b1);buttonGrid.add(b2);buttonGrid.add(b3);
buttonGrid.add(aufgebenButton);
buttonGrid.add(zurückButton);
buttonGrid.add(resetButton);
buttonGrid.add(abgebenButton);
mainPanel.add(buttonGrid, BorderLayout.SOUTH);
mainPanel.setVisible(true);
main.add(mainPanel, "GAME");
cl.show(main, "GAME");
}
public static void paintButton(CardLayout cl, JPanel main, JButton b, String[] pos, String[][] colors, Stack<String> madeMoves){
public static void paintButton(CardLayout cl, JPanel main, JButton button, String[] pos, String[][] colors, Stack<String> madeMoves){
int i = Integer.parseInt(pos[0]);
int j = Integer.parseInt(pos[1]);
String col = colors[Integer.parseInt(pos[0])][Integer.parseInt(pos[1])];
if(col.endsWith("W")){
b.setOpaque(true);
b.setForeground(Color.BLACK);
b.setContentAreaFilled(true);
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setBackground(Color.lightGray);
colors[i][j] += "G";
String logEntrance = i+"."+j+"."+"G";
madeMoves.push(logEntrance);
}
if(col.endsWith("G")){
b.setOpaque(true);
b.setForeground(Color.WHITE);
b.setContentAreaFilled(true);
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setBackground(Color.BLACK);
colors[i][j] += "B";
String logEntrance = i+"."+j+"."+"B";
madeMoves.push(logEntrance);
}
if(col.endsWith("B")){
b.setOpaque(true);
b.setForeground(Color.BLACK);
b.setContentAreaFilled(true);
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setBackground(Color.WHITE);
colors[i][j] += "W";
String logEntrance = i+"."+j+"."+"W";
madeMoves.push(logEntrance);
}
String col = colors[i][j];
String lastLetterString = String.valueOf(col.charAt(col.length()-1));
button.setOpaque(true);
button.setContentAreaFilled(true);
button.setBorderPainted(false);
button.setFocusPainted(false);
switch(lastLetterString) {
case "W":
button.setForeground(Color.BLACK);
button.setBackground(Color.lightGray);
break;
case "G":
button.setForeground(Color.WHITE);
button.setBackground(Color.BLACK);
break;
case "B":
button.setForeground(Color.BLACK);
button.setBackground(Color.WHITE);
break;
default:
System.exit(0);
}
colors[i][j] += lastLetterString;
String newMove = i+"."+j+"."+lastLetterString;
madeMoves.push(newMove);
}
public static void backOneStep(CardLayout cl, JPanel main, Stack<String> movesMade, JButton[][] buttons, String[][] colors, JPanel grid){
try {
String move = movesMade.pop();
String[] line = move.split("\\.");
String y = line[0];
String x = line[1];
int i = Integer.parseInt(line[0]);
int j = Integer.parseInt(line[1]);
String color = line[2];
if(color.equals("W")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.WHITE);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.BLACK);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
grid.repaint();
gridUpdate(grid, buttons);
}else if(color.equals("G")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.BLACK);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.WHITE);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
gridUpdate(grid, buttons);
} else if(color.equals("B")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.BLACK);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.lightGray);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
gridUpdate(grid, buttons);
}
} catch(EmptyStackException e) {
e.getStackTrace();
}
JButton button = buttons[i][j];
button.setOpaque(true);
button.setContentAreaFilled(true);
button.setBorderPainted(false);
button.setFocusPainted(false);
switch(color) {
case "W":
button.setForeground(Color.WHITE);
button.setBackground(Color.BLACK);
break;
case "G":
button.setForeground(Color.BLACK);
button.setBackground(Color.WHITE);
break;
case "B":
button.setForeground(Color.BLACK);
button.setBackground(Color.lightGray);
break;
}
String colorString = colors[i][j];
String colorStringWithoutLastElement = colorString.substring(0, colorString.length() - 1);
colors[i][j] = colorStringWithoutLastElement;
grid.repaint();
gridUpdate(grid, buttons);
}
public static void gridUpdate(JPanel grid, JButton[][] buttons){
@ -218,8 +183,8 @@ public class GameGUI extends JFrame implements ActionListener {
grid.repaint();
for(int i = 0; i<buttons.length; i++){
for(int j = 0; j<buttons.length;j++){
JButton b0 = buttons[i][j];
grid.add(b0);
JButton button = buttons[i][j];
grid.add(button);
grid.repaint();
}
}
@ -236,21 +201,21 @@ public class GameGUI extends JFrame implements ActionListener {
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
elapsedSeconds = 0;
return endtime;
}
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);
JLabel topText = new JLabel("Geben Sie unten Ihren Namen an um sich in der Highscore Liste einzutragen.");
mainPanel.add(topText, BorderLayout.NORTH);
JTextField field = new JTextField(20);
mainPanel.add(field, BorderLayout.CENTER);
JButton b = new JButton("In der Highscore Liste eintragen");
mainPanel.add(b, BorderLayout.SOUTH);
JButton addHighscoreButton = new JButton("In der Highscore Liste eintragen");
mainPanel.add(addHighscoreButton, BorderLayout.SOUTH);
mainPanel.setVisible(true);
mainPanel.setSize(600,600);
b.addActionListener(e -> {
addHighscoreButton.addActionListener(e -> {
String username = field.getText();
try {
LogHighscores.newRecord(path, username, endtime);

View File

@ -21,7 +21,8 @@ import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain2;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain;
public class HighscoreGUI extends JFrame implements ActionListener {
@ -46,12 +47,11 @@ public class HighscoreGUI extends JFrame implements ActionListener {
public HighscoreGUI(CardLayout cl, JPanel main) {
JPanel highscorePanel = new JPanel(new BorderLayout());
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);
JButton button = new JButton(buttons[i]);
buttonPanel.add(button);
final int index = i;
b0.addActionListener(e -> {
button.addActionListener(e -> {
String resourcePathInJar = paths[index];
String localFileName = resourcePathInJar.substring(resourcePathInJar.lastIndexOf('/') + 1);
File outFile = new File("resources/Hitori_Highscores", localFileName);
@ -59,20 +59,20 @@ public class HighscoreGUI extends JFrame implements ActionListener {
copyResourceIfNotExists(resourcePathInJar, outFile);
byte[] data = Files.readAllBytes(outFile.toPath());
showHighscores(cl, main, data, outFile.getPath());
} catch (IOException ex1) {
ex1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
empty(cl, main);
}
});
}
JButton b = new JButton("Zurück");
b.addActionListener(e -> cl.show(main, "HAUPT"));
buttonPanel.add(b);
JButton zurückButton = new JButton("Zurück");
zurückButton.addActionListener(e -> cl.show(main, "HAUPT"));
buttonPanel.add(zurückButton);
highscorePanel.setVisible(true);
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);
JLabel topText = new JLabel("Level für Highscore Liste auswählen!");
highscorePanel.add(topText, BorderLayout.NORTH);
main.add(highscorePanel, "HIGHSCORES");
cl.show(main, "HIGHSCORES");
}
@ -97,19 +97,19 @@ public class HighscoreGUI extends JFrame implements ActionListener {
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"));
JButton zurückButton = new JButton("Zurück");
JLabel topText = new JLabel("Noch kein Highscore eingetragen.");
panel.add(topText, BorderLayout.CENTER);
panel.add(zurückButton);
zurückButton.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);
HitoriMain.sortByTime(data, filename);
data = Files.readAllBytes(new File(filename).toPath());
JPanel highscorePanel1 = new JPanel(new BorderLayout());
JPanel highscorePanel = new JPanel(new BorderLayout());
List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(data), StandardCharsets.UTF_8))) {
@ -120,38 +120,21 @@ public class HighscoreGUI extends JFrame implements ActionListener {
} catch (IOException e) {
e.printStackTrace();
}
boolean hasContent = false;
for (String l : lines) {
if (!l.trim().isEmpty()) {
hasContent = true;
break;
}
JPanel entryPanel = new JPanel(new GridLayout(lines.size(), 1, 10, 10));
for (String s : lines) {
JLabel text = new JLabel(s);
entryPanel.add(text);
}
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);
}
}
highscorePanel1.add(entryPanel, BorderLayout.CENTER);
JLabel avgTime = HitoriMain2.getAvgTime(data, filename);
highscorePanel1.add(avgTime, BorderLayout.NORTH);
} else {
JLabel text = new JLabel("Noch kein Highscore eingetragen.");
highscorePanel1.add(text, BorderLayout.CENTER);
}
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");
highscorePanel.add(entryPanel, BorderLayout.CENTER);
String avgTime = HitoriMain.getAvgTime(data, filename);
JLabel avgTimeJLabel = new JLabel(avgTime);
highscorePanel.add(avgTimeJLabel, BorderLayout.NORTH);
JButton okButton = new JButton("OK");
highscorePanel.add(okButton, BorderLayout.SOUTH);
okButton.addActionListener(e -> cl.show(main, "HIGHSCORES"));
highscorePanel.setVisible(true);
highscorePanel.setSize(600, 600);
main.add(highscorePanel, "HIGHSCOREEINTRAG");
cl.show(main, "HIGHSCOREEINTRAG");
}

View File

@ -12,7 +12,8 @@ import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain2;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain;
public class MenuGUI extends JFrame implements ActionListener {
@ -40,18 +41,18 @@ public class MenuGUI extends JFrame implements ActionListener {
"/Hitori_Spielfelder/Hitori15x15_medium.csv"
};
for(int i=0;i<buttons.length;i++){
JButton b0 = new JButton(buttons[i]);
buttonPanel.add(b0);
JButton button = new JButton(buttons[i]);
buttonPanel.add(button);
int[] count = {i};
String[] num = buttons[i].split("x");
b0.addActionListener(e -> {
button.addActionListener(e -> {
int j = count[0];
filepath[0] = paths[j];
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);
HitoriMain.ablauf(cl, main, inputStream, Integer.parseInt(filepath[1]), path);
} else {
throw new FileNotFoundException("Resource not found: " + filepath[0]);
}
@ -60,14 +61,14 @@ public class MenuGUI extends JFrame implements ActionListener {
}
});
}
JButton b = new JButton("Highscores");
b.addActionListener(e -> {
JButton highscoreButton = new JButton("Highscores");
highscoreButton.addActionListener(e -> {
new HighscoreGUI(cl, main);
});
buttonPanel.add(b);
buttonPanel.add(highscoreButton);
menuPanel.add(buttonPanel, BorderLayout.CENTER);
JLabel text0 = new JLabel("Wählen Sie ein Level aus!");
menuPanel.add(text0, BorderLayout.NORTH);
JLabel topText = new JLabel("Wählen Sie ein Level aus!");
menuPanel.add(topText, BorderLayout.NORTH);
setVisible(true);
setSize(600, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);

View File

@ -1,60 +1,43 @@
package de.hs_mannheim.informatik.mvn.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JLabel;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain2;
import de.hs_mannheim.informatik.mvn.domain.HitoriMain;
import de.hs_mannheim.informatik.mvn.gui.GameGUI;
class HitoriTest{
@Test
void test1() {
void makeColorArrayTest1() {
String[][] array3x3 = {
{"W", "W", "W"},
{"W", "W", "W"},
{"W", "W", "W"}
};
assertTrue(Arrays.deepEquals(array3x3, HitoriMain2.makeColorArray(3)));
assertTrue(Arrays.deepEquals(array3x3, HitoriMain.makeColorArray(3)));
}
@Test
void test2() {
void makeColorArrayTest2() {
String[][] array3x2 = {
{"W", "W", "W"},
{"W", "W", "W"},
};
assertFalse(Arrays.deepEquals(array3x2, HitoriMain2.makeColorArray(3)));
assertFalse(Arrays.deepEquals(array3x2, HitoriMain.makeColorArray(3)));
}
@Test
void test3() {
void checkStartTimeTest() {
assertEquals("Zeit: 00:00", GameGUI.startTimer());
}
@Test
void test4() {
void checkArraySameTest1() {
String[][] data = {
{"W", "B", "W", "W"},
{"W", "W", "W", "B"},
@ -65,11 +48,11 @@ class HitoriTest{
{"W", "W", "W", "B"},
{"W", "B", "W", "W"},
{"B", "W", "W", "B"}};
assertFalse(HitoriMain2.checkArraySame(data, colors));
assertFalse(HitoriMain.checkArraySame(data, colors));
}
@Test
void test5() {
void checkArraySameTest2() {
String[][] data = {
{"W", "B", "W", "W"},
{"W", "W", "W", "B"},
@ -81,11 +64,11 @@ class HitoriTest{
{"W", "B", "W", "W"},
{"B", "W", "W", "B"}};
assertTrue(HitoriMain2.checkArraySame(data, colors));
assertTrue(HitoriMain.checkArraySame(data, colors));
}
@Test
void test6() {
void checkArraySameTest3() {
String[][] data = {
{"B", "W", "W", "B", "W"},
{"W", "W", "B", "W", "W"},
@ -98,11 +81,11 @@ class HitoriTest{
{"W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W"},
{"W", "B", "W", "W", "W"}};
assertFalse(HitoriMain2.checkArraySame(data, colors));
assertFalse(HitoriMain.checkArraySame(data, colors));
}
@Test
void test7() {
void checkArraySameTest4() {
String[][] data = {
{"B", "W", "W", "B", "W"},
{"W", "W", "B", "W", "W"},
@ -115,23 +98,23 @@ class HitoriTest{
{"B", "W", "W", "B", "W"},
{"W", "W", "B", "W", "W"},
{"B", "W", "W", "W", "B"}};
assertTrue(HitoriMain2.checkArraySame(data, colors));
assertTrue(HitoriMain.checkArraySame(data, colors));
}
@Test
void test8() {
void getCordsTest1() {
String[] array = {"3", "4"};
assertFalse(Arrays.deepEquals(array, HitoriMain2.getCords(3, 3)));
assertFalse(Arrays.deepEquals(array, HitoriMain.getCords(3, 3)));
}
@Test
void test9() {
void getCordsTest2() {
String[] array = {"3", "4"};
assertTrue(Arrays.deepEquals(array, HitoriMain2.getCords(3, 4)));
assertTrue(Arrays.deepEquals(array, HitoriMain.getCords(3, 4)));
}
@Test
void test10() throws FileNotFoundException {
void checkArraySameTest5() throws FileNotFoundException {
String[][] data = {
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "B", "W", "W", "B", "W", "W", "W"},
@ -154,11 +137,11 @@ class HitoriTest{
{"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));
assertTrue(HitoriMain.checkArraySame(data, colors));
}
@Test
void test11() {
void checkArraySameTest6() {
String[][] data = {
{"B", "W", "B", "W", "W", "B", "W", "W", "W", "B"},
{"W", "W", "W", "B", "W", "W", "B", "W", "W", "W"},
@ -181,11 +164,11 @@ class HitoriTest{
{"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));
assertFalse(HitoriMain.checkArraySame(data, colors));
}
@Test
void test12() {
void getResultTest1() {
String[][] data = {
{"W", "G", "B", "W"},
{"G", "B", "W", "G"},
@ -201,11 +184,11 @@ class HitoriTest{
{"W", "B", "W", "W"},
{"B", "W", "W", "B"},
{"W", "W", "B", "W"}};
assertTrue(Arrays.deepEquals(array, HitoriMain2.getResult(data, colors)));
assertTrue(Arrays.deepEquals(array, HitoriMain.getResult(data, colors)));
}
@Test
void test13() {
void getResultTest2() {
String[][] data = {
{"W", "G", "B", "W"},
{"G", "B", "W", "G"},
@ -221,15 +204,15 @@ class HitoriTest{
{"G", "B", "W", "W"},
{"B", "W", "W", "B"},
{"W", "W", "B", "W"}};
assertFalse(Arrays.deepEquals(array, HitoriMain2.getResult(data, colors)));
assertFalse(Arrays.deepEquals(array, HitoriMain.getResult(data, colors)));
}
@Test
void test14() throws FileNotFoundException {
void getSolutionTest1() throws FileNotFoundException {
String[] ergebnis4x4 = {"0,1", "1,3", "2,1", "3,0", "3,3"};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> filteredData = HitoriMain2.getSolution(inputStream);
ArrayList<String> filteredData = HitoriMain.getSolution(inputStream);
assertEquals(5, filteredData.size());
assertEquals(ergebnis4x4[0], filteredData.get(0));
assertEquals(ergebnis4x4[1], filteredData.get(1));
@ -239,11 +222,11 @@ class HitoriTest{
}
@Test
void test15() throws FileNotFoundException {
void getSolutionTest2() throws FileNotFoundException {
String[] ergebnis4x4 = {"0,0"};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> filteredData = HitoriMain2.getSolution(inputStream);
ArrayList<String> filteredData = HitoriMain.getSolution(inputStream);
assertNotEquals(1, filteredData.size());
assertNotEquals(ergebnis4x4[0], filteredData.get(0));
assertNotEquals(ergebnis4x4[0], filteredData.get(1));
@ -253,27 +236,27 @@ class HitoriTest{
}
@Test
void test16() {
void makeColorArrayTest3() {
String[][] colors = {
{"W", "W", "W", "W"},
{"W", "W", "W", "W"},
{"W", "W", "W", "W"},
{"W", "W", "W", "W"}};
assertTrue(Arrays.deepEquals(colors, HitoriMain2.makeColorArray(4)));
assertTrue(Arrays.deepEquals(colors, HitoriMain.makeColorArray(4)));
}
@Test
void test17() {
void makeColorArrayTest4() {
String[][] colors = {
{"B", "W", "W", "W"},
{"B", "W", "W", "W"},
{"B", "W", "W", "W"},
{"B", "W", "W", "W"}};
assertFalse(Arrays.deepEquals(colors, HitoriMain2.makeColorArray(4)));
assertFalse(Arrays.deepEquals(colors, HitoriMain.makeColorArray(4)));
}
@Test
void test18() throws FileNotFoundException {
void getDataTest1() throws FileNotFoundException {
String[][] data = {
{"3", "3", "1", "4"},
{"4", "3", "2", "2"},
@ -281,11 +264,11 @@ class HitoriTest{
{"3", "4", "3", "2"}};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
assertTrue(Arrays.deepEquals(data, HitoriMain2.getData(inputStream, 4)));
assertTrue(Arrays.deepEquals(data, HitoriMain.getData(inputStream, 4)));
}
@Test
void test19() throws FileNotFoundException {
void getDataTest2() throws FileNotFoundException {
String[][] data = {
{"3", "0", "1", "4"},
{"4", "0", "2", "2"},
@ -293,11 +276,11 @@ class HitoriTest{
{"3", "0", "3", "2"}};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
assertFalse(Arrays.deepEquals(data, HitoriMain2.getData(inputStream, 4)));
assertFalse(Arrays.deepEquals(data, HitoriMain.getData(inputStream, 4)));
}
@Test
void test20() {
void readFromFileTest1() {
String[] file = {
"3,3,1,4",
"4,3,2,2",
@ -316,12 +299,12 @@ class HitoriTest{
}
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> ergebnisList = HitoriMain2.readFromFile(inputStream);
ArrayList<String> ergebnisList = HitoriMain.readFromFile(inputStream);
assertEquals(fileEntry, ergebnisList);
}
@Test
void test21() {
void readFromFileTest2() {
String[] file = {
"3,3,1,0",
"4,3,2,0",
@ -340,7 +323,7 @@ class HitoriTest{
}
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> ergebnisList = HitoriMain2.readFromFile(inputStream);
ArrayList<String> ergebnisList = HitoriMain.readFromFile(inputStream);
assertNotEquals(fileEntry, ergebnisList);
}
@ -353,11 +336,11 @@ class HitoriTest{
{"B", "W", "W", "B"}};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> filteredData = HitoriMain2.getSolution(inputStream);
ArrayList<String> filteredData = HitoriMain.getSolution(inputStream);
InputStream newStream = getClass().getResourceAsStream(path);
int rows = 4;
String[][] data = HitoriMain2.getData(newStream, rows);
assertTrue(Arrays.deepEquals(ergebnis1, HitoriMain2.getErgebnisArray(data, filteredData)));
String[][] data = HitoriMain.getData(newStream, rows);
assertTrue(Arrays.deepEquals(ergebnis1, HitoriMain.getErgebnisArray(data, filteredData)));
}
@Test
@ -369,11 +352,11 @@ class HitoriTest{
{"W", "W", "W", "W"}};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> filteredData = HitoriMain2.getSolution(inputStream);
ArrayList<String> filteredData = HitoriMain.getSolution(inputStream);
InputStream newStream = getClass().getResourceAsStream(path);
int rows = 4;
String[][] data = HitoriMain2.getData(newStream, rows);
assertFalse(Arrays.deepEquals(ergebnis1, HitoriMain2.getErgebnisArray(data, filteredData)));
String[][] data = HitoriMain.getData(newStream, rows);
assertFalse(Arrays.deepEquals(ergebnis1, HitoriMain.getErgebnisArray(data, filteredData)));
}
@Test
@ -385,6 +368,45 @@ class HitoriTest{
void test25() throws IOException {
}
@Test
void getErgebnisArrayTest1() throws IOException {
String[][] ergebnis1 = {
{"W", "B", "W", "W"},
{"W", "W", "W", "B"},
{"W", "B", "W", "W"},
{"B", "W", "W", "B"}};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> filteredData = HitoriMain.getSolution(inputStream);
InputStream newStream = getClass().getResourceAsStream(path);
int rows = 4;
String[][] data = HitoriMain.getData(newStream, rows);
assertTrue(Arrays.deepEquals(ergebnis1, HitoriMain.getErgebnisArray(data, filteredData)));
}
@Test
void getErgebnisArrayTest2() throws IOException {
String[][] ergebnis1 = {
{"W", "W", "W", "W"},
{"W", "W", "W", "W"},
{"W", "W", "W", "W"},
{"W", "W", "W", "W"}};
String path = "/Hitori_Spielfelder/Hitori4x4_leicht.csv";
InputStream inputStream = getClass().getResourceAsStream(path);
ArrayList<String> filteredData = HitoriMain.getSolution(inputStream);
InputStream newStream = getClass().getResourceAsStream(path);
int rows = 4;
String[][] data = HitoriMain.getData(newStream, rows);
assertFalse(Arrays.deepEquals(ergebnis1, HitoriMain.getErgebnisArray(data, filteredData)));
}
}