Add JavaDocs and a few comments to some of the changed controller classes

This commit is contained in:
uykek 2020-08-09 09:44:31 +02:00
parent 82297a8367
commit a0e15e7508
5 changed files with 37 additions and 2 deletions

View file

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

View file

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

View file

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

View file

@ -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<DailySudoku> getDailySudokus() {
List<DailySudoku> 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));

View file

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