Merge branch 'Sudoku-v3.0' of https://github.com/SecUSo/privacy-friendly-sudoku into Sudoku-v3.0

This commit is contained in:
ErikWaegerle 2020-05-26 21:59:36 +02:00
commit af812f6bed
4 changed files with 58 additions and 2 deletions

View file

@ -6,6 +6,8 @@ import android.os.Handler;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import org.secuso.privacyfriendlysudoku.controller.database.DatabaseHelper;
import org.secuso.privacyfriendlysudoku.controller.database.model.DailySudoku;
import org.secuso.privacyfriendlysudoku.controller.helper.GameInfoContainer; import org.secuso.privacyfriendlysudoku.controller.helper.GameInfoContainer;
import org.secuso.privacyfriendlysudoku.game.CellConflict; import org.secuso.privacyfriendlysudoku.game.CellConflict;
import org.secuso.privacyfriendlysudoku.game.CellConflictList; import org.secuso.privacyfriendlysudoku.game.CellConflictList;
@ -19,7 +21,10 @@ import org.secuso.privacyfriendlysudoku.game.listener.IHighlightChangedListener;
import org.secuso.privacyfriendlysudoku.game.listener.IHintListener; import org.secuso.privacyfriendlysudoku.game.listener.IHintListener;
import org.secuso.privacyfriendlysudoku.game.listener.IModelChangedListener; import org.secuso.privacyfriendlysudoku.game.listener.IModelChangedListener;
import org.secuso.privacyfriendlysudoku.game.listener.ITimerListener; import org.secuso.privacyfriendlysudoku.game.listener.ITimerListener;
import org.secuso.privacyfriendlysudoku.ui.GameActivity;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
@ -32,6 +37,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class GameController implements IModelChangedListener, Parcelable { public class GameController implements IModelChangedListener, Parcelable {
// General // General
public static final int DAILY_SUDOKU_ID = Integer.MAX_VALUE - 1;
private SharedPreferences settings; private SharedPreferences settings;
// View // View
@ -305,7 +311,7 @@ public class GameController implements IModelChangedListener, Parcelable {
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
// is anyone ever gonna play so many levels? :) // is anyone ever gonna play so many levels? :)
if(gameID == Integer.MAX_VALUE-1) { if(gameID == DAILY_SUDOKU_ID - 1) {
editor.putInt("lastGameID", 1); editor.putInt("lastGameID", 1);
} else { } else {
editor.putInt("lastGameID", gameID); editor.putInt("lastGameID", gameID);
@ -318,6 +324,24 @@ public class GameController implements IModelChangedListener, Parcelable {
fm.saveGameState(this); fm.saveGameState(this);
} }
public void saveDailySudoku(Context context) {
int amountOfCells = size * size;
int[] encodedBoard = new int[amountOfCells];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
encodedBoard[i * size + j] = gameBoard.getCell(i, j).getValue();
}
}
Calendar currentDate = Calendar.getInstance();
int id = currentDate.get(Calendar.DAY_OF_MONTH) * 1000000
+ (currentDate.get(Calendar.MONTH) + 1) * 10000 + currentDate.get(Calendar.YEAR);
DatabaseHelper db = new DatabaseHelper(context);
DailySudoku dailySudoku = new DailySudoku(id, difficulty, gameType, encodedBoard, usedHints, GameActivity.timeToString(time));
db.addDailySudoku(dailySudoku);
}
public void deleteGame(Context context) { public void deleteGame(Context context) {
if(gameID == 0) { if(gameID == 0) {
throw new IllegalArgumentException("GameID may not be 0."); throw new IllegalArgumentException("GameID may not be 0.");

View file

@ -15,6 +15,9 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -75,6 +78,15 @@ public class NewLevelManager {
return false; return false;
} }
public int[] loadDailySudoku() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String toHash = "Sudoku/.PrivacyFriendly/." + dateFormat.format(date);
QQWingController controller = new QQWingController();
return controller.generateFromSeed(toHash.hashCode());
}
public int[] loadLevel(GameType type, GameDifficulty diff) { public int[] loadLevel(GameType type, GameDifficulty diff) {
Level level = dbHelper.getLevel(diff, type); Level level = dbHelper.getLevel(diff, type);
dbHelper.deleteLevel(level.getId()); dbHelper.deleteLevel(level.getId());

View file

@ -53,6 +53,20 @@ public class QQWingController {
return generated; return generated;
} }
public int[] generateFromSeed(int seed) {
generated.clear();
QQWing generator = new QQWing(GameType.Default_9x9, GameDifficulty.Unspecified);
generator.setRandom(seed);
generator.setRecordHistory(true);
generator.generatePuzzle();
generated.add(generator.getPuzzle());
opts.gameType = GameType.Default_9x9;
opts.gameDifficulty = generator.getDifficulty();
return generated.poll();
}
public int[] solve(GameBoard gameBoard) { public int[] solve(GameBoard gameBoard) {
level = new int[gameBoard.getSize()*gameBoard.getSize()]; level = new int[gameBoard.getSize()*gameBoard.getSize()];

View file

@ -369,6 +369,7 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
} }
}); });
shareDialog.show(getFragmentManager(), "ShareDialogFragment"); shareDialog.show(getFragmentManager(), "ShareDialogFragment");
break; break;
case R.id.nav_newgame: case R.id.nav_newgame:
@ -435,6 +436,11 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
gameController.deleteGame(this); gameController.deleteGame(this);
disableReset(); disableReset();
//Save solved sudoku, if it happens to be a daily sudoku, to daily sudoku database
if(gameController.getGameID() == GameController.DAILY_SUDOKU_ID) {
gameController.saveDailySudoku(GameActivity.this);
}
//Show time hints new plus old best time //Show time hints new plus old best time
statistics.saveGameStats(); statistics.saveGameStats();
@ -480,7 +486,7 @@ public class GameActivity extends BaseActivity implements NavigationView.OnNavig
specialButtonLayout.setButtonsEnabled(false); specialButtonLayout.setButtonsEnabled(false);
} }
public String timeToString(int time) { public static String timeToString(int time) {
int seconds = time % 60; int seconds = time % 60;
int minutes = ((time - seconds) / 60) % 60; int minutes = ((time - seconds) / 60) % 60;
int hours = (time - minutes - seconds) / (3600); int hours = (time - minutes - seconds) / (3600);