|
32 | 32 | import sudoku.Sudoku2; |
33 | 33 |
|
34 | 34 | /** |
35 | | - * A BackgroundGenerator generates sudokus with a given {@link DifficultyLevel} |
36 | | - * and for a given {@link GameMode}. An instance of this class can be contained |
37 | | - * within a {@link BackgroundGeneratorThread} or within a |
38 | | - * {@link GenerateSudokuProgressDialog}.<br> |
| 35 | + * A BackgroundGenerator generates sudokus with a given {@link DifficultyLevel} |
| 36 | + * and for a given {@link GameMode}. An instance of this class can be contained |
| 37 | + * within a {@link BackgroundGeneratorThread} or within a {@link GenerateSudokuProgressDialog}.<br> |
39 | 38 | * If it is called from a {@link GenerateSudokuProgressDialog}, it uses the |
40 | 39 | * default solver and reports the progress to the dialog. If a puzzle has been |
41 | | - * found, the dialog is closed. The creation process can be aborted at any |
42 | | - * time.<br> |
| 40 | + * found, the dialog is closed. The creation process can be aborted at any time.<br> |
43 | 41 | * If it is called from a {@link BackgroundGeneratorThread}, it simply delivers |
44 | 42 | * the generated puzzle or <code>null</code>, if no puzzle could be found. |
45 | 43 | * |
46 | 44 | * @author hobiwan |
47 | 45 | */ |
48 | 46 | public class BackgroundGenerator { |
49 | | - /** |
50 | | - * Maximal number of tries, when called from a |
51 | | - * {@link BackgroundGeneratorThread}. |
52 | | - */ |
53 | | - private static final int MAX_TRIES = 20000; |
54 | | - /** |
55 | | - * Current number of tries when called from |
56 | | - * {@link GenerateSudokuProgressDialog}. |
57 | | - */ |
58 | | - private int anz = 0; |
59 | | - /** Progress dialog when called from GUI. */ |
60 | | - private GenerateSudokuProgressDialog progressDialog = null; |
| 47 | + /** Maximal number of tries, when called from a {@link BackgroundGeneratorThread}. */ |
| 48 | + private static final int MAX_TRIES = 20000; |
| 49 | + /** Current number of tries when called from {@link GenerateSudokuProgressDialog}. */ |
| 50 | + private int anz = 0; |
| 51 | + /** Progress dialog when called from GUI. */ |
| 52 | + private GenerateSudokuProgressDialog progressDialog = null; |
61 | 53 |
|
62 | | - /** |
63 | | - * Generates a new instance. |
64 | | - */ |
65 | | - public BackgroundGenerator() { |
66 | | - // nothing to do! |
67 | | - } |
| 54 | + /** |
| 55 | + * Generates a new instance. |
| 56 | + */ |
| 57 | + public BackgroundGenerator() { |
| 58 | + // nothing to do! |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a sudoku without responses to the GUI. Delegates to |
| 63 | + * {@link #generate(sudoku.DifficultyLevel, sudoku.GameMode, sudoku.GenerateSudokuProgressDialog) }. |
| 64 | + * |
| 65 | + * @param level |
| 66 | + * @param mode |
| 67 | + * @return |
| 68 | + */ |
| 69 | + public String generate(DifficultyLevel level, GameMode mode) { |
| 70 | + Sudoku2 sudoku = generate(level, mode, null); |
| 71 | + if (sudoku != null) { |
| 72 | + return sudoku.getSudoku(ClipboardMode.CLUES_ONLY); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Generates a new sudoku: If <code>dlg</code> is <code>null</code>, the progress is |
| 79 | + * reported and the dialog is closed, when puzzle has been found. The |
| 80 | + * current thread is checked for interruptions.<br> |
| 81 | + * If <code>dlg</code> is not <code>null</code>, the creation process goes on |
| 82 | + * until a puzzle has been found or until {@link #MAX_TRIES} tries have been |
| 83 | + * run. |
| 84 | + * |
| 85 | + * @param level |
| 86 | + * @param mode |
| 87 | + * @param dlg |
| 88 | + * @return |
| 89 | + */ |
| 90 | + public Sudoku2 generate(DifficultyLevel level, GameMode mode, GenerateSudokuProgressDialog dlg) { |
| 91 | + long actMillis = System.currentTimeMillis(); |
| 92 | + progressDialog = dlg; |
| 93 | + Sudoku2 sudoku = null; |
| 94 | + SudokuGenerator creator = null; |
| 95 | + SudokuSolver solver = null; |
| 96 | + setAnz(0); |
| 97 | + if (dlg == null) { |
| 98 | + // get any instance |
| 99 | + solver = SudokuSolverFactory.getInstance(); |
| 100 | + creator = SudokuGeneratorFactory.getInstance(); |
| 101 | + } else { |
| 102 | + // use the default solver |
| 103 | + solver = SudokuSolverFactory.getDefaultSolverInstance(); |
| 104 | + creator = SudokuGeneratorFactory.getDefaultGeneratorInstance(); |
| 105 | + } |
| 106 | + while (dlg == null || ! Thread.currentThread().isInterrupted()) { |
| 107 | + sudoku = creator.generateSudoku(true); |
| 108 | + if (sudoku == null) { |
| 109 | + // impossible to create sudoku due to an invalid pattern |
| 110 | + return null; |
| 111 | + } |
| 112 | + Sudoku2 solvedSudoku = sudoku.clone(); |
| 113 | + boolean ok = solver.solve(level, solvedSudoku, true, null, false, |
| 114 | + Options.getInstance().solverSteps, mode); |
| 115 | + boolean containsTrainingStep = true; |
| 116 | + if (mode != GameMode.PLAYING) { |
| 117 | + containsTrainingStep = false; |
| 118 | + List<SolutionStep> steps = solver.getSteps(); |
| 119 | + for (SolutionStep step : steps) { |
| 120 | + if (step.getType().getStepConfig().isEnabledTraining()) { |
| 121 | + containsTrainingStep = true; |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + if (ok && containsTrainingStep && |
| 127 | + (solvedSudoku.getLevel().getOrdinal() == level.getOrdinal() |
| 128 | + || mode == GameMode.LEARNING)) { |
| 129 | + sudoku.setLevel(solvedSudoku.getLevel()); |
| 130 | + sudoku.setScore(solvedSudoku.getScore()); |
| 131 | + break; |
| 132 | + } |
| 133 | + setAnz(getAnz() + 1); |
| 134 | + if (dlg != null) { |
| 135 | + if ((System.currentTimeMillis() - actMillis) > 500) { |
| 136 | + actMillis = System.currentTimeMillis(); |
| 137 | + EventQueue.invokeLater(new Runnable() { |
68 | 138 |
|
69 | | - /** |
70 | | - * Creates a sudoku without responses to the GUI. Delegates to |
71 | | - * {@link #generate(sudoku.DifficultyLevel, sudoku.GameMode, sudoku.GenerateSudokuProgressDialog) }. |
72 | | - * |
73 | | - * @param level |
74 | | - * @param mode |
75 | | - * @return |
76 | | - */ |
77 | | - public String generate(DifficultyLevel level, GameMode mode) { |
78 | | - Sudoku2 sudoku = generate(level, mode, null); |
79 | | - if (sudoku != null) { |
80 | | - return sudoku.getSudoku(ClipboardMode.CLUES_ONLY); |
81 | | - } |
82 | | - return null; |
83 | | - } |
| 139 | + @Override |
| 140 | + public void run() { |
| 141 | + progressDialog.updateProgressLabel(); |
| 142 | + //progressLabel.setText(Integer.toString(getAnz())); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + } else { |
| 147 | + if (getAnz() > MAX_TRIES) { |
| 148 | + // give up... |
| 149 | + sudoku = null; |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + if (dlg == null) { |
| 155 | + // give everything back |
| 156 | + SudokuGeneratorFactory.giveBack(creator); |
| 157 | + SudokuSolverFactory.giveBack(solver); |
| 158 | + } |
| 159 | + return sudoku; |
| 160 | + } |
84 | 161 |
|
85 | | - /** |
86 | | - * Generates a new sudoku: If <code>dlg</code> is <code>null</code>, the |
87 | | - * progress is reported and the dialog is closed, when puzzle has been found. |
88 | | - * The current thread is checked for interruptions.<br> |
89 | | - * If <code>dlg</code> is not <code>null</code>, the creation process goes on |
90 | | - * until a puzzle has been found or until {@link #MAX_TRIES} tries have been |
91 | | - * run. |
92 | | - * |
93 | | - * @param level |
94 | | - * @param mode |
95 | | - * @param dlg |
96 | | - * @return |
97 | | - */ |
98 | | - public Sudoku2 generate(DifficultyLevel level, GameMode mode, GenerateSudokuProgressDialog dlg) { |
99 | | - long actMillis = System.currentTimeMillis(); |
100 | | - progressDialog = dlg; |
101 | | - Sudoku2 sudoku = null; |
102 | | - SudokuGenerator creator = null; |
103 | | - SudokuSolver solver = null; |
104 | | - setAnz(0); |
105 | | - if (dlg == null) { |
106 | | - // get any instance |
107 | | - solver = SudokuSolverFactory.getInstance(); |
108 | | - creator = SudokuGeneratorFactory.getInstance(); |
109 | | - } else { |
110 | | - // use the default solver |
111 | | - solver = SudokuSolverFactory.getDefaultSolverInstance(); |
112 | | - creator = SudokuGeneratorFactory.getDefaultGeneratorInstance(); |
113 | | - } |
114 | | - while (dlg == null || !Thread.currentThread().isInterrupted()) { |
115 | | - sudoku = creator.generateSudoku(true); |
116 | | - if (sudoku == null) { |
117 | | - // impossible to create sudoku due to an invalid pattern |
118 | | - return null; |
119 | | - } |
120 | | - Sudoku2 solvedSudoku = sudoku.clone(); |
121 | | - boolean ok = solver.solve(level, solvedSudoku, true, null, false, Options.getInstance().solverSteps, mode); |
122 | | - boolean containsTrainingStep = true; |
123 | | - if (mode != GameMode.PLAYING) { |
124 | | - containsTrainingStep = false; |
125 | | - List<SolutionStep> steps = solver.getSteps(); |
126 | | - for (SolutionStep step : steps) { |
127 | | - if (step.getType().getStepConfig().isEnabledTraining()) { |
128 | | - containsTrainingStep = true; |
129 | | - break; |
130 | | - } |
131 | | - } |
132 | | - } |
133 | | - if (ok && containsTrainingStep |
134 | | - && (solvedSudoku.getLevel().getOrdinal() == level.getOrdinal() || mode == GameMode.LEARNING)) { |
135 | | - sudoku.setLevel(solvedSudoku.getLevel()); |
136 | | - sudoku.setScore(solvedSudoku.getScore()); |
137 | | - break; |
138 | | - } |
139 | | - setAnz(getAnz() + 1); |
140 | | - if (dlg != null) { |
141 | | - if ((System.currentTimeMillis() - actMillis) > 500) { |
142 | | - actMillis = System.currentTimeMillis(); |
143 | | - EventQueue.invokeLater(new Runnable() { |
| 162 | + /** |
| 163 | + * @return the anz |
| 164 | + */ |
| 165 | + public synchronized int getAnz() { |
| 166 | + return anz; |
| 167 | + } |
144 | 168 |
|
145 | | - @Override |
146 | | - public void run() { |
147 | | - progressDialog.updateProgressLabel(); |
148 | | - // progressLabel.setText(Integer.toString(getAnz())); |
149 | | - } |
150 | | - }); |
151 | | - } |
152 | | - } else { |
153 | | - if (getAnz() > MAX_TRIES) { |
154 | | - // give up... |
155 | | - sudoku = null; |
156 | | - break; |
157 | | - } |
158 | | - } |
159 | | - } |
160 | | - if (dlg == null) { |
161 | | - // give everything back |
162 | | - SudokuGeneratorFactory.giveBack(creator); |
163 | | - SudokuSolverFactory.giveBack(solver); |
164 | | - } |
165 | | - return sudoku; |
166 | | - } |
167 | | - |
168 | | - /** |
169 | | - * @return the anz |
170 | | - */ |
171 | | - public synchronized int getAnz() { |
172 | | - return anz; |
173 | | - } |
174 | | - |
175 | | - /** |
176 | | - * @param anz the anz to set |
177 | | - */ |
178 | | - public synchronized void setAnz(int anz) { |
179 | | - this.anz = anz; |
180 | | - } |
| 169 | + /** |
| 170 | + * @param anz the anz to set |
| 171 | + */ |
| 172 | + public synchronized void setAnz(int anz) { |
| 173 | + this.anz = anz; |
| 174 | + } |
181 | 175 | } |
0 commit comments