Adjust generation of the daily sudoku so that challenges have a lesser probability of being generated than other sudokus, but generating them on the days they do appear won't take as long as before

This commit is contained in:
uykek 2020-07-21 10:43:54 +02:00
parent a4b80b9ecc
commit 3db3110618
2 changed files with 22 additions and 17 deletions

View file

@ -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) {

View file

@ -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;