-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudoku.java
More file actions
151 lines (98 loc) · 2.96 KB
/
Copy pathSudoku.java
File metadata and controls
151 lines (98 loc) · 2.96 KB
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
package Projects;
public class Sudoku {
//Joshua Reyes
//initiate a valid and sudoku puzzle for debugging and testing
static int[][] puzzleTrue = new int[][] { {6,2,4,5,3,9,1,8,7},
{5,1,9,7,2,8,6,3,4},
{8,3,7,6,1,4,2,9,5},
{1,4,3,8,6,5,7,2,9},
{9,5,8,2,4,7,3,6,1},
{7,6,2,3,9,1,4,5,8},
{3,7,1,9,5,6,8,4,2},
{4,9,6,1,8,2,5,7,3},
{2,8,5,4,7,3,9,1,6}
};
// init term for thread usage and completeSet to determine if the puzzle is valid
static int term;
static boolean completeSet = true;
int rows;
int cols;
int [][] puzzle;
static Thread[] valid = new Thread[27];
public Sudoku(int rows,int cols,int[][]puzzle) {
this.rows = rows;
this.cols = cols;
this.puzzle = puzzle;
}
public static void main(String [] args) {
//create threads
for(int i =0;i<9;i++) {
for(int j = 0; j<9; j++) {
valid[i] = new Thread(new Sudoku(i,0,puzzleTrue).new rows());
valid[i+9] = new Thread(new Sudoku(0,i,puzzleTrue).new columns());
if(i%3== 0 || j%3 ==0) valid[i+18] = new Thread(new Sudoku(i,j,puzzleTrue).new square());
}
}
//start and join threads
for(int i =0;i<18;i++) {
valid[i].start();
}
try {
for(int i = 0; i<27; i++) {
valid[i].join();
}
}catch(Exception e) {
System.out.println(e.getMessage());
}
if(completeSet == true) System.out.println("This is a valid Sudoku puzzle.");
else System.out.println("This is not a valid Sudoku puzzle.");
}
//threads that check if the rows are valid
private class rows implements Runnable{
@Override
public void run() {
boolean[] allNine = new boolean [9];
int row = rows;
for(int i = 0; i<9;i++){
term = puzzle[row][i];
if(allNine[term-1]) {
completeSet = false;
return;
}else allNine[term -1] = true;
}
}
}
//thread that checks if the columns are valid
private class columns implements Runnable{
@Override
public void run() {
boolean[] allNine = new boolean [9];
int col = cols;
for(int i = 0; i<9;i++){
term = puzzle[i][col];
if(allNine[term-1]) {
completeSet = false;
return;
}else allNine[term -1] = true;
}
}
}
//thread that checks if the squares are valid
private class square implements Runnable{
@Override
public void run() {
boolean[] allNine = new boolean[9];
int row = rows;
int col = cols;
for(int i = row; i<row + 3; i++){
for(int j = col; j<col + 3; j++) {
term = puzzle[i][j];
if(allNine[term-1]) {
completeSet = false;
return;
}else allNine[term -1] = true;
}
}
}
}
}