diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java index b656bb5..7aca04a 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java @@ -40,6 +40,9 @@ public class NewLevelManager { public static int PRE_SAVES_MIN = 3; public static int PRE_SAVES_MAX = 10; + private final double CHALLENGE_GENERATION_PROBABILITY = 0.9; + + public static NewLevelManager getInstance(Context context, SharedPreferences settings) { if(instance == null) { instance = new NewLevelManager(context, settings); @@ -80,11 +83,17 @@ public class NewLevelManager { public int[] loadDailySudoku() { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - Date date = new Date(); - String toHash = "Sudoku/.PrivacyFriendly/." + dateFormat.format(date); - + String toHash = "Sudoku/.PrivacyFriendly/." + dateFormat.format(new Date()); + boolean generateChallenge = new Random(toHash.hashCode()).nextDouble() >= CHALLENGE_GENERATION_PROBABILITY; QQWingController controller = new QQWingController(); - return controller.generateFromSeed(toHash.hashCode()); + int[] result; + + if (generateChallenge) { + result = controller.generateFromSeed(toHash.hashCode(), GameDifficulty.Challenge); + } else { + result = controller.generateFromSeed(toHash.hashCode(), GameDifficulty.Unspecified); + } + return result; } public int[] loadLevel(GameType type, GameDifficulty diff) { diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java index 2f44929..eb78863 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java @@ -53,13 +53,22 @@ public class QQWingController { return generated; } - public int[] generateFromSeed(int seed) { + public int[] generateFromSeed(int seed, GameDifficulty difficulty) { generated.clear(); - QQWing generator = new QQWing(GameType.Default_9x9, GameDifficulty.Unspecified); - generator.setRandom(seed); - generator.setRecordHistory(true); - generator.generatePuzzle(); + boolean havePuzzle = false; + + while(!havePuzzle) { + seed++; + generator.setRandom(seed); + generator.setRecordHistory(true); + generator.generatePuzzle(); + + if (difficulty == GameDifficulty.Unspecified && generator.getDifficulty() != GameDifficulty.Challenge + || difficulty == generator.getDifficulty()) { + havePuzzle = true; + } + } generated.add(generator.getPuzzle()); opts.gameType = GameType.Default_9x9;