-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.java
1113 lines (1041 loc) · 44 KB
/
Project.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Project;
import javax.swing.*;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
new SudokuGrid();
}
}
class SudokuGrid extends JFrame {
JLabel jllNewGame;
JLabel jllHintNum;
JButton jbnEasy;
JButton jbnMid;
JButton jbnHard;
JButton jbnRepeal;
JButton jbnHint;
JButton jbnWipe;
JButton jbnNote;
JButton jbnArchive;
JButton jbnRead;
JButton jbnJudgement;
JButton jbnTime;
JPanel[] pnlGame;
JTextField[][][] txtGame;
UndoManager undoManager;
public static ArrayList<String> result = new ArrayList<>();
public SudokuGrid() {
pnlGame = new JPanel[9];
txtGame = new JTextField[9][3][3];
gridInit();
new Timer();
}
public void gridInit() {
Container container = this.getContentPane();
container.setLayout(null);
this.setSize(1100, 800);
this.setLocationRelativeTo(null);
this.setTitle("Sudoku Game");
undoManager = new UndoManager();
jllNewGame = new JLabel();
jllNewGame.setIcon(new ImageIcon("shudu.png"));
jllNewGame.setSize(375, 100);
jllNewGame.setLocation(700, 0);
container.add(jllNewGame);
jbnTime = new JButton("纪录");
jbnTime.setLocation(735, 650);
jbnTime.setSize(300, 75);
jbnTime.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnTime.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Record();
}
});
container.add(jbnTime);
jbnEasy = new JButton("简单");
jbnEasy.setBackground(Color.green);
jbnEasy.setSize(125, 100);
jbnEasy.setLocation(700, 100);
jbnEasy.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnEasy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Reset();
int[][] sudoku = EasyGame();
SetBoard(sudoku);
}
});
container.add(jbnEasy);
jbnMid = new JButton("中等");
jbnMid.setBackground(Color.yellow);
jbnMid.setSize(125, 100);
jbnMid.setLocation(825, 100);
jbnMid.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnMid.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Reset();
int[][] sudoku = MidGame();
SetBoard(sudoku);
}
});
container.add(jbnMid);
jbnHard = new JButton("困难");
jbnHard.setBackground(Color.red);
jbnHard.setSize(125, 100);
jbnHard.setLocation(950, 100);
jbnHard.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnHard.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Reset();
int[][] sudoku = HardGame();
SetBoard(sudoku);
}
});
container.add(jbnHard);
jbnJudgement = new JButton("结束");
jbnJudgement.setBackground(Color.pink);
jbnJudgement.setSize(375, 100);
jbnJudgement.setLocation(700, 200);
jbnJudgement.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnJudgement.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Judgement();
}
});
container.add(jbnJudgement);
jbnArchive = new JButton("存档");
jbnArchive.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnArchive.setBackground(Color.cyan);
jbnArchive.setSize(150, 100);
jbnArchive.setLocation(735, 350);
jbnArchive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Storage();
}
});
container.add(jbnArchive);
jbnRead = new JButton("读取");
jbnRead.setSize(150, 100);
jbnRead.setLocation(885, 350);
jbnRead.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnRead.setBackground(Color.cyan);
jbnRead.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Read();
}
});
container.add(jbnRead);
jbnRepeal = new JButton("撤销");
jbnRepeal.setBackground(Color.ORANGE);
jbnRepeal.setSize(150, 100);
jbnRepeal.setLocation(735, 450);
jbnRepeal.setFont(new Font("微软雅黑", Font.PLAIN, 40));
jbnRepeal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
});
container.add(jbnRepeal);
jbnHint = new JButton("提示");
jbnHint.setBackground(Color.ORANGE);
jbnHint.setSize(150, 100);
jbnHint.setLocation(735, 550);
jbnHint.setFont(new Font("微软雅黑", Font.PLAIN, 40));
container.add(jbnHint);
jbnWipe = new JButton("擦除");
jbnWipe.setBackground(Color.ORANGE);
jbnWipe.setSize(150, 100);
jbnWipe.setLocation(885, 450);
jbnWipe.setFont(new Font("微软雅黑", Font.PLAIN, 40));
container.add(jbnWipe);
jbnNote = new JButton("笔记");
jbnNote.setBackground(Color.ORANGE);
jbnNote.setSize(150, 100);
jbnNote.setLocation(885, 550);
jbnNote.setFont(new Font("微软雅黑", Font.PLAIN, 40));
container.add(jbnNote);
jllHintNum = new JLabel("3");//剩余提示次数
jllHintNum.setLocation(700, 580);
jllHintNum.setSize(30, 40);
jllHintNum.setFont(new Font("微软雅黑", Font.PLAIN, 30));
container.add(jllHintNum);
for (int i = 0; i < 9; i++) {
pnlGame[i] = new JPanel();
pnlGame[i].setBorder(BorderFactory.createLineBorder(Color.black, 2));
pnlGame[i].setLayout(new GridLayout(3, 3));
pnlGame[i].setSize(225, 225);
if (i == 0) {
pnlGame[i].setLocation(0, 0);
} else if (i == 1) {
pnlGame[i].setLocation(225, 0);
} else if (i == 2) {
pnlGame[i].setLocation(450, 0);
} else if (i == 3) {
pnlGame[i].setLocation(0, 225);
} else if (i == 4) {
pnlGame[i].setLocation(225, 225);
} else if (i == 5) {
pnlGame[i].setLocation(450, 225);
} else if (i == 6) {
pnlGame[i].setLocation(0, 450);
} else if (i == 7) {
pnlGame[i].setLocation(225, 450);
} else {
pnlGame[i].setLocation(450, 450);
}
container.add(pnlGame[i]);
}
for (int z = 0; z < 9; z++) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
txtGame[z][x][y] = new JTextField();
txtGame[z][x][y].setBorder(BorderFactory.createLineBorder(Color.black, 1));
txtGame[z][x][y].setFont(new Font("Dialog", Font.ITALIC, 40));
txtGame[z][x][y].setHorizontalAlignment(JTextField.CENTER);
pnlGame[z].add(txtGame[z][x][y]);
}
}
}
for (int z = 0; z < 9; z++) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
txtGame[z][x][y].getDocument().addUndoableEditListener(undoManager);
}
}
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void Judgement() {
JFrame frameJudge = new JFrame("Winning or Losing");
frameJudge.setSize(600, 100);
JPanel jpJudge = new JPanel();
JLabel labelWin = new JLabel();
JLabel labelLose = new JLabel();
newSudoku:
for (int i = 1; i <= 1; i++) {
int[][] sudoku = new int[9][9];
int z = 0;
for (int line = 0; line < 9; line += 3) {
for (int column = 0; column < 9; column += 3) {
for (int squareLine = line; squareLine < line + 3; squareLine++) {
for (int squareCol = column; squareCol < column + 3; squareCol++) {
String str = txtGame[z][squareLine % 3][squareCol % 3].getText();
if (str.equals("")) {
sudoku[squareLine][squareCol] = 0;
} else {
sudoku[squareLine][squareCol] = Integer.parseInt(str);
}
}
}
z = z + 1;
}
}
//判断数独中是否含0,含0代表没有填写数字
for (int line = 0; line < 9; line++) {
for (int column = 0; column < 9; column++) {
if (sudoku[line][column] == 0) {
result.add("Wrong");
continue newSudoku;
}
}
}
//判断每一行是否符合1到9不重复的标准
for (int line = 0; line < 9; line++) {
int[] arrLine = new int[9];
for (int column = 0; column < 9; column++) {
if (arrLine[sudoku[line][column] - 1] > 0) {
result.add("Wrong");
continue newSudoku;
} else {
arrLine[sudoku[line][column] - 1] = 1;
}
}
}
//判断每一列是否符合1到9不重复的标准
for (int column = 0; column < 9; column++) {
int[] arrCol = new int[9];
for (int line = 0; line < 9; line++) {
if (arrCol[sudoku[line][column] - 1] > 0) {
result.add("Wrong");
continue newSudoku;
} else {
arrCol[sudoku[line][column] - 1] = 1;
}
}
}
//判断每一个方块是否符合1到9不重复的标准
for (int line = 0; line < 9; line += 3) {
for (int column = 0; column < 9; column += 3) {
int[] arrSquare = new int[9];
for (int squareLine = line; squareLine < line + 3; squareLine++) {
for (int squareCol = column; squareCol < column + 3; squareCol++) {
if (arrSquare[sudoku[squareLine][squareCol] - 1] > 0) {
result.add("Wrong");
continue newSudoku;
} else {
arrSquare[sudoku[squareLine][squareCol] - 1] = 1;
}
}
}
}
}
result.add("Right");
}
for (String str : result) {
if (str.equals("Right")) {
jpJudge.add(labelWin);
labelWin.setText("Congratulation!You Win!");
labelWin.setFont(new Font("微软雅黑", Font.PLAIN, 40));
}
if (str.equals("Wrong")) {
jpJudge.add(labelLose);
labelLose.setText("Sorry~You Lose~");
labelLose.setFont(new Font("微软雅黑", Font.PLAIN, 40));
}
}
frameJudge.add(jpJudge);
frameJudge.setVisible(true);
frameJudge.setLocationRelativeTo(null);
}
public void Storage() {
File fileStorage = new File("Storage.txt");
File fileEditable = new File("Editable.txt");
//将数独txtGame[z][x][y]存入二维数组sudokuStore[][]
int[][] sudokuStore = new int[9][9];
int z = 0;
for (int line = 0; line < 9; line += 3) {
for (int column = 0; column < 9; column += 3) {
for (int squareLine = line; squareLine < line + 3; squareLine++) {
for (int squareCol = column; squareCol < column + 3; squareCol++) {
String str = txtGame[z][squareLine % 3][squareCol % 3].getText();
if (str.equals("")) {
sudokuStore[squareLine][squareCol] = 0;
} else {
sudokuStore[squareLine][squareCol] = Integer.parseInt(str);
}
}
}
z = z + 1;
}
}
//将二维数组sudokuStore[][]存入Storage.txt中保存
PrintWriter outputStorage = null;
try {
outputStorage = new PrintWriter(fileStorage);
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
outputStorage.print(sudokuStore[i][j] + " ");
}
outputStorage.println();
}
outputStorage.close();
//创建该数独中代表数字是否可编辑的数组sudokuEditable[][],1代表可编辑,0代表不可编辑
int[][] sudokuEditable = new int[9][9];
z = 0;
for (int line = 0; line < 9; line += 3) {
for (int column = 0; column < 9; column += 3) {
for (int squareLine = line; squareLine < line + 3; squareLine++) {
for (int squareCol = column; squareCol < column + 3; squareCol++) {
String str = txtGame[z][squareLine % 3][squareCol % 3].getText();
if (txtGame[z][squareLine % 3][squareCol % 3].isEditable()) {
sudokuEditable[squareLine][squareCol] = 1;
} else {
sudokuEditable[squareLine][squareCol] = 0;
}
}
}
z = z + 1;
}
}
//将二维数组sudokuEditable[][]存入Editable.txt中保存之前的数独
PrintWriter outputEditable = null;
try {
outputEditable = new PrintWriter(fileEditable);
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
outputEditable.print(sudokuEditable[i][j] + " ");
}
outputEditable.println();
}
outputEditable.close();
}
public void Read() {
File fileStorage = new File("Storage.txt");
File fileEditable = new File("Editable.txt");
InputStreamReader inputStreamStorage = null;
try {
inputStreamStorage = new InputStreamReader(new FileInputStream(fileStorage));
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
BufferedReader bufferStorage = new BufferedReader(inputStreamStorage);
InputStreamReader inputStreamEditable = null;
try {
inputStreamEditable = new InputStreamReader(new FileInputStream(fileEditable));
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
BufferedReader bufferEditable = new BufferedReader(inputStreamEditable);
int[][] sudokuLoad = new int[9][9];
int[][] sudokuEditable = new int[9][9];
//将Storage.txt当当中的二维数组读取到sudokuLoad[][]中
for (int line = 0; line < 9; line++) {
String[] intStr = new String[0];
try {
intStr = bufferStorage.readLine().split(" ");
} catch (IOException ioException) {
ioException.printStackTrace();
}
for (int column = 0; column < 9; column++) {
sudokuLoad[line][column] = Integer.parseInt(intStr[column]);
}
}
try {
inputStreamStorage.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
//将Editable.txt当当中的二维数组读取到sudokuEditable[][]中
for (int line = 0; line < 9; line++) {
String[] intStr = new String[0];
try {
intStr = bufferEditable.readLine().split(" ");
} catch (IOException ioException) {
ioException.printStackTrace();
}
for (int column = 0; column < 9; column++) {
sudokuEditable[line][column] = Integer.parseInt(intStr[column]);
}
}
try {
inputStreamEditable.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
Reset();
//将sudokuLoad[][]赋值给txtGame[z][x][y],并判断每个数字是否为可编辑的数字
int z = 0;
for (int line = 0; line < 9; line += 3) {
for (int column = 0; column < 9; column += 3) {
for (int squareLine = line; squareLine < line + 3; squareLine++) {
for (int squareCol = column; squareCol < column + 3; squareCol++) {
if (sudokuLoad[squareLine][squareCol] != 0) {
txtGame[z][squareLine % 3][squareCol % 3].setText(String.valueOf(sudokuLoad[squareLine][squareCol]));
}
if (sudokuEditable[squareLine][squareCol] == 0) {
txtGame[z][squareLine % 3][squareCol % 3].setEditable(false);
}
if (sudokuEditable[squareLine][squareCol] == 1) {
txtGame[z][squareLine % 3][squareCol % 3].setEditable(true);
}
}
}
z = z + 1;
}
}
}
public boolean isRecord = true;
public void Record() {
File fileTime = new File("TimeRecord.txt");
Scanner input = null;
try {
input = new Scanner(fileTime);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String List = input.nextLine();
String[] timeList = List.split(" ");
int[] time = new int[timeList.length];
for (int i = 0; i < timeList.length; i++) {
time[i] = Integer.parseInt(timeList[i]);
}
Arrays.sort(time);
int hour, minute, second, milli;
int elapsed = time[0];
milli = elapsed % 1000;
elapsed = elapsed / 1000;
second = elapsed % 60;
elapsed = elapsed / 60;
minute = elapsed % 60;
elapsed = elapsed / 60;
hour = elapsed % 60;
if (isRecord) {
jbnTime.setText(hour + ":" + minute + ":" + second + " " + milli);
isRecord = false;
} else{
jbnTime.setText("记录");
isRecord = true;
}
}
public void Reset() {
//刷新文本框
for (int z = 0; z < 9; z++) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
txtGame[z][x][y].setText("");
txtGame[z][x][y].setEditable(true);
}
}
}
}
public void SetBoard(int[][] sudoku) {
int z = 0;
for (int line = 0; line < 9; line += 3) {
for (int column = 0; column < 9; column += 3) {
for (int squareLine = line; squareLine < line + 3; squareLine++) {
for (int squareCol = column; squareCol < column + 3; squareCol++) {
if (sudoku[squareLine][squareCol] != 0) {
txtGame[z][squareLine % 3][squareCol % 3].setText(String.valueOf(sudoku[squareLine][squareCol]));
txtGame[z][squareLine % 3][squareCol % 3].setEditable(false);
}
}
}
z = z + 1;
}
}
}
public int[][] EasyGame() {
int a = (int) (Math.random() * 10);
switch (a) {
case 0:
return new int[][]{
{0, 6, 1, 0, 3, 0, 0, 2, 0},
{0, 5, 0, 0, 0, 8, 1, 0, 7},
{0, 0, 0, 0, 0, 7, 0, 3, 4},
{0, 0, 9, 0, 0, 6, 3, 7, 8},
{0, 0, 3, 2, 7, 9, 5, 0, 0},
{5, 7, 0, 3, 0, 0, 9, 0, 2},
{1, 9, 0, 7, 6, 0, 0, 0, 0},
{8, 0, 2, 4, 0, 0, 7, 6, 0},
{6, 4, 0, 0, 1, 0, 2, 5, 0},
};
case 1:
return new int[][]{
{1, 0, 0, 8, 3, 0, 0, 0, 2},
{5, 7, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 5, 0, 9, 0, 6, 4},
{7, 0, 4, 0, 0, 8, 5, 9, 0},
{0, 0, 3, 0, 1, 0, 4, 0, 0},
{0, 5, 1, 4, 0, 0, 3, 0, 6},
{3, 6, 0, 7, 0, 4, 0, 0, 0},
{0, 0, 0, 6, 0, 0, 0, 7, 9},
{8, 0, 0, 0, 5, 2, 0, 0, 3},
};
case 2:
return new int[][]{
{0, 3, 0, 0, 0, 7, 0, 0, 4},
{6, 0, 2, 0, 4, 1, 0, 0, 0},
{0, 5, 0, 0, 3, 0, 9, 6, 7},
{0, 4, 0, 0, 0, 3, 0, 0, 6},
{0, 8, 7, 0, 0, 0, 3, 5, 0},
{9, 0, 0, 7, 0, 0, 0, 2, 0},
{7, 1, 8, 0, 2, 0, 0, 4, 0},
{0, 0, 0, 1, 6, 0, 8, 0, 9},
{4, 0, 0, 5, 0, 0, 0, 3, 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 0, 0, 0, 3, 0, 0},
{2, 5, 0, 0, 0, 0, 0, 1, 9},
};
case 7:
return new int[][]{
{8, 0, 3, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 4, 0, 0},
{1, 0, 0, 0, 2, 8, 0, 0, 3},
{9, 0, 4, 0, 6, 0, 0, 1, 7},
{0, 0, 0, 4, 0, 5, 0, 0, 0},
{2, 1, 0, 0, 8, 0, 3, 0, 6},
{3, 0, 0, 2, 7, 0, 0, 0, 5},
{0, 0, 9, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 8, 0, 0, 6, 0, 2},
};
case 8:
return new int[][]{
{0, 0, 8, 0, 0, 0, 6, 0, 0},
{0, 5, 0, 0, 4, 0, 0, 8, 0},
{7, 9, 0, 6, 0, 8, 0, 4, 5},
{4, 0, 0, 0, 5, 0, 0, 0, 6},
{0, 0, 0, 2, 0, 1, 0, 0, 0},
{2, 0, 0, 0, 7, 0, 0, 0, 3},
{9, 1, 0, 5, 0, 7, 0, 3, 8},
{0, 3, 0, 0, 6, 0, 0, 2, 0},
{0, 0, 4, 0, 0, 0, 7, 0, 0},
};
case 9:
return new int[][]{
{0, 0, 0, 0, 0, 7, 0, 0, 4},
{8, 0, 0, 0, 0, 0, 0, 6, 0},
{5, 4, 0, 9, 2, 0, 0, 0, 1},
{4, 5, 9, 0, 1, 0, 0, 0, 3},
{2, 0, 0, 0, 0, 0, 0, 0, 9},
{1, 0, 0, 0, 3, 0, 4, 8, 2},
{7, 0, 0, 0, 5, 8, 0, 9, 6},
{0, 1, 0, 0, 0, 0, 0, 0, 5},
{6, 0, 0, 7, 0, 0, 0, 0, 0},
};
}
return null;
}
public int[][] MidGame() {
int a = (int) (Math.random() * 10);
switch (a) {
case 0:
return new int[][]{
{0, 0, 0, 1, 0, 0, 2, 6, 0},
{7, 0, 0, 0, 3, 0, 0, 0, 0},
{3, 0, 2, 0, 8, 0, 4, 0, 0},
{0, 0, 0, 4, 0, 8, 0, 0, 1},
{0, 3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0, 0, 8, 0, 0, 0, 0, 4, 5},
{0, 0, 4, 0, 0, 1, 3, 0, 0},
{0, 0, 0, 0, 5, 9, 0, 0, 0},
{0, 9, 0, 0, 3, 0, 0, 6, 0},
{0, 0, 0, 2, 6, 0, 0, 0, 0},
{0, 0, 1, 4, 0, 0, 2, 0, 0},
{2, 6, 0, 0, 0, 0, 1, 0, 0},
{4, 0, 0, 0, 0, 2, 5, 0, 3},
};
case 9:
return new int[][]{
{0, 9, 5, 0, 0, 8, 0, 0, 0},
{0, 0, 2, 0, 0, 6, 7, 0, 0},
{0, 4, 0, 0, 0, 0, 0, 0, 5},
{0, 5, 0, 0, 2, 0, 0, 0, 7},
{0, 6, 0, 0, 5, 0, 0, 2, 0},
{4, 0, 0, 0, 7, 0, 0, 8, 0},
{2, 0, 0, 0, 0, 0, 0, 4, 0},
{0, 0, 6, 1, 0, 0, 3, 0, 0},
{0, 0, 0, 3, 0, 0, 2, 5, 0},
};
}
return null;
}
public int[][] HardGame() {
int a = (int) (Math.random() * 10);
switch (a) {
case 0:
return new int[][]{
{0, 1, 0, 0, 0, 8, 4, 0, 7},
{9, 5, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 8, 0, 1, 0, 0, 0, 0},
{0, 8, 2, 0, 0, 0, 0, 0, 0},
{7, 0, 0, 4, 0, 6, 0, 0, 8},
{0, 0, 0, 0, 0, 0, 6, 2, 0},
{0, 0, 0, 0, 5, 0, 7, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 8, 2},
{5, 0, 3, 2, 0, 0, 0, 1, 0},
};
case 1:
return new int[][]{
{7, 5, 0, 9, 0, 0, 0, 4, 6},
{9, 0, 1, 0, 0, 0, 3, 0, 2},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 0, 0, 6, 0, 1, 0, 0, 7},
{0, 8, 0, 0, 0, 0, 0, 2, 0},
{1, 0, 0, 3, 0, 8, 0, 0, 5},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{3, 0, 9, 0,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, 1, 0, 7, 0, 8, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 4, 0, 5, 0},
{0, 0, 5, 0, 1, 0, 0, 0, 6},
{0, 0, 2, 0, 0, 6, 7, 9, 0},
};
}
return null;
}
class Timer extends JFrame {
public Timer() {
timer();
}
public void timer() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Timer frame = new Timer("Timer");
frame.pack();
frame.setVisible(true);
}
private static final long serialVersionUID = 1L;
private static final String INITIAL_LABEL_TEXT = "00:00:00 000";
// 计数线程
private CountingThread thread = new CountingThread();
// 记录程序开始时间
private long programStart = System.currentTimeMillis();
// 程序一开始就是暂停的
private long pauseStart = programStart;
// 程序暂停的总时间
private long pauseCount = 0;
private JLabel label = new JLabel(INITIAL_LABEL_TEXT);
private JButton pauseButton = new JButton("暂停");
private JButton resetButton = new JButton("清零");
private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thread.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart);
thread.stopped = false;
} else {
pauseStart = System.currentTimeMillis();
thread.stopped = true;
for (String str : SudokuGrid.result) {
if (str.equals("Right")) {
long elapsed = System.currentTimeMillis() - programStart - pauseCount;
String time = String.format(String.valueOf(elapsed)) + " ";
WriteToText(time);
}
}
}
}
};