Added File Manager. Not working yet.

This commit is contained in:
Christopher Beckmann 2015-11-16 23:26:28 +01:00
parent 1e482e68f5
commit e640ae9b31
3 changed files with 116 additions and 3 deletions

View file

@ -1,17 +1,76 @@
package tu_darmstadt.sudoku.controller; package tu_darmstadt.sudoku.controller;
import android.content.Context;
import android.provider.MediaStore;
import android.util.Log;
import java.io.File; 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. * Created by Chris on 16.11.2015.
*/ */
public class FileManager { public class FileManager {
FileManager() {} Context context;
private static String savesFile = "saves.txt";
private static String highscoresFile = "highscores.txt";
void doSomething() { FileManager(Context context) {
//File f = new File("./level/sudoku.txt"); 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.");
}
} }
} }

View file

@ -10,6 +10,7 @@ import tu_darmstadt.sudoku.game.CellConflictList;
import tu_darmstadt.sudoku.game.GameBoard; import tu_darmstadt.sudoku.game.GameBoard;
import tu_darmstadt.sudoku.game.GameCell; import tu_darmstadt.sudoku.game.GameCell;
import tu_darmstadt.sudoku.game.GameType; 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.Solver;
import tu_darmstadt.sudoku.game.solver.ISolver; import tu_darmstadt.sudoku.game.solver.ISolver;
@ -346,5 +347,57 @@ public class GameController {
// l.onModelChanged(); // 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<StringBuilder>() {
@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<StringBuilder>() {
@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<StringBuilder>() {
@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();
}
} }

View file

@ -205,4 +205,5 @@ public class GameBoard implements Cloneable {
} }
return sb.toString(); return sb.toString();
} }
} }