-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.java
More file actions
249 lines (213 loc) · 8.32 KB
/
solver.java
File metadata and controls
249 lines (213 loc) · 8.32 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
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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
/**
* Created by kevin on 3/5/2017.
*/
public class solver {
//global puzzle value to allow multiple threads to change it
public static int[][] puzzle = new int[9][9];
public solver()
{
// =D
}
public static void readFile()
/*
Reads a .txt file into a string one line at a time
The string reads into a 2d array
A new line in the .txt file trips a "row" counter to begin the new line in the array
*/
{
Scanner scan = null;try {
String inputLine = "";
int row = 0;
scan = new Scanner
(new BufferedReader
(new FileReader
("C:\\Users\\kevin\\Desktop\\sudoku.txt")));
while (scan.hasNextLine()) {
inputLine = scan.nextLine();
inputLine = inputLine.replace("\n", "").replace("\r", "");
String [] inArray = inputLine.split(",");
for (int x = 0; x<inArray.length;++x) {
puzzle[row][x] = Integer.parseInt(inArray[x]);
}
row++;
}
System.out.println("Text file read successfully!");
}catch (FileNotFoundException e) {
System.out.println("The file was not found!");
e.printStackTrace();
} catch (Exception e) {
System.out.println("The following error accurred" + e);
}
}
public static void check3x3(int x, int y)
/*
Runs through the array one value at a time
After a value is encountered, run through the array one element at a time beginning with the element in front of it
Check both values against eachother
If a duplicate is found, run a function to generate the replacement
Set array element equal to replacement value
*/
{
for (int i = x; i < x + 3; ++i)
{
for (int j = y; j < y+3; ++j)
{
for (int k = i+1; k<3; ++k)
{
for (int l = j+i; l<3; k++)
{
if (puzzle[i][j] == puzzle[l][k]) {
int newNum = replacement(x,y);
System.out.println("An error was found at [" + i + "],[" + j + "] " +
" in the grid [" + x + "],[" + y + "] " +
"and will be replaced with " + newNum);
puzzle[i][j] = newNum;
}//sorry
}//sorry
}//sorry
}//I know
}//sorry
}//...sorry
public static void checkRows(int completed, int x)
/*
Checks row x for duplicates by running through the array
All elements are checked against the remaining elements of the array starting with the beginning of the row
If a duplicate is found, the first duplicate value is swapped with the value on the row below it
If a duplicate is found in the final row, it is swapped with the corresponding value in the first row
Function runs recursively until all rows have been checked without errors
*/
{
for (int i = 0; i <9; ++i)
{
for (int j = i+1; j < 9; ++j)
{
if (puzzle[x][i] == puzzle [x][j]) {
System.out.println("A duplicate value was found at [" + x + "],[" + i + "] " +
"and will swap places with the value below it");
if (x == 8) {
int temp = puzzle[x][i];
puzzle[8][i] = puzzle[0][i];
puzzle[0][i] = temp;
} else {
int temp = puzzle[x][i];
puzzle[x][i] = puzzle[x+1][i];
puzzle[x+1][i] = temp;
}
//if an error is found, reset the counter to 0
completed = 0;
}
}
}
completed++;
/*
NOTE: "completed" will always be incremented by one even after the counter is reset
NOTE: to counteract this, I check for 9 iterations rather than 8 (the index size of a sudoku board)
If not enough iterations have been completed, check the next row in the puzzle array
If the row being check was the final row, reset the counter to start checking the beginning row
*/
if (completed < 9) {
if (x == 8) {
x = 0;
} else {
checkRows(completed, x++);
}
} else {
System.out.println("Vertical lines have been validated and are complete");
}
}
public static void checkColumns(int completed, int x)
/*
Checks column x for duplicates by running through the array
All elements are checked against the remaining elements of the array starting with the beginning of the column
If a duplicate is found, the first duplicate value is swapped with the value on the column next to it
If a duplicate is found in the final column, it is swapped with the corresponding value in the first column
Function runs recursively until all column have been checked without errors
*/
{
for (int i = 0; i <9; ++i)
{
for (int j = i+1; j < 9; ++j)
{
if (puzzle[x][i] == puzzle [x][j])
{
if (x == 8) {
System.out.println("A duplicate value was found at [" + i + "],[" + x + "] " +
"and will swap places with the value beside it");
int temp = puzzle[i][x];
puzzle[i][8] = puzzle[i][0];
puzzle[i][0] = temp;
} else {
int temp = puzzle[i][x];
puzzle[i][x] = puzzle[i][x+1];
puzzle[i][x+1] = temp;
}
completed = 0;
}
}
}
completed++;
/*
NOTE: "completed" will always be incremented by one even after the counter is reset
NOTE: to counteract this, I check for 9 iterations rather than 8 (the index size of a sudoku board)
If not enough iterations have been completed, check the next row in the puzzle array
If the row being check was the final row, reset the counter to start checking the beginning row
*/
if (completed < 9) {
if (x == 8) {
x = 0;
} else {
checkColumns(completed, x++);
}
} else {
System.out.println("All columns have been validated and are complete");
}
}
public static int replacement(int x, int y)
/*
Calculates a replacement value for a 3x3 grid
takes in 3x3 beginning coordinates as parameters
checks corresponding 3x3 for a 1 - 9 value that is NOT currently used by the 3x3
Returns the value to be the replacement for a duplicate
*/
{
//counter acts as the value that is being checked for in the 3x3
int counter = 1;
boolean valueMatch = false;
int replacement = 0;
//check for a value = to counter
//if one is found, do nothing
for (int k = 0; k < 9; ++k) {
for (int i = x; i < x + 3; ++i) {
for (int j = y; j < y + 3; ++j) {
if (puzzle[i][j] == counter) {
valueMatch = true;
}
}
}
//if a cycle goes through the 3x3 without encountering a match;
//return the value of the counter as the replacement
//otherwise, check for the next value by incrementing "counter"
if (!valueMatch) {
replacement = counter;
}
counter++;
valueMatch = false;
}
return replacement;
}
public static void printPuzzle()
{
for (int i = 0; i<9; ++i)
{
for (int j = 0; j < 9; ++j)
{
System.out.print(puzzle[i][j]);
}
System.out.println("\n");
}
}
}