From e640ae9b31f04ed00f641f8a808fe88650b9982e Mon Sep 17 00:00:00 2001 From: Christopher Beckmann Date: Mon, 16 Nov 2015 23:26:28 +0100 Subject: [PATCH] Added File Manager. Not working yet. --- .../sudoku/controller/FileManager.java | 65 ++++++++++++++++++- .../sudoku/controller/GameController.java | 53 +++++++++++++++ .../tu_darmstadt/sudoku/game/GameBoard.java | 1 + 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/FileManager.java b/app/src/main/java/tu_darmstadt/sudoku/controller/FileManager.java index 409503e..d715115 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/FileManager.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/FileManager.java @@ -1,17 +1,76 @@ package tu_darmstadt.sudoku.controller; +import android.content.Context; +import android.provider.MediaStore; +import android.util.Log; + import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.StringBufferInputStream; + +import tu_darmstadt.sudoku.game.GameBoard; + /** * Created by Chris on 16.11.2015. */ public class FileManager { - FileManager() {} + Context context; + private static String savesFile = "saves.txt"; + private static String highscoresFile = "highscores.txt"; - void doSomething() { - //File f = new File("./level/sudoku.txt"); + FileManager(Context context) { + this.context = context; + } + + public String loadGameState() { + File dir = context.getFilesDir(); + + File file = new File(dir, savesFile); + + byte[] bytes = new byte[(int)file.length()]; + + try { + FileInputStream stream = new FileInputStream(file); + try { + stream.read(bytes); + } finally { + stream.close(); + } + } catch(IOException e) { + Log.e("File Manager", "Could not load game. IOException occured."); + } + String saves = new String(bytes); + String[] levels = saves.split("###"); + for(String level : levels) { + String[] values = level.split("|"); + int size = Integer.valueOf(values[0]); + + } + return saves; + } + public void saveGameState(GameController controller) { + String level = controller.getStringRepresentation(); + + File dir = context.getFilesDir(); + + File file = new File(dir, savesFile); + + try { + FileOutputStream stream = new FileOutputStream(file); + try { + stream.write(level.getBytes()); + } finally { + stream.close(); + } + } catch(IOException e) { + Log.e("File Manager", "Could not save game. IOException occured."); + } } } diff --git a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java index 1c4a668..52e9dd7 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java +++ b/app/src/main/java/tu_darmstadt/sudoku/controller/GameController.java @@ -10,6 +10,7 @@ import tu_darmstadt.sudoku.game.CellConflictList; import tu_darmstadt.sudoku.game.GameBoard; import tu_darmstadt.sudoku.game.GameCell; import tu_darmstadt.sudoku.game.GameType; +import tu_darmstadt.sudoku.game.ICellAction; import tu_darmstadt.sudoku.game.solver.Solver; import tu_darmstadt.sudoku.game.solver.ISolver; @@ -346,5 +347,57 @@ public class GameController { // l.onModelChanged(); // } // } +public String getStringRepresentation() { + StringBuilder sb = new StringBuilder(); + sb.append("###"); // TODO add some game information + + sb.append(gameType); + sb.append("|"); + + // add every fixed cell + gameBoard.actionOnCells(new ICellAction() { + @Override + public StringBuilder action(GameCell gc, StringBuilder existing) { + if (gc.isFixed()) { + existing.append(gc.getValue()); + } else { + existing.append(0); + } + return existing; + } + }, sb); + + // add a seperator + sb.append("|"); + + // Add every set cell + gameBoard.actionOnCells(new ICellAction() { + @Override + public StringBuilder action(GameCell gc, StringBuilder existing) { + if (gc.isFixed()) { + existing.append(0); + } else { + existing.append(gc.getValue()); + } + return existing; + } + }, sb); + + // add a seperator + sb.append("|"); + + // now add notes + gameBoard.actionOnCells(new ICellAction() { + @Override + public StringBuilder action(GameCell gc, StringBuilder existing) { + for (Boolean b : gc.getNotes()) { + existing.append(b); + } + return existing; + } + }, sb); + sb.append("\n\n"); + return sb.toString(); +} } diff --git a/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java b/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java index 0052979..11bad5c 100644 --- a/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java +++ b/app/src/main/java/tu_darmstadt/sudoku/game/GameBoard.java @@ -205,4 +205,5 @@ public class GameBoard implements Cloneable { } return sb.toString(); } + }