-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.jack
189 lines (148 loc) · 3.7 KB
/
Main.jack
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
// Implementation Suduko Solver in Jack
class Main {
function int remainder(int a, int b){
var int minus;
var int quiotent;
var int rem;
var int sqt;
let minus = a - b;
let quiotent = 0;
while(minus > -1){
let minus = minus - b;
let quiotent = quiotent + 1;
}
let rem = minus+b;
return rem;
}
/* checks whether the given number is in the given row */
function boolean isNumberinRow(MultiArray a, int n, int r){
var int i;
let i = 0;
while(i < 9){
if(a.get(r,i) = n){
return true;
}
let i = i + 1;
}
return false;
}
/* checks whether the given number is in the given coloumn */
function boolean isNumberinColumn(MultiArray a, int n, int c){
var int i;
let i = 0;
while(i < 9){
if(a.get(i,c) = n){
return true;
}
let i = i + 1;
}
return false;
}
/* checks whether the given number is in the given 3x3 box */
function boolean isinBox(MultiArray a, int n, int r, int c){
var int boxRowSt, boxColSt;
var int i,j;
let boxRowSt = (r - (Main.remainder(r,3)));
let boxColSt = (c - (Main.remainder(c,3)));
let i = boxRowSt;
while(i < (boxRowSt + 3)){
let j = boxColSt;
while(j < (boxColSt + 3)){
if(a.get(i,j) = n){
return true;
}
let j = j + 1;
}
let i = i + 1;
}
return false;
}
/* checks whether is itvalid to insert a number into the given row and coloumn */
function boolean isvalidPlacement(MultiArray a, int n, int r, int c){
return (~((Main.isNumberinRow(a,n,r)) | (Main.isNumberinColumn(a,n,c)) | (Main.isinBox(a,n,r,c))));
}
function boolean solveSuduko(MultiArray a){
var int row;
var int col;
var int numtotry;
let row = 0;
while(row < 9){
let col = 0;
while(col < 9){
if((a.get(row,col)) = 0){
let numtotry = 1;
while(numtotry < 10){
if(Main.isvalidPlacement(a,numtotry,row,col)){
do a.add(row,col,numtotry);
if(Main.solveSuduko(a)){
return true;
}
else{
do a.add(row,col,0);
}
}
let numtotry = numtotry + 1;
}
return false;
}
let col = col + 1;
}
let row = row + 1;
}
return true;
}
function void main(){
var MultiArray b;
var int i, j;
let b = MultiArray.new(9);
/* Here we are initially adding some numbers into Suduko borad */
do b.add(0,0,7);
do b.add(0,2,2);
do b.add(0,4,5);
do b.add(0,6,6);
do b.add(1,5,3);
do b.add(2,0,1);
do b.add(2,5,9);
do b.add(2,6,5);
do b.add(3,0,8);
do b.add(3,7,9);
do b.add(4,1,4);
do b.add(4,2,3);
do b.add(4,6,7);
do b.add(4,7,5);
do b.add(5,1,9);
do b.add(5,8,8);
do b.add(6,2,9);
do b.add(6,3,7);
do b.add(6,8,5);
do b.add(7,3,2);
do b.add(8,2,7);
do b.add(8,4,4);
do b.add(8,6,2);
do b.add(8,8,3);
if(Main.solveSuduko(b)){
do Output.printString("The Suduko is Solved");
}
else{
do Output.printString("The Suduko is Unsolved");
}
do Output.println();
do Output.println();
do Output.printString("-----------------------------------");
do Output.println();
let i = 0;
while (i < 9) {
let j = 0;
while (j < 9) {
do Output.printInt(b.get(i, j));
do Output.printString(" | ");
let j = j + 1;
}
do Output.println();
do Output.printString("-----------------------------------");
do Output.println();
let i = i + 1;
}
return;
}
}