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 7aca04a..47ec3a8 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java @@ -40,7 +40,8 @@ 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; + private final double CHALLENGE_GENERATION_PROBABILITY = 0.25; + private final int CHALLENGE_ITERATIONS = 4; public static NewLevelManager getInstance(Context context, SharedPreferences settings) { @@ -84,16 +85,8 @@ public class NewLevelManager { public int[] loadDailySudoku() { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); String toHash = "Sudoku/.PrivacyFriendly/." + dateFormat.format(new Date()); - boolean generateChallenge = new Random(toHash.hashCode()).nextDouble() >= CHALLENGE_GENERATION_PROBABILITY; QQWingController controller = new QQWingController(); - int[] result; - - if (generateChallenge) { - result = controller.generateFromSeed(toHash.hashCode(), GameDifficulty.Challenge); - } else { - result = controller.generateFromSeed(toHash.hashCode(), GameDifficulty.Unspecified); - } - return result; + return controller.generateFromSeed(toHash.hashCode(), CHALLENGE_GENERATION_PROBABILITY, CHALLENGE_ITERATIONS); } 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 eb78863..87a81b1 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java @@ -4,6 +4,7 @@ import android.os.Parcelable; import android.util.Log; import java.util.LinkedList; +import java.util.Random; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.Date; @@ -53,20 +54,27 @@ public class QQWingController { return generated; } - public int[] generateFromSeed(int seed, GameDifficulty difficulty) { + public int[] generateFromSeed(int seed) { + return generateFromSeed(seed, 1, 1); + } + + public int[] generateFromSeed(int seed, double challengePermission, int challengeIterations) { generated.clear(); QQWing generator = new QQWing(GameType.Default_9x9, GameDifficulty.Unspecified); - boolean havePuzzle = false; + boolean continueSearch = true; + Random random = new Random(seed); + int seedFactor = 2; - while(!havePuzzle) { - seed++; + while(continueSearch && challengeIterations > 0) { + seed *= seedFactor; generator.setRandom(seed); generator.setRecordHistory(true); generator.generatePuzzle(); - if (difficulty == GameDifficulty.Unspecified && generator.getDifficulty() != GameDifficulty.Challenge - || difficulty == generator.getDifficulty()) { - havePuzzle = true; + if (generator.getDifficulty() != GameDifficulty.Challenge || random.nextDouble() < challengePermission) { + continueSearch = false; + } else { + challengeIterations--; } } @@ -223,6 +231,10 @@ public class QQWingController { } } + public boolean isImpossible() { + return solveImpossible; + } + private static class QQWingOptions { // defaults for options boolean needNow = false;