Modify NewLevelManager's loadDailySudoku() method so that the probability of the daily sudoku's difficulty being 'challenge' is only 10%

This commit is contained in:
uykek 2020-06-17 18:33:59 +02:00
parent 87a80de4b5
commit f2aa3460ff
2 changed files with 26 additions and 9 deletions

View file

@ -40,6 +40,9 @@ public class NewLevelManager {
public static int PRE_SAVES_MIN = 3; public static int PRE_SAVES_MIN = 3;
public static int PRE_SAVES_MAX = 10; public static int PRE_SAVES_MAX = 10;
private final double CHALLENGE_GENERATION_PROBABILITY = 0.9;
public static NewLevelManager getInstance(Context context, SharedPreferences settings) { public static NewLevelManager getInstance(Context context, SharedPreferences settings) {
if(instance == null) { if(instance == null) {
instance = new NewLevelManager(context, settings); instance = new NewLevelManager(context, settings);
@ -80,11 +83,17 @@ public class NewLevelManager {
public int[] loadDailySudoku() { public int[] loadDailySudoku() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date(); String toHash = "Sudoku/.PrivacyFriendly/." + dateFormat.format(new Date());
String toHash = "Sudoku/.PrivacyFriendly/." + dateFormat.format(date); boolean generateChallenge = new Random(toHash.hashCode()).nextDouble() >= CHALLENGE_GENERATION_PROBABILITY;
QQWingController controller = new QQWingController(); 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) { public int[] loadLevel(GameType type, GameDifficulty diff) {

View file

@ -53,13 +53,21 @@ public class QQWingController {
return generated; return generated;
} }
public int[] generateFromSeed(int seed) { public int[] generateFromSeed(int seed, GameDifficulty difficulty) {
generated.clear(); generated.clear();
QQWing generator = new QQWing(GameType.Default_9x9, GameDifficulty.Unspecified); QQWing generator = new QQWing(GameType.Default_9x9, GameDifficulty.Unspecified);
generator.setRandom(seed); boolean havePuzzle = false;
generator.setRecordHistory(true);
generator.generatePuzzle(); while(!havePuzzle) {
seed++;
generator.setRandom(seed);
generator.setRecordHistory(true);
generator.generatePuzzle();
if (difficulty == GameDifficulty.Unspecified || difficulty == generator.getDifficulty()) {
havePuzzle = true;
}
}
generated.add(generator.getPuzzle()); generated.add(generator.getPuzzle());
opts.gameType = GameType.Default_9x9; opts.gameType = GameType.Default_9x9;