ogarcia / opensudoku

Open Source Sudoku game for Android.
GNU General Public License v3.0
328 stars 144 forks source link

Add ability to generate games #94

Closed chrisawad closed 3 years ago

chrisawad commented 4 years ago

I've added some code to SudokuGame, and CellCollection in order to generate new games. Basically all it does is use SudokuSolver to solve an empty board then randomly loops and unset x number of cells in that board. Very simple, but I am open to suggestions on better ways to generate new games.

I found that using 55 empty cells is pretty good for a very hard game. 60 empty cells became too difficult.

In order to facilitate the ability to generate and download multiple games at once, I've added 2 new activities (SudokuGenerateActivity and SudokuDownloadActivity) and a new task (GenerateImportTask) and integrated them into the existing import mechanisms.

https://opensudoku.moire.org/ doesn't expose the download link for generated games, so i cant really test SodokuDownloadActivity, but maybe someone has suggestions?

Let me know what you think!

chrisawad commented 4 years ago

After playing a few games I'm finding out that just looping through and unsetting x cells probably isn't the best way to generate quality games.

I think the problem is that some of the games have more than 1 path to solve the board. For example, sometimes I end up with 4 cells that could be the same 4 values and any random solution works.

Does anyone know who developed the SodokuSolver#solve() method? I'm wondering if the recursive algo can be modified to indicate if there are multiple ways to solve a board. If that's the case, I can dump it and generate a new one (which is what I suspect magazines and official games do).

Thoughts?

EDIT: I noticed the generated games on https://opensudoku.moire.org/ do not have my problem. Is the generator also open source?

ogarcia commented 4 years ago

I noticed the generated games on https://opensudoku.moire.org/ do not have my problem. Is the generator also open source?

Yes, here is

About this PR, it have good ideas, but I prefer a dedicated code to generate sudokus instead of sudoku solver. But if you want to help you can try to fix the import intent, I explain:

If you download an .opensudoku file or a .sdm file and try to open it you don't have Open Sudoku as option. I think that this is caused by a recent change in new versions of Android. I think that is better fix this issue first that is more easy that add an internal generator :wink:

chrisawad commented 3 years ago

Just so you know, I'm still working on this. I have something that works pretty well and have been optimizing it in my free time, but I've played 100s of quickly generated, very hard, unique games, without having to leave the app.

Only thing I'm missing is an algorithm that detects how hard the generated game is outside of counting empty cells. Some of the games with over 55 empty cells are extreamly hard and some are pretty simple (for me at least - I generally solve very hard hands in 5-11 minutes).

ogarcia commented 3 years ago

Ok. You can open a new PR when you have the functionality complete :wink: