Skip to content

Commit 4095925

Browse files
committed
添加数独游戏。
1 parent ccee899 commit 4095925

6 files changed

Lines changed: 41 additions & 153 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ xJavaFxTool是使用javaFx开发的实用小工具集,利用业余时间把工
107107
54. FileUnicodeTransformationTool:文件编码转换工具
108108
55. FileCompressTool:文件解压缩工具(目前支持ar、zip、tar、jar、cpio、7z、gz、bzip2、xz、lzma、pack200、deflate、snappy-framed、lz4-block、lz4-framed、zstd等格式解压缩)
109109
56. IdiomDataTool:成语字典工具(使用[h2](http://www.h2database.com)数据库存储数据字典)
110+
57. Sudoku:数独游戏
110111

111112
传输工具目前支持功能如下:
112113

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Support plug-in development, the plug-in jar package can be automatically loaded
107107
54. FileUnicodeTransformationTool: File encoding conversion tool
108108
55. FileCompressTool: File decompression tool (currently supports ar, zip, tar, jar, cpio, 7z, gz, bzip2, xz, lzma, pack200, deflate, snappy-framed, lz4-block, lz4-framed, ZSTD, etc.)
109109
56. IdiomDataTool:IdiomDataTool(Use the [h2](http://www.h2database.com) database to store the data dictionary)
110+
57. Sudoku:Sudoku Game
110111

111112
##### The transfer tools currently support the following features:
112113
###### Receiver:

src/main/java/com/xwintop/xJavaFxTool/controller/games/SudokuController.java

Lines changed: 16 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333

3434
public class SudokuController extends SudokuView {
35-
// private SudokuService sudokuService = new SudokuService(this);
35+
private SudokuService sudokuService = new SudokuService(this);
3636
Sudoku sudoku = new Sudoku();
3737

3838
private Matrix puzzle;
@@ -62,11 +62,11 @@ private void initEvent() {
6262
}
6363

6464
private void initService() {
65-
initTimer();
65+
sudokuService.initTimer();
6666
puzzle = sudoku.init(levelChoiceBox.getSelectionModel().getSelectedIndex() + 1).get(0);
6767
ans = sudoku.getAns();
68-
ansMap = ansParse(this.ans);
69-
setCellNums(puzzle);
68+
ansMap = sudokuService.ansParse(this.ans);
69+
sudokuService.setCellNums(puzzle);
7070
}
7171

7272
private void addCellFocusedProperty() {
@@ -76,80 +76,20 @@ private void addCellFocusedProperty() {
7676
@Override
7777
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
7878
if (newValue.booleanValue()) {
79-
showSelectedCells(cell);
79+
sudokuService.showSelectedCells(cell);
8080
}
8181
}
8282
});
8383
}
8484
}
8585

86-
private void setCellNums(Matrix puzzle) {
87-
for (int i = 0; i < 9; i++) {
88-
for (int j = 0; j < 9; j++) {
89-
cells.get(i * 9 + j).setInitStatus();
90-
if (puzzle.get(i, j) != 0) {
91-
cells.get(i * 9 + j).setText(Integer.toString(puzzle.get(i, j)));
92-
} else {
93-
cells.get(i * 9 + j).setText("");
94-
}
95-
}
96-
}
97-
}
98-
99-
private void initTimer() {
100-
stop = false;
101-
startTime = System.currentTimeMillis();
102-
thread = new Thread() {
103-
public void run() {
104-
while (!stop) {
105-
curTime = System.currentTimeMillis();
106-
int interval = (int) ((curTime - startTime) / 1000);
107-
int hour = interval / 3600;
108-
int minute = (interval / 60) % 60;
109-
int second = interval % 60;
110-
timer.setText(String.format("%02d:%02d:%02d", hour, minute, second));
111-
try {
112-
Thread.sleep(1000);
113-
} catch (InterruptedException e) {
114-
e.printStackTrace();
115-
}
116-
}
117-
118-
}
119-
};
120-
thread.setDaemon(true);
121-
thread.start();
122-
}
123-
124-
/**
125-
* 将二维数组类型的数独ANS转为Map类型。
126-
*/
127-
private Map<String, String> ansParse(Matrix ans) {
128-
Map<String, String> ansMap = new HashMap<String, String>();
129-
for (int i = 0; i < 9; i++) {
130-
for (int j = 0; j < 9; j++) {
131-
ansMap.put("cell" + i + j, String.valueOf(ans.get(i, j)));
132-
}
133-
}
134-
return ansMap;
135-
}
136-
137-
private boolean checkSucess() {
138-
for (SCell cell : cells) {
139-
if (cell.getText().isEmpty() || cell.status.equals(CStatus.NOTE)) {
140-
return false;
141-
}
142-
}
143-
return true;
144-
}
145-
14686
@FXML
14787
private void keyListener(KeyEvent e) {
14888
String input = e.getText();
14989
SCell cell = ((SCell) e.getSource());
15090
if (e.getCode() == KeyCode.ENTER || e.getCode() == KeyCode.SPACE) {
15191
if (cell.status.equals(CStatus.INIT) && !cell.getText().isEmpty()) {
152-
highLight(cell);
92+
sudokuService.highLight(cell);
15393
}
15494
} else if (!noteItem.isSelected()) {
15595
setInputText(cell, input);
@@ -162,7 +102,7 @@ private void keyListener(KeyEvent e) {
162102
private void mouseListener(MouseEvent e) {
163103
SCell cell = (SCell) e.getSource();
164104
if (cell.status.equals(CStatus.NOTE) || showSelectedRC.isSelected() || showSelectedBlock.isSelected()) return;
165-
highLight(cell);
105+
sudokuService.highLight(cell);
166106
}
167107

168108
private void setInputText(SCell cell, String input) {
@@ -173,7 +113,7 @@ private void setInputText(SCell cell, String input) {
173113
cell.setInitStatus();
174114
;
175115
cell.setText(input);
176-
if (checkSucess()) {
116+
if (sudokuService.checkSucess()) {
177117
stop = true;
178118
AlertUtil.showInfoAlert("Sucessful", "Sucessful!\nTime: " + timer.getText());
179119
}
@@ -191,34 +131,6 @@ private void setInputText(SCell cell, String input) {
191131
}
192132
}
193133

194-
private void highLight(SCell cell) {
195-
// cell空或ERROR或已经高亮,取消所有高亮
196-
if (cell.getText().isEmpty() || cell.status.equals(CStatus.ERROR) || cell.status.equals(CStatus.NOTE) || cell.isHighLight) {
197-
for (SCell cell2 : cells) {
198-
if (cell2.status.equals(CStatus.INIT)) {
199-
cell2.setInitStatus();
200-
}
201-
}
202-
// cell未高亮,则执行高亮
203-
} else {
204-
for (SCell cell2 : cells) {
205-
if (cell2.getText().equals(cell.getText()) && !cell2.status.equals(CStatus.NOTE)) {
206-
cell2.setHighLightStatus();
207-
} else if (cell2.status.equals(CStatus.INIT)) {
208-
cell2.setInitStatus();
209-
} else if (cell2.status.equals(CStatus.ERROR)) {
210-
cell2.setEffect(null);
211-
cell2.isHighLight = false;
212-
cell2.setErrorStatus();
213-
} else if (cell2.status.equals(CStatus.NOTE)) {
214-
cell2.setEffect(null);
215-
cell2.isHighLight = false;
216-
cell2.setNoteStatus(getFontSize(cell2.getText()));
217-
}
218-
}
219-
}
220-
}
221-
222134
@FXML
223135
private void newGame() {
224136
noteItem.setSelected(false);
@@ -228,9 +140,9 @@ private void newGame() {
228140
times.setFill(Color.BLACK);
229141
puzzle = sudoku.init(levelChoiceBox.getSelectionModel().getSelectedIndex() + 1).get(0);
230142
ans = sudoku.getAns();
231-
ansMap = ansParse(this.ans);
232-
setCellNums(puzzle);
233-
initTimer();
143+
ansMap = sudokuService.ansParse(this.ans);
144+
sudokuService.setCellNums(puzzle);
145+
sudokuService.initTimer();
234146
}
235147

236148
@FXML
@@ -241,7 +153,7 @@ private void solve() {
241153
else num = Integer.parseInt(cell.getText());
242154
puzzle.set(Character.getNumericValue(cell.getId().charAt(4)), Character.getNumericValue(cell.getId().charAt(5)), num);
243155
ans = sudoku.solve(puzzle).get(0);
244-
setCellNums(ans);
156+
sudokuService.setCellNums(ans);
245157
stop = true;
246158
}
247159
}
@@ -271,8 +183,8 @@ private void openFile() {
271183
return;
272184
}
273185
ans = sudoku.solve(puzzle).get(0);
274-
setCellNums(puzzle);
275-
initTimer();
186+
sudokuService.setCellNums(puzzle);
187+
sudokuService.initTimer();
276188
}
277189
}
278190

@@ -327,7 +239,7 @@ private void showCandidates() {
327239
}
328240
Collections.sort(candidates);
329241
String cellText = String.join("", candidates);
330-
cell.setNoteStatus(getFontSize(cellText));
242+
cell.setNoteStatus(sudokuService.getFontSize(cellText));
331243
cell.wrapTextProperty().setValue(true);
332244
cell.setText(cellText);
333245
}
@@ -341,53 +253,14 @@ private void showCandidates() {
341253
}
342254
}
343255

344-
private void showSelectedCells(SCell focusCells) {
345-
if (!showSelectedRC.isSelected() && !showSelectedBlock.isSelected()) return;
346-
List<SCell> showCells = new ArrayList<SCell>();
347-
for (SCell cell : cells) {
348-
if (showSelectedRC.isSelected() && (cell.getId().charAt(4) == focusCells.getId().charAt(4) || cell.getId().charAt(5) == focusCells.getId().charAt(5))) {
349-
showCells.add(cell);
350-
}
351-
if (showSelectedBlock.isSelected() && sudoku.getBlock(Character.getNumericValue(cell.getId().charAt(4)), Character.getNumericValue(cell.getId().charAt(5))) == sudoku.getBlock(Character.getNumericValue(focusCells.getId().charAt(4)), Character.getNumericValue(focusCells.getId().charAt(5)))) {
352-
showCells.add(cell);
353-
}
354-
}
355-
if (showSelectedRC.isSelected() || showSelectedBlock.isSelected()) {
356-
for (SCell cell : cells) {
357-
if (cell.equals(focusCells)) {
358-
cell.setEffect(null);
359-
cell.isHighLight = false;
360-
} else if (showCells.contains(cell)) {
361-
cell.setHighLightStatus();
362-
} else {
363-
cell.isHighLight = false;
364-
if (cell.status.equals(CStatus.INIT) && !cell.isHighLight) {
365-
cell.setInitStatus();
366-
} else if (cell.status.equals(CStatus.NOTE)) {
367-
cell.setNoteStatus(getFontSize(cell.getText()));
368-
cell.setEffect(null);
369-
} else if (cell.status.equals(CStatus.ERROR)) {
370-
cell.setErrorStatus();
371-
cell.setEffect(null);
372-
}
373-
}
374-
}
375-
}
376-
}
377-
378-
private int getFontSize(String cellText) {
379-
if (cellText.length() <= 2) return 24 - cellText.length() * 4;
380-
else return 12;
381-
}
382-
383256
@FXML
384257
private void hideAllHighLightCells() {
385258
for (SCell cell : cells) {
386259
cell.isHighLight = false;
387260
if (cell.status.equals(CStatus.INIT) && !cell.isHighLight) {
388261
cell.setInitStatus();
389262
} else if (cell.status.equals(CStatus.NOTE)) {
390-
cell.setNoteStatus(getFontSize(cell.getText()));
263+
cell.setNoteStatus(sudokuService.getFontSize(cell.getText()));
391264
cell.setEffect(null);
392265
} else if (cell.status.equals(CStatus.ERROR)) {
393266
cell.setErrorStatus();

src/main/java/com/xwintop/xJavaFxTool/services/games/SudokuService.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
import java.util.List;
1717
import java.util.Map;
1818

19+
/**
20+
* @ClassName: SudokuService
21+
* @Description: 数独游戏
22+
* @author: xufeng
23+
* @date: 2020/1/6 16:08
24+
*/
25+
1926
@Getter
2027
@Setter
2128
@Slf4j
@@ -33,7 +40,7 @@ public class SudokuService {
3340
private long curTime;
3441
private Thread thread;
3542

36-
private void setCellNums(Matrix puzzle) {
43+
public void setCellNums(Matrix puzzle) {
3744
for (int i = 0; i < 9; i++) {
3845
for (int j = 0; j < 9; j++) {
3946
sudokuController.getCells().get(i * 9 + j).setInitStatus();
@@ -46,7 +53,7 @@ private void setCellNums(Matrix puzzle) {
4653
}
4754
}
4855

49-
private void initTimer() {
56+
public void initTimer() {
5057
stop = false;
5158
startTime = System.currentTimeMillis();
5259
thread = new Thread() {
@@ -74,7 +81,7 @@ public void run() {
7481
/**
7582
* 将二维数组类型的数独ANS转为Map类型。
7683
*/
77-
private Map<String, String> ansParse(Matrix ans) {
84+
public Map<String, String> ansParse(Matrix ans) {
7885
Map<String, String> ansMap = new HashMap<String, String>();
7986
for (int i = 0; i < 9; i++) {
8087
for (int j = 0; j < 9; j++) {
@@ -84,7 +91,7 @@ private Map<String, String> ansParse(Matrix ans) {
8491
return ansMap;
8592
}
8693

87-
private boolean checkSucess() {
94+
public boolean checkSucess() {
8895
for (SCell cell : sudokuController.getCells()) {
8996
if (cell.getText().isEmpty() || cell.status.equals(CStatus.NOTE)) {
9097
return false;
@@ -93,7 +100,7 @@ private boolean checkSucess() {
93100
return true;
94101
}
95102

96-
private void setInputText(SCell cell, String input) {
103+
public void setInputText(SCell cell, String input) {
97104
if (!(cell.getText().isEmpty() || cell.status.equals(CStatus.NOTE) || cell.status.equals(CStatus.ERROR)))
98105
return;
99106
if (!input.isEmpty() && "123456789".contains(input)) {
@@ -119,7 +126,7 @@ private void setInputText(SCell cell, String input) {
119126
}
120127
}
121128

122-
private void highLight(SCell cell) {
129+
public void highLight(SCell cell) {
123130
// cell空或ERROR或已经高亮,取消所有高亮
124131
if (cell.getText().isEmpty() || cell.status.equals(CStatus.ERROR) || cell.status.equals(CStatus.NOTE) || cell.isHighLight) {
125132
for (SCell cell2 : sudokuController.getCells()) {
@@ -147,7 +154,7 @@ private void highLight(SCell cell) {
147154
}
148155
}
149156

150-
private void showSelectedCells(SCell focusCells) {
157+
public void showSelectedCells(SCell focusCells) {
151158
if (!sudokuController.getShowSelectedRC().isSelected() && !sudokuController.getShowSelectedBlock().isSelected())
152159
return;
153160
List<SCell> showCells = new ArrayList<SCell>();
@@ -182,7 +189,7 @@ private void showSelectedCells(SCell focusCells) {
182189
}
183190
}
184191

185-
private int getFontSize(String cellText) {
192+
public int getFontSize(String cellText) {
186193
if (cellText.length() <= 2) return 24 - cellText.length() * 4;
187194
else return 12;
188195
}

src/main/java/com/xwintop/xJavaFxTool/view/games/SudokuView.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
import java.util.List;
1414

15+
/**
16+
* @ClassName: SudokuView
17+
* @Description: 数独游戏
18+
* @author: xufeng
19+
* @date: 2020/1/6 16:08
20+
*/
21+
1522
@Getter
1623
@Setter
1724
public abstract class SudokuView implements Initializable {
@@ -23,7 +30,6 @@ public abstract class SudokuView implements Initializable {
2330
protected CheckMenuItem showSelectedRC;
2431
@FXML
2532
protected CheckMenuItem showSelectedBlock;
26-
2733
@FXML
2834
protected ImageView noteIcon;
2935
@FXML

src/main/resources/config/toolFxmlLoaderConfiguration.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@
329329
<url>/com/xwintop/xJavaFxTool/fxmlView/games/Sudoku.fxml</url>
330330
<title>Sudoku</title>
331331
<menuParentId>games</menuParentId>
332-
<isDefaultShow>true</isDefaultShow>
332+
<isDefaultShow></isDefaultShow>
333333
</ToolFxmlLoaderConfiguration>
334334

335335
<ToolFxmlLoaderConfiguration url="/web/littleTools/cron/index.htm"

0 commit comments

Comments
 (0)