From a0e15e750859b9927242309c2fe4ffd33a921938 Mon Sep 17 00:00:00 2001 From: uykek Date: Sun, 9 Aug 2020 09:44:31 +0200 Subject: [PATCH] Add JavaDocs and a few comments to some of the changed controller classes --- .../controller/GameController.java | 11 +++++++++-- .../controller/NewLevelManager.java | 3 +++ .../controller/QQWingController.java | 13 +++++++++++++ .../controller/database/DatabaseHelper.java | 11 +++++++++++ .../controller/helper/GameInfoContainer.java | 1 + 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java index 88e2472..b99ff38 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/GameController.java @@ -119,8 +119,10 @@ public class GameController implements IModelChangedListener, Parcelable { public void loadNewDailySudokuLevel() { NewLevelManager newLevelManager = NewLevelManager.getInstance(context, settings); + // generate the daily sudoku int[] level = newLevelManager.loadDailySudoku(); + // calculate the difficulty of the daily sudoku QQWing difficultyCheck = new QQWing(GameType.Default_9x9, GameDifficulty.Unspecified); difficultyCheck.setRecordHistory(true); difficultyCheck.setPuzzle(level); @@ -129,8 +131,6 @@ public class GameController implements IModelChangedListener, Parcelable { loadLevel(new GameInfoContainer(DAILY_SUDOKU_ID, difficultyCheck.getDifficulty(), GameType.Default_9x9, level, null, null)); - newLevelManager.checkAndRestock(); - } public int getTime() { @@ -347,19 +347,26 @@ public class GameController implements IModelChangedListener, Parcelable { fm.saveGameState(this); } + /** + * Save progress on the current daily sudoku + * @param context the context in which this method is called + */ 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(); } } + // turn the current date into an id Calendar currentDate = Calendar.getInstance(); int id = currentDate.get(Calendar.DAY_OF_MONTH) * 1000000 + (currentDate.get(Calendar.MONTH) + 1) * 10000 + currentDate.get(Calendar.YEAR); + // save the sudoku to the database using the previously calculated id DatabaseHelper db = new DatabaseHelper(context); DailySudoku dailySudoku = new DailySudoku(id, difficulty, gameType, encodedBoard, usedHints, GameActivity.timeToString(time)); db.addDailySudoku(dailySudoku); 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 47ec3a8..cdd0b4a 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/NewLevelManager.java @@ -83,9 +83,12 @@ public class NewLevelManager { } public int[] loadDailySudoku() { + // create a seed from the current date DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); String toHash = "Sudoku/.PrivacyFriendly/." + dateFormat.format(new Date()); QQWingController controller = new QQWingController(); + + // generate new sudoku using the previously computed seed return controller.generateFromSeed(toHash.hashCode(), CHALLENGE_GENERATION_PROBABILITY, CHALLENGE_ITERATIONS); } 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 87a81b1..a865a37 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/QQWingController.java @@ -54,10 +54,23 @@ public class QQWingController { return generated; } + /** + * Generate a new sudoku based on a given seed regardless of outcome difficulty + * @param seed the seed based on which the sudoku should be calculated + * @return the generated sudoku + */ public int[] generateFromSeed(int seed) { return generateFromSeed(seed, 1, 1); } + /** + * Generate a new sudoku based on a given seed, but only accept challenge sudokus with a certain probability + * @param seed the seed based on which the sudoku should be calculated + * @param challengePermission the probability with which a challenge sudoku is accepted upon calculation + * @param challengeIterations the amount of times a challenge sudoku can be rejected in a row before being + * accepted with a probability of 100% + * @return the generated sudoku + */ public int[] generateFromSeed(int seed, double challengePermission, int challengeIterations) { generated.clear(); QQWing generator = new QQWing(GameType.Default_9x9, GameDifficulty.Unspecified); diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/database/DatabaseHelper.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/database/DatabaseHelper.java index 55ac340..054b502 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/database/DatabaseHelper.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/database/DatabaseHelper.java @@ -84,9 +84,15 @@ public class DatabaseHelper extends SQLiteOpenHelper { return levelList.get(0); } + /** + * Returns a list of all the daily sudokus that have been solved and thus saved to the database + * @return a list of all the daily sudokus that have been solved so far + */ public synchronized List getDailySudokus() { List dailySudokuList = new LinkedList<>(); SQLiteDatabase database = getWritableDatabase(); + + // order results from most to least recent String order = DailySudokuColumns._ID + " DESC"; // How you want the results sorted in the resulting Cursor @@ -125,6 +131,11 @@ public class DatabaseHelper extends SQLiteOpenHelper { return database.insert(LevelColumns.TABLE_NAME, null, LevelColumns.getValues(level)); } + /** + * Adds a new daily sudoku to the database + * @param ds the daily sudoku which is to be added to the database + * @return the row id of the newly inserted sudoku (or -1 if an error occurred) + */ public synchronized long addDailySudoku(DailySudoku ds) { SQLiteDatabase database = getWritableDatabase(); return database.insert(DailySudokuColumns.TABLE_NAME, null, DailySudokuColumns.getValues(ds)); diff --git a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/helper/GameInfoContainer.java b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/helper/GameInfoContainer.java index 79cc687..ab1b936 100644 --- a/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/helper/GameInfoContainer.java +++ b/app/src/main/java/org/secuso/privacyfriendlysudoku/controller/helper/GameInfoContainer.java @@ -203,6 +203,7 @@ public class GameInfoContainer { sb.append("/"); sb.append(controller.getUsedHints()); + // add additional information to custom sudokus to ensure they can be distinguished from 'regular' sudokus if (custom) { sb.append("/"); sb.append(custom);