diff --git a/PR2Projekt/.classpath b/PR2Projekt/.classpath index ea3904f..e2a23a6 100644 --- a/PR2Projekt/.classpath +++ b/PR2Projekt/.classpath @@ -1,48 +1,43 @@ - - - - - - - - - - - - - - - - - - target/classes - - - - - - - - target/test-classes - - - - - - - - target/test-classes - - - - - - - - - - - + + + + + + + + + + + + target/classes + + + + + + + + target/test-classes + + + + + + + + + + + + + + + + + + + + diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HighscoreEintrag.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HighscoreEintrag.java new file mode 100644 index 0000000..3f2d6c3 --- /dev/null +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HighscoreEintrag.java @@ -0,0 +1,21 @@ +package de.hs_mannheim.informatik.mvn.domain; + +import java.time.LocalTime; + +public class HighscoreEintrag { + LocalTime time; + String name; + + HighscoreEintrag(LocalTime time, String name) { + this.time = time; + this.name = name; + } + + public LocalTime getTime() { + return time; + } + + public String getName() { + return name; + } +} \ No newline at end of file diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HighscoreManager.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HighscoreManager.java new file mode 100644 index 0000000..03e5c34 --- /dev/null +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HighscoreManager.java @@ -0,0 +1,126 @@ +package de.hs_mannheim.informatik.mvn.domain; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +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.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; +import java.time.DateTimeException; +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.Comparator; + +import de.hs_mannheim.informatik.mvn.gui.HighscoreMenuGUI; + +public class HighscoreManager { + + public static void sortByTime(byte[] data, String filename) throws IOException { + ArrayList highscores = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(new ByteArrayInputStream(data), StandardCharsets.UTF_8))) { + + String line; + 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(HighscoreEintrag::getTime)); + ArrayList formattedHighscores = new ArrayList<>(); + for (HighscoreEintrag e : highscores) { + String formattedTime = String.format("%02d:%02d", e.getTime().getHour(), e.getTime().getMinute()); + String line = formattedTime + " " + e.getName(); + formattedHighscores.add(line); + } + String fileName = filename.substring(filename.lastIndexOf('/') + 1); + File file = new File(fileName); + overwriteContentOfFile(formattedHighscores, file); + } + + private static void overwriteContentOfFile(ArrayList 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(); + } + } + } + + 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") + .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) + .toFormatter(); + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.isEmpty()) continue; + String[] parts = line.split("\\s+", 2); + String timePart = parts[0]; + LocalTime localTime = LocalTime.parse(timePart, dtf); + int minutes = localTime.getMinute(); + int seconds = localTime.getSecond(); + int totalSecThisLine = minutes * 60 + seconds; + totalSeconds += totalSecThisLine; + count++; + } + int timeInt = totalSeconds/count; + int minutes = timeInt/60; + int seconds = timeInt%60; + String timeString= "Durchschnittszeit: " + minutes+":"+seconds; + return timeString; + } + } + + public static void copyResourceIfNotExists(String resourcePathInJar, File outFile) throws IOException { + if (outFile.exists()) { + return; + } + outFile.getParentFile().mkdirs(); + try (InputStream in = HighscoreMenuGUI.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); + } + } + } +} diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/LogHighscores.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/Highscores.java similarity index 89% rename from PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/LogHighscores.java rename to PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/Highscores.java index 5ea9751..75a3903 100644 --- a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/LogHighscores.java +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/Highscores.java @@ -4,8 +4,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.*; -public class LogHighscores { - private static final Logger logger = LogManager.getLogger(LogHighscores.class); +public class Highscores { + private static final Logger logger = LogManager.getLogger(Highscores.class); public static void newRecord(String resourcePath, String username, String time) throws IOException { String timePart = time.substring("Zeit: ".length()).trim(); @@ -21,7 +21,7 @@ public class LogHighscores { File localFile = new File(localFolder, newName); - try (InputStream inputStream = LogHighscores.class.getResourceAsStream(resourcePath)) { + try (InputStream inputStream = Highscores.class.getResourceAsStream(resourcePath)) { if (inputStream == null) { logger.warn("Resource not found in JAR: {}", resourcePath); } else { diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HitoriMain.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HitoriMain.java index 579c27c..5c91650 100644 --- a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HitoriMain.java +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HitoriMain.java @@ -1,50 +1,20 @@ package de.hs_mannheim.informatik.mvn.domain; -import java.awt.CardLayout; import java.io.BufferedReader; -import java.io.BufferedWriter; -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.io.OutputStreamWriter; -import java.nio.charset.StandardCharsets; -import java.time.DateTimeException; -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoField; import java.util.ArrayList; -import java.util.Comparator; -import java.util.Stack; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import java.time.format.DateTimeFormatterBuilder; -import de.hs_mannheim.informatik.mvn.gui.*; +import de.hs_mannheim.informatik.mvn.facade.HitoriAPI; public class HitoriMain { - private static byte[] gameData; - public static void main(String[] args) throws FileNotFoundException{ - new MenuGUI(); - } - - public static void ablauf(CardLayout cl, JPanel main, InputStream inputStream, int rows, String path) throws IOException { - Stack 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 gameStream = new ByteArrayInputStream(gameData); - GameGUI.paintGame(gameStream, cl, main, buttons, colors, madeMoves, data, path); - } + HitoriAPI.starter(); + } + public static ArrayList readFromFile(InputStream inputStream){ ArrayList lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { @@ -82,18 +52,6 @@ public class HitoriMain { } return colors; } - - public static JButton[][] makeButtonArray(String[][] data){ - JButton[][] buttons = new JButton[data.length][data.length]; - for(int i=0;i madeMoves,String[][] data,String path) throws FileNotFoundException{ - madeMoves.clear(); - for(int i = 0; i highscores = new ArrayList<>(); - try (BufferedReader reader = new BufferedReader( - new InputStreamReader(new ByteArrayInputStream(data), StandardCharsets.UTF_8))) { - - String line; - 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(HighscoreEintrag::getTime)); - ArrayList formattedHighscores = new ArrayList<>(); - for (HighscoreEintrag e : highscores) { - String formattedTime = String.format("%02d:%02d", e.getTime().getHour(), e.getTime().getMinute()); - String line = formattedTime + " " + e.getName(); - formattedHighscores.add(line); - } - String fileName = filename.substring(filename.lastIndexOf('/') + 1); - File file = new File(fileName); - overwriteContentOfFile(formattedHighscores, file); } - - private static void overwriteContentOfFile(ArrayList 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(); - } - } - } - 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") - .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) - .toFormatter(); - while ((line = reader.readLine()) != null) { - line = line.trim(); - if (line.isEmpty()) continue; - String[] parts = line.split("\\s+", 2); - String timePart = parts[0]; - LocalTime localTime = LocalTime.parse(timePart, dtf); - int minutes = localTime.getMinute(); - int seconds = localTime.getSecond(); - int totalSecThisLine = minutes * 60 + seconds; - totalSeconds += totalSecThisLine; - count++; - } - int timeInt = totalSeconds/count; - int minutes = timeInt/60; - int seconds = timeInt%60; - String timeString= "Durchschnittszeit: " + minutes+":"+seconds; - return timeString; - } + public static String[] getGameButtonNames() { + String[] buttonNames = { + "4x4 - leicht", + "5x5 - leicht", + "8x8 - leicht", + "8x8 - medium", + "10x10 - medium", + "15x15 - medium" + }; + return buttonNames; } -} -class HighscoreEintrag { - LocalTime time; - String name; + public static String[] getGamePath() { + String[] buttonPaths = { + "/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" + }; + return buttonPaths; + } - HighscoreEintrag(LocalTime time, String name) { - this.time = time; - this.name = name; - } - - public LocalTime getTime() { - return time; - } - - public String getName() { - return name; - } -} + public static String[] getHighscorePathNames() { + String[] pathNames = { + "src/main/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" + }; + return pathNames; + } + + public static String[] getHighscoreButtonNames() { + final String[] buttonNames = { + "Highscore 4x4 - leicht", + "Highscore 5x5 - leicht", + "Highscore 8x8 - leicht", + "Highscore 8x8 - medium", + "Highscore 10x10 - medium", + "Highscore 15x15 - medium" + }; + return buttonNames; + } +} \ No newline at end of file diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/GameGUI.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/GameGUI.java index b2bf3b7..be9f46b 100644 --- a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/GameGUI.java +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/GameGUI.java @@ -2,15 +2,12 @@ package de.hs_mannheim.informatik.mvn.gui; import java.awt.*; import java.awt.event.*; -import java.util.EmptyStackException; 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.HitoriMain; -import de.hs_mannheim.informatik.mvn.domain.LogHighscores; +import de.hs_mannheim.informatik.mvn.facade.HitoriAPI; public class GameGUI extends JFrame implements ActionListener { @@ -22,21 +19,18 @@ public class GameGUI extends JFrame implements ActionListener { public static void paintGame(InputStream inputStream, CardLayout cl, JPanel main, JButton[][] buttons, String[][] colors, Stack 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 {paintButton(cl, main, b, pos, colors , madeMoves);}); + JButton button = buttons[i][j]; + Color foreground = Color.BLACK; + Color background = Color.WHITE; + changeButtonColor(button, foreground, background); + gameGrid.add(button); + String[] pos = HitoriAPI.getCordsCatcher(i,j); + button.addActionListener(e -> {paintButton(cl, main, button, pos, colors , madeMoves, gameGrid, buttons);}); } } JPanel mainPanel = new JPanel(new BorderLayout()); @@ -52,7 +46,7 @@ public class GameGUI extends JFrame implements ActionListener { 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); + HitoriAPI.totalResetButtonCatcher(inputStream, cl, main, buttons, colors, madeMoves, data, path0); } catch (FileNotFoundException e1) { e1.printStackTrace(); }}); @@ -64,7 +58,7 @@ public class GameGUI extends JFrame implements ActionListener { topGrid.add(timeLabel); topGrid.revalidate(); topGrid.repaint(); - boolean isOkay = HitoriMain.abgabeMöglich(inputStream, data, colors); + boolean isOkay = HitoriAPI.abgabeMöglichCatcher(inputStream, data, colors); levelFinished[0] = isOkay; Timer timer = new Timer(); startTimer(); @@ -88,7 +82,7 @@ public class GameGUI extends JFrame implements ActionListener { abgebenButton.addActionListener(e -> { try { InputStream newStream = new ByteArrayInputStream(gameData); - levelFinished[0] = HitoriMain.abgabeMöglich(newStream, data, colors); + levelFinished[0] = HitoriAPI.abgabeMöglichCatcher(newStream, data, colors); } catch (FileNotFoundException e1) { e1.printStackTrace(); } @@ -96,7 +90,7 @@ public class GameGUI extends JFrame implements ActionListener { String endtime = stopTimer(); timer.cancel(); try { - finish(cl, main, endtime, filepath, path0); + NewEntryGUI.finish(cl, main, endtime, filepath, path0); } catch (FileNotFoundException e1) { e1.printStackTrace(); } @@ -112,73 +106,94 @@ public class GameGUI extends JFrame implements ActionListener { buttonGrid.add(abgebenButton); mainPanel.add(buttonGrid, BorderLayout.SOUTH); mainPanel.setVisible(true); + mainPanel.setSize(700,700); main.add(mainPanel, "GAME"); cl.show(main, "GAME"); } - public static void paintButton(CardLayout cl, JPanel main, JButton button, String[] pos, String[][] colors, Stack madeMoves){ - int i = Integer.parseInt(pos[0]); - int j = Integer.parseInt(pos[1]); - 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); - } + private static void addMove(String[][] colors, int i, int j, String lastLetterString, Stack madeMoves){ colors[i][j] += lastLetterString; String newMove = i+"."+j+"."+lastLetterString; madeMoves.push(newMove); } + + private static void changeButtonColor(JButton button, Color foreground, Color background) { + button.setOpaque(true); + button.setForeground(foreground); + button.setContentAreaFilled(true); + button.setBorderPainted(false); + button.setFocusPainted(false); + button.setBackground(background); + } + + public static void paintButton(CardLayout cl, JPanel main, JButton button, String[] pos, String[][] colors, Stack madeMoves, JPanel grid, JButton[][] buttons){ + int i = Integer.parseInt(pos[0]); + int j = Integer.parseInt(pos[1]); + String col = colors[i][j]; + String lastLetterString = String.valueOf(col.charAt(col.length()-1)); + Color foreground; + Color background; + switch(lastLetterString) { + case "W": + foreground = Color.BLACK; + background = Color.lightGray; + lastLetterString = "G"; + changeButtonColor(button, foreground, background); + break; + case "G": + foreground = Color.WHITE; + background = Color.BLACK; + lastLetterString = "B"; + changeButtonColor(button, foreground, background); + break; + case "B": + foreground = Color.BLACK; + background = Color.WHITE; + lastLetterString = "W"; + changeButtonColor(button, foreground, background); + break; + } + addMove(colors, i, j, lastLetterString, madeMoves); + } public static void backOneStep(CardLayout cl, JPanel main, Stack movesMade, JButton[][] buttons, String[][] colors, JPanel grid){ - String move = movesMade.pop(); - String[] line = move.split("\\."); - int i = Integer.parseInt(line[0]); - int j = Integer.parseInt(line[1]); - String color = line[2]; - 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); + String move = movesMade.pop(); + String[] line = move.split("\\."); + int i = Integer.parseInt(line[0]); + int j = Integer.parseInt(line[1]); + String color = line[2]; + JButton button = buttons[i][j]; + Color foreground; + Color background; + switch(color) { + case "W": + foreground = Color.WHITE; + background = Color.BLACK; + changeButtonColor(button, foreground, background); break; case "G": - button.setForeground(Color.BLACK); - button.setBackground(Color.WHITE); + foreground = Color.BLACK; + background = Color.WHITE; + changeButtonColor(button, foreground, background); break; case "B": - button.setForeground(Color.BLACK); - button.setBackground(Color.lightGray); + foreground = Color.BLACK; + background = Color.lightGray; + changeButtonColor(button, foreground, background); break; - } - String colorString = colors[i][j]; - String colorStringWithoutLastElement = colorString.substring(0, colorString.length() - 1); - colors[i][j] = colorStringWithoutLastElement; - grid.repaint(); - gridUpdate(grid, buttons); + } + removeMove(colors, i, j, grid, buttons); } - - public static void gridUpdate(JPanel grid, JButton[][] buttons){ + + private static void removeMove(String[][] colors, int i, int j, JPanel grid, JButton[][] buttons) { + 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){ grid.removeAll(); grid.repaint(); for(int i = 0; i { - String username = field.getText(); - try { - LogHighscores.newRecord(path, username, endtime); - filepath[0] = ""; - filepath[1] = ""; - cl.show(main, "HAUPT"); - } catch (IOException e1) { - e1.getMessage(); - } - }); - main.add(mainPanel, "HIGHSCORENEU"); - cl.show(main, "HIGHSCORENEU"); - } @Override public void actionPerformed(ActionEvent e) { } + + public static JButton[][] makeButtonArray(String[][] data){ + JButton[][] buttons = new JButton[data.length][data.length]; + for(int i=0;i madeMoves,String[][] data,String path) throws FileNotFoundException{ + madeMoves.clear(); + for(int i = 0; i { - String resourcePathInJar = paths[index]; - String localFileName = resourcePathInJar.substring(resourcePathInJar.lastIndexOf('/') + 1); - File outFile = new File("resources/Hitori_Highscores", localFileName); - try { - copyResourceIfNotExists(resourcePathInJar, outFile); - byte[] data = Files.readAllBytes(outFile.toPath()); - showHighscores(cl, main, data, outFile.getPath()); - } catch (IOException e1) { - e1.printStackTrace(); - empty(cl, main); - } - }); - } - 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 topText = new JLabel("Level für Highscore Liste auswählen!"); - highscorePanel.add(topText, BorderLayout.NORTH); - main.add(highscorePanel, "HIGHSCORES"); - cl.show(main, "HIGHSCORES"); - } - - 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 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 { - HitoriMain.sortByTime(data, filename); - data = Files.readAllBytes(new File(filename).toPath()); - JPanel highscorePanel = new JPanel(new BorderLayout()); - List 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(); - } - JPanel entryPanel = new JPanel(new GridLayout(lines.size(), 1, 10, 10)); - for (String s : lines) { - JLabel text = new JLabel(s); - entryPanel.add(text); - } - 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"); - } - - @Override - public void actionPerformed(ActionEvent e) { - } -} diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/HighscoreMenuGUI.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/HighscoreMenuGUI.java new file mode 100644 index 0000000..92e7b2f --- /dev/null +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/HighscoreMenuGUI.java @@ -0,0 +1,44 @@ +package de.hs_mannheim.informatik.mvn.gui; + +import java.awt.BorderLayout; +import java.awt.CardLayout; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import de.hs_mannheim.informatik.mvn.facade.HitoriAPI; + +public class HighscoreMenuGUI extends JFrame implements ActionListener { + final String[] pathNames = HitoriAPI.getHighscorePathNamesCatcher(); + final String[] buttonNames = HitoriAPI.getHighscoreButtonNamesCatcher(); + + public HighscoreMenuGUI(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 < buttonNames.length; i++) { + JButton button = new JButton(buttonNames[i]); + buttonPanel.add(button); + final int index = i; + button.addActionListener(e -> { + HitoriAPI.loadHighscore(index, pathNames, cl, main); + }); + } + JButton zurückButton = new JButton("Zurück"); + zurückButton.addActionListener(e -> cl.show(main, "HAUPT")); + buttonPanel.add(zurückButton); + highscorePanel.setVisible(true); + highscorePanel.setSize(700,700); + highscorePanel.add(buttonPanel, BorderLayout.CENTER); + JLabel topText = new JLabel("Level für Highscore Liste auswählen!"); + highscorePanel.add(topText, BorderLayout.NORTH); + main.add(highscorePanel, "HIGHSCORES"); + cl.show(main, "HIGHSCORES"); + } + + @Override + public void actionPerformed(ActionEvent e) { + } +} diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/HighscoreTableGUI.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/HighscoreTableGUI.java new file mode 100644 index 0000000..a77f3e7 --- /dev/null +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/HighscoreTableGUI.java @@ -0,0 +1,66 @@ +package de.hs_mannheim.informatik.mvn.gui; + +import java.awt.BorderLayout; +import java.awt.CardLayout; +import java.awt.GridLayout; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import de.hs_mannheim.informatik.mvn.facade.HitoriAPI; + +public class HighscoreTableGUI { + public static void showHighscores(CardLayout cl, JPanel main, byte[] data, String filename) throws IOException { + HitoriAPI.sortByTimeCatcher(data,filename); + data = Files.readAllBytes(new File(filename).toPath()); + JPanel highscorePanel = new JPanel(new BorderLayout()); + List 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) { + JLabel text = new JLabel(s); + entryPanel.add(text); + } + highscorePanel.add(entryPanel, BorderLayout.CENTER); + + String avgString = HitoriAPI.getAvgTimeCatcher(data,filename); + JLabel avgTime = new JLabel(avgString); + highscorePanel.add(avgTime, BorderLayout.NORTH); + } else { + JLabel text = new JLabel("Noch kein Highscore eingetragen."); + highscorePanel.add(text, BorderLayout.CENTER); + } + 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"); + } +} diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/MenuGUI.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/MenuGUI.java index f8b6dc0..fa88963 100644 --- a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/MenuGUI.java +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/MenuGUI.java @@ -5,72 +5,46 @@ import java.awt.CardLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; - -import de.hs_mannheim.informatik.mvn.domain.HitoriMain; +import de.hs_mannheim.informatik.mvn.facade.HitoriAPI; public class MenuGUI extends JFrame implements ActionListener { private static CardLayout cl = new CardLayout(); private static JPanel main = new JPanel(cl); private static String[] filepath = {"", ""}; + private static byte[] gameData; public MenuGUI(){ JPanel menuPanel = new JPanel(new BorderLayout()); JPanel buttonPanel = new JPanel(new GridLayout(7,1,10,10)); - final String[] buttons = { - "4x4 - leicht", - "5x5 - leicht", - "8x8 - leicht", - "8x8 - medium", - "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 { 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]; - HitoriMain.ablauf(cl, main, inputStream, Integer.parseInt(filepath[1]), path); - } else { - throw new FileNotFoundException("Resource not found: " + filepath[0]); - } - } catch (IOException e1) { - e1.getMessage(); - } - }); + filepath[0] = buttonPaths[j]; + filepath[1] = num[0]; + HitoriAPI.loadGame(filepath, cl, main, gameData); + }); } JButton highscoreButton = new JButton("Highscores"); highscoreButton.addActionListener(e -> { - new HighscoreGUI(cl, main); + new HighscoreMenuGUI(cl, main); }); buttonPanel.add(highscoreButton); menuPanel.add(buttonPanel, BorderLayout.CENTER); JLabel topText = new JLabel("Wählen Sie ein Level aus!"); menuPanel.add(topText, BorderLayout.NORTH); - setVisible(true); - setSize(600, 600); + setVisible(true); + setSize(700,700); setDefaultCloseOperation(EXIT_ON_CLOSE); add(main); main.add(menuPanel, "HAUPT"); @@ -80,4 +54,6 @@ public class MenuGUI extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { } + + } diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/NewEntryGUI.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/NewEntryGUI.java new file mode 100644 index 0000000..3535a6a --- /dev/null +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/gui/NewEntryGUI.java @@ -0,0 +1,40 @@ +package de.hs_mannheim.informatik.mvn.gui; + +import java.awt.BorderLayout; +import java.awt.CardLayout; +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import de.hs_mannheim.informatik.mvn.domain.Highscores; + +public class NewEntryGUI { + public static void finish(CardLayout cl, JPanel main, String endtime, String[] filepath, String path) throws FileNotFoundException{ + JPanel mainPanel = new JPanel(new BorderLayout()); + 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 addHighscoreButton = new JButton("In der Highscore Liste eintragen"); + mainPanel.add(addHighscoreButton, BorderLayout.SOUTH); + mainPanel.setVisible(true); + mainPanel.setSize(700,700); + addHighscoreButton.addActionListener(e -> { + String username = field.getText(); + try { + Highscores.newRecord(path, username, endtime); + filepath[0] = ""; + filepath[1] = ""; + cl.show(main, "HAUPT"); + } catch (IOException e1) { + e1.getMessage(); + } + }); + main.add(mainPanel, "HIGHSCORENEU"); + cl.show(main, "HIGHSCORENEU"); + } +}