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 new file mode 100644 index 0000000..6f8f352 --- /dev/null +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/domain/HitoriMain.java @@ -0,0 +1,235 @@ +package 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.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +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 gui.GameGUI; +import gui.MenuGUI; + +public class HitoriMain extends JFrame implements ActionListener{ + + private static String[] filepath = {"", ""}; + + public static void main(String[] args) throws FileNotFoundException{ + new MenuGUI(); + } + + public static void ablauf(CardLayout cl, JPanel main, String[] filepath) throws FileNotFoundException { + Stack 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 ArrayList readFromFile(String path){ + ArrayList lines = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(path))) { + String line; + while ((line = reader.readLine()) != null) { + lines.add(line); + } + } catch (IOException e) { + e.printStackTrace(); + } + return lines; + } + + public static String[][] getData(String filepath, int rows) throws FileNotFoundException{ + Scanner sc = new Scanner(new File(filepath)); + 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(); + return data; + } + + + public static String[][] makeColorArray(int size){ + String[][] colors = new String[size][size]; + for(int i=0;i filteredData = getSolution(path, result); + String[][] ergebnis1 = getErgebnisArray(result, filteredData); + int count = 0; + for(int i=0;i madeMoves,String[][] data) throws FileNotFoundException{ + madeMoves.clear(); + for(int i = 0; i getSolution(String path, String[][] result) throws FileNotFoundException{ + Scanner sc = new Scanner(new File(path)); + ArrayList 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(); + return filteredData; + } + + public static String[][] getErgebnisArray(String[][] result, ArrayList filteredData){ + String[][] ergebnis = new String[result.length][result.length]; + for(int i=0;i= 0 && row < ergebnis.length && col >= 0 && col < ergebnis[row].length) { + ergebnis[row][col] = "B"; + } + } + return ergebnis; + } + + public static void sortByTime(String path) throws FileNotFoundException, IOException{ + File file = new File(path); + ArrayList entries = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(file))) { + 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); + + entries.add(new Entry(time, name)); + } + } + + entries.sort(Comparator.comparing(e -> e.time)); + + try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { + for (Entry e : entries) { + String formattedTime = String.format("%02d:%02d", e.time.getHour(), e.time.getMinute()); + bw.write(formattedTime + " " + e.name); + bw.newLine(); + } + } + } + + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + + } +} + +class Entry { + LocalTime time; + String name; + + Entry(LocalTime time, String name) { + this.time = time; + this.name = name; + } +}