diff --git a/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/facade/HitoriAPI.java b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/facade/HitoriAPI.java new file mode 100644 index 0000000..784177a --- /dev/null +++ b/PR2Projekt/src/main/java/de/hs_mannheim/informatik/mvn/facade/HitoriAPI.java @@ -0,0 +1,128 @@ +package de.hs_mannheim.informatik.mvn.facade; + +import java.awt.CardLayout; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.Stack; +import javax.swing.JButton; +import javax.swing.JPanel; +import de.hs_mannheim.informatik.mvn.domain.*; +import de.hs_mannheim.informatik.mvn.gui.*; + +public class HitoriAPI { + + public static void starter() { + new MenuGUI(); + } + + public void paintGameCatcher(InputStream gameStream, CardLayout cl, JPanel main, JButton[][] buttons, + String[][] colors, Stack madeMoves, String[][] data, String path) throws IOException { + new GameGUI().paintGame(gameStream, cl, main, buttons, colors, madeMoves, data, path); + } + + public static String[] getCordsCatcher(int i, int j) { + String[] pos = new HitoriMain().getCords(i,j); + return pos; + } + + public void totalResetButtonCatcher(InputStream inputStream, CardLayout cl, JPanel main, JButton[][] buttons, + String[][] colors, Stack madeMoves, String[][] data, String path0) throws FileNotFoundException { + new GameGUI().totalResetButton(inputStream, cl, main, buttons, colors, madeMoves, data, path0); + } + + public boolean abgabeMöglichCatcher(InputStream newStream, String[][] data, String[][] colors) throws FileNotFoundException { + boolean abgabeMöglich = new HitoriMain().abgabeMöglich(newStream, data, colors); + return abgabeMöglich; + } + + public void sortByTimeCatcher(byte[] data, String filename) throws IOException { + new HighscoreManager().sortByTime(data, filename); + } + + public String getAvgTimeCatcher(byte[] data, String filename) throws IOException { + String avgTime = new HighscoreManager().getAvgTime(data, filename); + return avgTime; + } + + public void ablauf(CardLayout cl, JPanel main, InputStream inputStream, int rows, String path) throws IOException { + Stack madeMoves = new Stack<>(); + byte[] gameData = inputStream.readAllBytes(); + InputStream newStream = new ByteArrayInputStream(gameData); + String[][] data = new HitoriMain().getData(newStream, rows); + String[][] colors = new HitoriMain().makeColorArray(data.length); + + JButton[][] buttons = new GameGUI().makeButtonArray(data); + InputStream gameStream = new ByteArrayInputStream(gameData); + new GameGUI().paintGame(gameStream, cl, main, buttons, colors, madeMoves, data, path); + } + + public void repaintGameCatcher(String[][] data, InputStream inputStream, CardLayout cl, JPanel main, + String[][] colors, Stack madeMoves, String[][] data2, String path) { + JButton[][] buttons = new GameGUI().makeButtonArray(data); + try { + new GameGUI().paintGame(inputStream, cl, main, buttons, colors, madeMoves, data, path); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void copyResourceIfNotExistsCatcher(String resourcePathInJar, File outFile) throws IOException { + new HighscoreManager().copyResourceIfNotExists(resourcePathInJar, outFile); + } + + public String[] getGameButtonNamesCatcher() { + String[] buttonNames = new HitoriMain().getGameButtonNames(); + return buttonNames; + } + + public String[] getGamePathCatcher() { + String[] pathNames = new HitoriMain().getGamePath(); + return pathNames; + } + + public void loadGame(String[] filepath, CardLayout cl, JPanel main, byte[] gameData) { + try (InputStream inputStream = GameGUI.class.getResourceAsStream(filepath[0])) { + if (inputStream != null) { + String path=filepath[0]; + ablauf(cl, main, inputStream, Integer.parseInt(filepath[1]), path); + } else { + throw new FileNotFoundException("Resource not found: " + filepath[0]); + } + } catch (IOException e1) { + e1.getMessage(); + } + } + + public String[] getHighscorePathNamesCatcher() { + String[] pathNames = new HitoriMain().getHighscorePathNames(); + return pathNames; + } + + public String[] getHighscoreButtonNamesCatcher() { + String[] buttonNames = new HitoriMain().getHighscoreButtonNames(); + return buttonNames; + } + + public void loadHighscore(int index, String[] pathNames, CardLayout cl, JPanel main) { + String resourcePathInJar = pathNames[index]; + String localFileName = resourcePathInJar.substring(resourcePathInJar.lastIndexOf('/') + 1); + File outFile = new File("resources/Hitori_Highscores", localFileName); + try { + copyResourceIfNotExistsCatcher(resourcePathInJar, outFile); + byte[] data = Files.readAllBytes(outFile.toPath()); + new HighscoreTableGUI().showHighscores(cl, main, data, outFile.getPath()); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + + + + + + +}