diff --git a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java index 03d5170..b31addd 100644 --- a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java +++ b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/GameGUI.java @@ -4,12 +4,14 @@ import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.io.FileNotFoundException; +import java.util.EmptyStackException; import java.util.Stack; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.JTextField; import de.hs_mannheim.informatik.pr2projekt.domain.HitoriMain; @@ -50,7 +52,7 @@ public class GameGUI { try{ boolean levelFinished = HitoriMain.abgabeMöglich(path, data, colors); if(levelFinished == true){ - HitoriMain.finish(path, data, colors); + GameGUI.finish(filepath, path, data, colors); frame.dispose(); } else { JLabel text1 = new JLabel("Abgabe nicht richtig!"); @@ -107,6 +109,7 @@ public class GameGUI { String logEntrance = i+"."+j+"."+"W"; madeMoves.push(logEntrance); } + } public static void backOneStep(Stack movesMade, JButton[][] buttons, String[][] colors, JPanel grid){ try { @@ -177,5 +180,30 @@ public class GameGUI { } } - } + public static void finish(String[] filepath, String path, String[][] data, String[][] colors) throws FileNotFoundException{ + JFrame frame = new JFrame("Highscore anlegen"); + 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); + JTextField field = new JTextField(20); + mainPanel.add(field, BorderLayout.CENTER); + JButton b = new JButton("In der Highscore Liste eintragen"); + mainPanel.add(b, BorderLayout.SOUTH); + frame.setVisible(true); + frame.setSize(600,600); + frame.add(mainPanel); + b.addActionListener(e -> { + String username = field.getText(); + try { + HighscoreGUI.newRecord(path, username); + frame.dispose(); + } catch (FileNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + MenuGUI.getPath(filepath); + }); + filepath[0] = ""; + filepath[1] = ""; + } } diff --git a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/HighscoreGUI.java b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/HighscoreGUI.java index 104b3a6..37c3915 100644 --- a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/HighscoreGUI.java +++ b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/HighscoreGUI.java @@ -2,12 +2,21 @@ package de.hs_mannheim.informatik.pr2projekt.gui; import java.awt.BorderLayout; import java.awt.GridLayout; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; +import de.hs_mannheim.informatik.pr2projekt.domain.HitoriMain; + public class HighscoreGUI { public static void highscoreScreen(String[] filepath){ @@ -38,7 +47,7 @@ public class HighscoreGUI { "Hitori_Highscores/Hitori10x10medium.txt", "Hitori_Highscores/Hitori15x15_medium.txt" }; - showHighscores(paths[j]); + HighscoreGUI.showHighscores(paths[j]); frame.dispose(); }); } @@ -56,5 +65,73 @@ public class HighscoreGUI { frame.setDefaultCloseOperation(0); frame.add(highscorePanel); } + + public static void newRecord(String path, String username) throws FileNotFoundException{ + 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; + Scanner sc = new Scanner(filepath); + while (sc.hasNextLine()) { + sc.nextLine(); + } + sc.close(); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(filepath, true))) { + writer.write(username); + writer.newLine(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void showHighscores(String[] filepath, String path){ + JFrame frame = new JFrame("Highscore"); + JPanel mainPanel = new JPanel(new BorderLayout()); + File file = new File(path); + int i = 0; + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + scanner.nextLine(); + i++; + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + JPanel highscorePanel = new JPanel(new GridLayout(i,1,10,10)); + if(i>0){ + ArrayList lines = HitoriMain.readFromFile(path); + for(String s: lines){ + JLabel text = new JLabel(s); + highscorePanel.add(text); + } + JButton b = new JButton("Zurück"); + b.addActionListener(e -> { + frame.dispose(); + highscoreScreen(filepath); + }); + mainPanel.add(b, BorderLayout.SOUTH); + } else { + JLabel text = new JLabel("Noch kein Highscore eingetragen."); + highscorePanel.add(text); + JButton b = new JButton("Zurück"); + b.addActionListener(e -> { + frame.dispose(); + highscoreScreen(filepath); + }); + mainPanel.add(b, BorderLayout.SOUTH); + } + JButton b = new JButton("Zurück"); + b.addActionListener(e ->{ + frame.dispose(); + MenuGUI.getPath(filepath); + }); + mainPanel.add(highscorePanel, BorderLayout.CENTER); + frame.add(mainPanel); + frame.setVisible(true); + frame.setSize(600,600); + } } diff --git a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/MenuGUI.java b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/MenuGUI.java index ea98b17..530ba66 100644 --- a/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/MenuGUI.java +++ b/PR2Projekt/src/de/hs_mannheim/informatik/pr2projekt/gui/MenuGUI.java @@ -51,7 +51,7 @@ public class MenuGUI { } JButton b = new JButton("Highscores"); b.addActionListener(e -> { - HighscoreGUI.highscoreScreen(); + HighscoreGUI.highscoreScreen(filepath); frame.dispose(); }); buttonPanel.add(b);