LevelLoad from File implemented.
LevelSave to File implemented. We can now continue a saved level. Choice of GameType and difficulty is now saved through App restarts.
This commit is contained in:
parent
93cf408ea5
commit
5ba2ee98a1
9 changed files with 451 additions and 152 deletions
|
@ -1,18 +1,17 @@
|
|||
package tu_darmstadt.sudoku.controller;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.MediaStore;
|
||||
import android.content.SharedPreferences;
|
||||
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 java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import tu_darmstadt.sudoku.game.GameBoard;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
import tu_darmstadt.sudoku.game.GameInfoContainer;
|
||||
|
||||
/**
|
||||
* Created by Chris on 16.11.2015.
|
||||
|
@ -20,15 +19,91 @@ import tu_darmstadt.sudoku.game.GameType;
|
|||
public class FileManager {
|
||||
|
||||
Context context;
|
||||
private static String savesFile = "saves.txt";
|
||||
private static String highscoresFile = "highscores.txt";
|
||||
private SharedPreferences settings;
|
||||
|
||||
FileManager(Context context) {
|
||||
private static String FILE_EXTENSION = ".txt";
|
||||
private static String SAVE_PREFIX = "save_";
|
||||
private static String SAVES_DIR = "saves";
|
||||
private static String highscoresDir = "highscores";
|
||||
|
||||
private static List<GameInfoContainer> list = new LinkedList<>();
|
||||
|
||||
public FileManager(Context context, SharedPreferences settings) {
|
||||
this.context = context;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public String loadGameState() {
|
||||
File dir = context.getFilesDir();
|
||||
public static List<GameInfoContainer> getLoadableGameList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public List<GameInfoContainer> loadGameStateInfo() {
|
||||
if(!settings.getBoolean("savesChanged", false)) {
|
||||
return list;
|
||||
}
|
||||
File dir = context.getDir(SAVES_DIR, 0);
|
||||
|
||||
List<GameInfoContainer> result = new LinkedList<>();
|
||||
|
||||
// go through every file
|
||||
for(File file : dir.listFiles()) {
|
||||
|
||||
// filter so we only work with actual files
|
||||
if(file.isFile()) {
|
||||
|
||||
// create a new GameInfoContainer
|
||||
GameInfoContainer gic = new GameInfoContainer();
|
||||
|
||||
// load file
|
||||
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.");
|
||||
}
|
||||
// start parsing
|
||||
String gameString = new String(bytes);
|
||||
String[] values = gameString.split("/");
|
||||
|
||||
//String[] levels = saves.split("###");
|
||||
try {
|
||||
if(values.length < 4) {
|
||||
throw new IllegalArgumentException("Can not load game info. File seems to be damaged or incomplete.");
|
||||
}
|
||||
|
||||
// fill the container
|
||||
String id = file.getName().substring(5, file.getName().lastIndexOf("."));
|
||||
gic.setID(Integer.valueOf(id)); // save_x.txt
|
||||
gic.parseGameType(values[0]);
|
||||
gic.parseFixedValues(values[1]);
|
||||
gic.parseSetValues(values[2]);
|
||||
gic.parseNotes(values[3]);
|
||||
} catch(IllegalArgumentException e) {
|
||||
file.delete();
|
||||
continue;
|
||||
}
|
||||
|
||||
// then add it to the list
|
||||
result.add(gic);
|
||||
}
|
||||
}
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putBoolean("savesChanged", false);
|
||||
editor.commit();
|
||||
|
||||
list = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
/*public String loadGameState(int index) {
|
||||
File dir = context.getDir(SAVES_DIR, 0);
|
||||
|
||||
dir.listFiles();
|
||||
|
||||
File file = new File(dir, savesFile);
|
||||
|
||||
|
@ -49,19 +124,18 @@ public class FileManager {
|
|||
for(String level : levels) {
|
||||
String[] values = level.split("|");
|
||||
GameType type = Enum.valueOf(GameType.class, values[0]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return saves;
|
||||
}
|
||||
}*/
|
||||
|
||||
public void saveGameState(GameController controller) {
|
||||
String level = controller.getStringRepresentation();
|
||||
String level = GameInfoContainer.getGameInfo(controller);
|
||||
|
||||
File dir = context.getFilesDir();
|
||||
File dir = context.getDir(SAVES_DIR, 0);
|
||||
|
||||
File file = new File(dir, savesFile);
|
||||
//controller.getGameID()
|
||||
|
||||
File file = new File(dir, SAVE_PREFIX+String.valueOf(controller.getGameID())+FILE_EXTENSION);
|
||||
|
||||
try {
|
||||
FileOutputStream stream = new FileOutputStream(file);
|
||||
|
@ -73,5 +147,10 @@ public class FileManager {
|
|||
} catch(IOException e) {
|
||||
Log.e("File Manager", "Could not save game. IOException occured.");
|
||||
}
|
||||
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putBoolean("savesChanged", true);
|
||||
editor.commit();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package tu_darmstadt.sudoku.controller;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
@ -9,6 +10,7 @@ import tu_darmstadt.sudoku.game.CellConflict;
|
|||
import tu_darmstadt.sudoku.game.CellConflictList;
|
||||
import tu_darmstadt.sudoku.game.GameBoard;
|
||||
import tu_darmstadt.sudoku.game.GameCell;
|
||||
import tu_darmstadt.sudoku.game.GameInfoContainer;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
import tu_darmstadt.sudoku.game.ICellAction;
|
||||
import tu_darmstadt.sudoku.game.solver.Solver;
|
||||
|
@ -28,6 +30,7 @@ public class GameController {
|
|||
private int selectedRow;
|
||||
private int selectedCol;
|
||||
private SharedPreferences settings;
|
||||
private int gameID = 0;
|
||||
private CellConflictList errorList = new CellConflictList();
|
||||
//private LinkedList<IModelChangeListener> listeners = new LinkedList<>();
|
||||
|
||||
|
@ -48,19 +51,23 @@ public class GameController {
|
|||
setSettings(pref);
|
||||
}
|
||||
|
||||
public int getGameID() {
|
||||
return gameID;
|
||||
}
|
||||
|
||||
public void loadNewLevel(GameType type, int difficulty) {
|
||||
switch(type) {
|
||||
case Default_6x6:
|
||||
loadLevel(GameType.Default_6x6,
|
||||
loadLevel(new GameInfoContainer(1, GameType.Default_6x6,
|
||||
new int[]{1,0,0,0,0,6,
|
||||
4,0,6,1,0,0,
|
||||
0,0,2,3,0,5,
|
||||
0,4,0,0,1,0,
|
||||
0,6,0,2,0,0,
|
||||
0,3,0,5,0,1}, null,null);
|
||||
0,3,0,5,0,1}, null,null));
|
||||
break;
|
||||
case Default_12x12:
|
||||
loadLevel(GameType.Default_12x12,
|
||||
loadLevel(new GameInfoContainer(2, GameType.Default_12x12,
|
||||
new int[] {0, 2, 1, 0, 0, 6, 0, 0, 0, 8, 9, 0,
|
||||
10, 0,12, 0, 0, 2, 1,11, 0, 0, 0, 6,
|
||||
6, 0, 0, 4, 0,12, 0, 0, 0, 0, 2, 1,
|
||||
|
@ -73,12 +80,12 @@ public class GameController {
|
|||
1, 5, 0, 0, 0, 0, 4, 0,10, 0, 0,11,
|
||||
9, 0, 0, 0, 1,10, 2, 0, 0, 6, 0, 7,
|
||||
0, 6,10, 0, 0, 0, 8, 0, 0, 1,12, 0}
|
||||
,null, null);
|
||||
,null, null));
|
||||
break;
|
||||
case Default_9x9:
|
||||
case Unspecified:
|
||||
default:
|
||||
loadLevel(GameType.Default_9x9,
|
||||
loadLevel(new GameInfoContainer(3, GameType.Default_9x9,
|
||||
new int[]{5, 0, 1, 9, 0, 0, 0, 0, 0,
|
||||
2, 0, 0, 0, 0, 4, 9, 5, 0,
|
||||
3, 9, 0, 7, 0, 0, 0, 2, 6,
|
||||
|
@ -88,12 +95,17 @@ public class GameController {
|
|||
0, 0, 0, 0, 7, 0, 4, 0, 9,
|
||||
6, 4, 0, 0, 0, 0, 0, 0, 0,
|
||||
7, 0, 0, 0, 1, 0, 3, 0, 5}
|
||||
, null, null);
|
||||
, null, null));
|
||||
}
|
||||
}
|
||||
|
||||
public void loadLevel(GameType type, int[] fixedValues, int[] setValues, boolean[][] setNotes) {
|
||||
setGameType(type);
|
||||
public void loadLevel(GameInfoContainer gic) {
|
||||
int[] fixedValues = gic.getFixedValues();
|
||||
int[] setValues = gic.getSetValues();
|
||||
boolean[][] setNotes = gic.getSetNotes();
|
||||
this.gameID = gic.getID();
|
||||
|
||||
setGameType(gic.getGameType());
|
||||
this.gameBoard = new GameBoard(size, sectionHeight, sectionWidth);
|
||||
|
||||
if(fixedValues == null) throw new IllegalArgumentException("fixedValues may not be null.");
|
||||
|
@ -116,7 +128,7 @@ public class GameController {
|
|||
int col = i % size;
|
||||
for(int k = 0 ; k < size; k++) {
|
||||
if(setNotes[i][k]) {
|
||||
setNote(row, col, k);
|
||||
setNote(row, col, k+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -152,29 +164,9 @@ public class GameController {
|
|||
|
||||
private void setGameType(GameType type) {
|
||||
this.gameType = type;
|
||||
switch(type) {
|
||||
case Default_9x9:
|
||||
this.size = 9;
|
||||
this.sectionHeight = 3;
|
||||
this.sectionWidth = 3;
|
||||
break;
|
||||
case Default_12x12:
|
||||
this.size = 12;
|
||||
this.sectionHeight = 3;
|
||||
this.sectionWidth = 4;
|
||||
break;
|
||||
case Default_6x6:
|
||||
this.size = 6;
|
||||
this.sectionHeight = 2;
|
||||
this.sectionWidth = 3;
|
||||
break;
|
||||
case Unspecified:
|
||||
default:
|
||||
this.size = 1;
|
||||
this.sectionHeight = 1;
|
||||
this.sectionWidth = 1;
|
||||
throw new IllegalArgumentException("GameType can not be unspecified.");
|
||||
}
|
||||
this.size = GameType.getSize(type);
|
||||
this.sectionHeight = GameType.getSectionHeight(type);
|
||||
this.sectionWidth = GameType.getSectionWidth(type);
|
||||
}
|
||||
|
||||
/** Use with care.
|
||||
|
@ -228,6 +220,32 @@ public class GameController {
|
|||
return cell.getValue();
|
||||
}
|
||||
|
||||
public GameType getGameType() {
|
||||
return gameType;
|
||||
}
|
||||
|
||||
public <T> T actionOnCells(ICellAction<T> ca, T existing) {
|
||||
return gameBoard.actionOnCells(ca,existing);
|
||||
}
|
||||
|
||||
public void saveGame(Context context) {
|
||||
if(settings == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(gameID == 0) {
|
||||
gameID = settings.getInt("lastGameID", 0)+1;
|
||||
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putInt("lastGameID", gameID);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
//gameID now has a value other than 0 and hopefully unique
|
||||
FileManager fm = new FileManager(context, settings);
|
||||
fm.saveGameState(this);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
@ -262,8 +280,10 @@ public class GameController {
|
|||
}
|
||||
|
||||
public void setNote(int row, int col, int value) {
|
||||
if(isValidNumber(value)) {
|
||||
GameCell c = gameBoard.getCell(row, col);
|
||||
c.setNote(value);
|
||||
}
|
||||
//notifyListeners();
|
||||
}
|
||||
|
||||
|
@ -347,57 +367,4 @@ public class GameController {
|
|||
// l.onModelChanged();
|
||||
// }
|
||||
// }
|
||||
public String getStringRepresentation() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("###"); // TODO add some game information
|
||||
|
||||
sb.append(gameType.name());
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.List;
|
|||
public class GameBoard implements Cloneable {
|
||||
|
||||
//private int id;
|
||||
private GameType gameType;
|
||||
private int sectionHeight;
|
||||
private int sectionWidth;
|
||||
//private List additionalSections
|
||||
|
|
|
@ -0,0 +1,152 @@
|
|||
package tu_darmstadt.sudoku.game;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import tu_darmstadt.sudoku.controller.GameController;
|
||||
|
||||
/**
|
||||
* Created by Chris on 17.11.2015.
|
||||
*/
|
||||
public class GameInfoContainer {
|
||||
|
||||
GameType gameType;
|
||||
int ID;
|
||||
int time;
|
||||
int difficulty;
|
||||
int[] fixedValues;
|
||||
int[] setValues;
|
||||
boolean[][] setNotes;
|
||||
|
||||
public GameInfoContainer() {}
|
||||
public GameInfoContainer(int ID, GameType gameType, int[] fixedValues, int[] setValues, boolean[][] setNotes) {
|
||||
this.ID = ID;
|
||||
this.gameType = gameType;
|
||||
this.fixedValues = fixedValues;
|
||||
this.setValues = setValues;
|
||||
this.setNotes = setNotes;
|
||||
}
|
||||
|
||||
public void setID(int ID) {
|
||||
this.ID = ID;
|
||||
}
|
||||
|
||||
public void parseGameType(String s) {
|
||||
gameType = Enum.valueOf(GameType.class, s);
|
||||
}
|
||||
|
||||
public void parseFixedValues(String s){
|
||||
fixedValues = new int[s.length()];
|
||||
for(int i = 0; i < s.length(); i++) {
|
||||
fixedValues[i] = Integer.parseInt(s.charAt(i)+"");
|
||||
}
|
||||
}
|
||||
|
||||
public void parseSetValues(String s) {
|
||||
setValues = new int[s.length()];
|
||||
for(int i = 0; i < s.length(); i++) {
|
||||
setValues[i] = Integer.parseInt(s.charAt(i)+"");
|
||||
}
|
||||
}
|
||||
|
||||
public void parseNotes(String s) {
|
||||
String[] strings = s.split("-");
|
||||
setNotes = new boolean[strings.length][strings[0].length()];
|
||||
for(int i = 0; i < strings.length; i++) {
|
||||
for(int k = 0; k < strings[i].length(); k++) {
|
||||
setNotes[i][k] = (strings[i].charAt(k)) == '1' ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GameType getGameType() {
|
||||
return gameType;
|
||||
}
|
||||
|
||||
public int[] getFixedValues() {
|
||||
return fixedValues;
|
||||
}
|
||||
|
||||
public int[] getSetValues() {
|
||||
return setValues;
|
||||
}
|
||||
|
||||
public boolean[][] getSetNotes() {
|
||||
return setNotes;
|
||||
}
|
||||
|
||||
public int getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public static String getGameInfo(GameController controller) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// TODO add some game information
|
||||
sb.append(controller.getGameType().name());
|
||||
sb.append("/");
|
||||
sb.append(getFixedCells(controller));
|
||||
sb.append("/");
|
||||
sb.append(getSetCells(controller));
|
||||
sb.append("/");
|
||||
sb.append(getNotes(controller));
|
||||
|
||||
String result = sb.toString();
|
||||
|
||||
Log.d("getGameInfo", result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String getFixedCells(GameController controller) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
controller.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);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String getSetCells(GameController controller) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
controller.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);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String getNotes(GameController controller) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
controller.actionOnCells(new ICellAction<StringBuilder>() {
|
||||
@Override
|
||||
public StringBuilder action(GameCell gc, StringBuilder existing) {
|
||||
for (Boolean b : gc.getNotes()) {
|
||||
existing.append( b ? '1' : '0' );
|
||||
}
|
||||
existing.append("-");
|
||||
return existing;
|
||||
}
|
||||
}, sb);
|
||||
sb.deleteCharAt(sb.lastIndexOf("-"));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -10,7 +10,9 @@ public enum GameType {
|
|||
Unspecified,
|
||||
Default_9x9,
|
||||
Default_12x12,
|
||||
Default_6x6;
|
||||
Default_6x6,
|
||||
X_9x9,
|
||||
Hyper_9x9;
|
||||
|
||||
public static List<GameType> getValidGameTypes() {
|
||||
LinkedList<GameType> result = new LinkedList<>();
|
||||
|
@ -20,5 +22,51 @@ public enum GameType {
|
|||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static int getSize(GameType type) {
|
||||
switch(type) {
|
||||
case X_9x9:
|
||||
case Hyper_9x9:
|
||||
case Default_9x9:
|
||||
return 9;
|
||||
case Default_12x12:
|
||||
return 12;
|
||||
case Default_6x6:
|
||||
return 6;
|
||||
case Unspecified:
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getSectionHeight(GameType type) {
|
||||
switch(type) {
|
||||
case X_9x9:
|
||||
case Hyper_9x9:
|
||||
case Default_9x9:
|
||||
return 3;
|
||||
case Default_12x12:
|
||||
return 3;
|
||||
case Default_6x6:
|
||||
return 2;
|
||||
case Unspecified:
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static int getSectionWidth(GameType type) {
|
||||
switch(type) {
|
||||
case X_9x9:
|
||||
case Hyper_9x9:
|
||||
case Default_9x9:
|
||||
return 3;
|
||||
case Default_12x12:
|
||||
return 4;
|
||||
case Default_6x6:
|
||||
return 3;
|
||||
case Unspecified:
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,11 @@ import android.support.v7.app.AppCompatActivity;
|
|||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import tu_darmstadt.sudoku.controller.FileManager;
|
||||
import tu_darmstadt.sudoku.controller.GameController;
|
||||
import tu_darmstadt.sudoku.game.GameInfoContainer;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
import tu_darmstadt.sudoku.ui.view.R;
|
||||
import tu_darmstadt.sudoku.ui.view.SudokuFieldLayout;
|
||||
|
@ -25,7 +29,6 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
GameController gameController;
|
||||
SudokuFieldLayout layout;
|
||||
SudokuKeyboardLayout keyboard;
|
||||
SudokuButton [] buttons;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -33,6 +36,7 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
|
||||
GameType gameType = GameType.Unspecified;
|
||||
int gameDifficulty = 0;
|
||||
int loadLevel = 0;
|
||||
|
||||
Bundle extras = getIntent().getExtras();
|
||||
if (extras != null) {
|
||||
|
@ -41,19 +45,9 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
gameType = (GameType)extras.get("gameType");
|
||||
}
|
||||
gameDifficulty = extras.getInt("gameDifficulty");
|
||||
|
||||
loadLevel = extras.getInt("loadLevel");
|
||||
}
|
||||
|
||||
// TODO: DEBUG START
|
||||
/*if(gameDifficulty == 0) {
|
||||
gameType = GameType.Default_6x6;
|
||||
} else if(gameDifficulty == 5) {
|
||||
gameType = GameType.Default_12x12;
|
||||
} else {
|
||||
gameType = GameType.Default_9x9;
|
||||
}*/
|
||||
// TODO: DEBUG END
|
||||
|
||||
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
setContentView(R.layout.activity_game_view);
|
||||
|
@ -65,7 +59,15 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
layout = (SudokuFieldLayout)findViewById(R.id.sudokuLayout);
|
||||
gameController = new GameController(sharedPref);
|
||||
|
||||
List<GameInfoContainer> loadableGames = FileManager.getLoadableGameList();
|
||||
|
||||
if(loadLevel != 0 && loadableGames.size() >= loadLevel) {
|
||||
// load level from FileManager
|
||||
gameController.loadLevel(loadableGames.get(loadLevel-1));
|
||||
} else {
|
||||
// load a new level
|
||||
gameController.loadNewLevel(gameType, gameDifficulty);
|
||||
}
|
||||
|
||||
layout.setGame(gameController);
|
||||
layout.setSettings(sharedPref);
|
||||
|
@ -103,6 +105,8 @@ public class GameActivity extends AppCompatActivity implements NavigationView.On
|
|||
if (drawer.isDrawerOpen(GravityCompat.START)) {
|
||||
drawer.closeDrawer(GravityCompat.START);
|
||||
} else {
|
||||
gameController.saveGame(getBaseContext());
|
||||
finish();
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package tu_darmstadt.sudoku.ui;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
|
||||
|
@ -18,13 +20,17 @@ import android.widget.Button;
|
|||
import android.widget.RatingBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import tu_darmstadt.sudoku.controller.FileManager;
|
||||
import tu_darmstadt.sudoku.game.GameInfoContainer;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
import tu_darmstadt.sudoku.ui.view.R;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
GameType gameType = GameType.Default_9x9;
|
||||
int gameDifficulty = 1;
|
||||
RatingBar difficultyBar;
|
||||
SharedPreferences settings;
|
||||
|
||||
/**
|
||||
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
||||
|
@ -44,6 +50,9 @@ public class MainActivity extends AppCompatActivity {
|
|||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
settings = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
setContentView(R.layout.activity_main_menu);
|
||||
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
|
@ -53,15 +62,27 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
||||
|
||||
|
||||
// Set up the ViewPager with the sections adapter.
|
||||
mViewPager = (ViewPager) findViewById(R.id.scroller);
|
||||
mViewPager.setAdapter(mSectionsPagerAdapter);
|
||||
|
||||
mViewPager.setCurrentItem(1);
|
||||
// set default gametype choice to whatever was chosen the last time.
|
||||
List<GameType> validGameTypes = GameType.getValidGameTypes();
|
||||
String lastChosenGameType = settings.getString("lastChosenGameType", GameType.Default_9x9.name());
|
||||
int index = validGameTypes.indexOf(Enum.valueOf(GameType.class, lastChosenGameType));
|
||||
mViewPager.setCurrentItem(index);
|
||||
|
||||
Button continueButton = (Button)findViewById(R.id.continueButton);
|
||||
continueButton.setEnabled(false);
|
||||
// Set the difficulty Slider to whatever was chosen the last time
|
||||
difficultyBar = (RatingBar)findViewById(R.id.difficultyBar);
|
||||
int lastChosenDifficulty = settings.getInt("lastChosenDifficulty", 1);
|
||||
difficultyBar.setProgress(lastChosenDifficulty);
|
||||
|
||||
// on first create always check for loadable levels!
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putBoolean("savesChanged", true);
|
||||
editor.commit();
|
||||
refreshContinueButton();
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
|
@ -77,7 +98,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
case R.id.continueButton:
|
||||
// TODO continue from file.
|
||||
i = new Intent(this, GameActivity.class);
|
||||
int levelNr = 0;
|
||||
int levelNr = 1;
|
||||
i.putExtra("loadLevel", levelNr);
|
||||
break;
|
||||
case R.id.highscoreButton:
|
||||
|
@ -90,9 +111,14 @@ public class MainActivity extends AppCompatActivity {
|
|||
// TODO: create help page.. what is supposed to be in there?!
|
||||
break;
|
||||
case R.id.playButton:
|
||||
gameType = GameType.getValidGameTypes().get(mViewPager.getCurrentItem());
|
||||
RatingBar difficultyBar = (RatingBar)findViewById(R.id.difficultyBar);
|
||||
gameDifficulty = difficultyBar.getProgress();
|
||||
GameType gameType = GameType.getValidGameTypes().get(mViewPager.getCurrentItem());
|
||||
int gameDifficulty = difficultyBar.getProgress();
|
||||
|
||||
// save current setting for later
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putString("lastChosenGameType", gameType.name());
|
||||
editor.putInt("lastChosenDifficulty", gameDifficulty);
|
||||
editor.commit();
|
||||
|
||||
// send everything to game activity
|
||||
i = new Intent(this, GameActivity.class);
|
||||
|
@ -107,6 +133,25 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
refreshContinueButton();
|
||||
}
|
||||
|
||||
private void refreshContinueButton() {
|
||||
// enable continue button if we have saved games.
|
||||
Button continueButton = (Button)findViewById(R.id.continueButton);
|
||||
FileManager fm = new FileManager(getBaseContext(), settings);
|
||||
List<GameInfoContainer> gic = fm.loadGameStateInfo();
|
||||
if(gic.size() > 0) {
|
||||
continueButton.setEnabled(true);
|
||||
} else {
|
||||
continueButton.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
// Handle action bar item clicks here. The action bar will
|
||||
|
|
|
@ -3,6 +3,7 @@ package tu_darmstadt.sudoku.controller;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import tu_darmstadt.sudoku.game.GameInfoContainer;
|
||||
import tu_darmstadt.sudoku.game.GameType;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
@ -18,7 +19,7 @@ public class GameControllerTest {
|
|||
@Before
|
||||
public void init() {
|
||||
controller = new GameController();
|
||||
controller. loadLevel(GameType.Default_9x9,
|
||||
controller.loadLevel(new GameInfoContainer(3, GameType.Default_9x9,
|
||||
new int[]{5, 0, 1, 9, 0, 0, 0, 0, 0,
|
||||
2, 0, 0, 0, 0, 4, 9, 5, 0,
|
||||
3, 9, 0, 7, 0, 0, 0, 2, 6,
|
||||
|
@ -27,9 +28,10 @@ public class GameControllerTest {
|
|||
0, 7, 2, 0, 0, 9, 0, 4, 1,
|
||||
0, 0, 0, 0, 7, 0, 4, 0, 9,
|
||||
6, 4, 0, 0, 0, 0, 0, 0, 0,
|
||||
7, 0, 0, 0, 1, 0, 3, 0, 5}, null, null);
|
||||
7, 0, 0, 0, 1, 0, 3, 0, 5}
|
||||
, null, null));
|
||||
controller2 = new GameController();
|
||||
controller2.loadLevel(GameType.Default_12x12,
|
||||
controller2.loadLevel(new GameInfoContainer(2, GameType.Default_12x12,
|
||||
new int[]{0, 2, 1, 0, 0, 6, 0, 0, 0, 8, 9, 0,
|
||||
10, 0, 12, 0, 0, 2, 1, 11, 0, 0, 0, 6,
|
||||
6, 0, 0, 4, 0, 12, 0, 0, 0, 0, 2, 1,
|
||||
|
@ -42,7 +44,8 @@ public class GameControllerTest {
|
|||
1, 5, 0, 0, 0, 0, 4, 0, 10, 0, 0, 11,
|
||||
9, 0, 0, 0, 1, 10, 2, 0, 0, 6, 0, 7,
|
||||
0, 6, 10, 0, 0, 0, 8, 0, 0, 1, 12, 0}
|
||||
,null, null);
|
||||
, null, null));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ public class SolverTest {
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
controller = new GameController();
|
||||
/*controller = new GameController();
|
||||
int[][] level = {{ 5, 0, 1, 9, 0, 0, 0, 0, 0 },
|
||||
{ 2, 0, 0, 0, 0, 4, 9, 5, 0 },
|
||||
{ 3, 9, 0, 7, 0, 0, 0, 2, 6 },
|
||||
|
@ -31,7 +31,7 @@ public class SolverTest {
|
|||
controller.loadLevel(GameType.Default_9x9,
|
||||
new int[]{0,0,0,0,4,1,0,0,0,0,6,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,3,2,0,6,0,0,0,0,0,0,0,0,0,5,0,0,4,1,7,0,0,0,0,0,0,0,0,0,0,0,2,0,0,3,0,0,0,4,8,0,0,0,0,0,0,5,0,1,0,0,0,0,0,0},
|
||||
null,
|
||||
null);
|
||||
null);*/
|
||||
}
|
||||
|
||||
//000041000060000200000000000320600000000050041700000000000200300048000000501000000
|
||||
|
|
Loading…
Reference in a new issue