-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuPuzzle.java
194 lines (166 loc) · 4.41 KB
/
SudokuPuzzle.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
package sudoku;
public class SudokuPuzzle {
protected String [][] board;
// Table to determine if a slot is mutable
protected boolean [][] mutable;
private final int ROWS;
private final int COLUMNS;
private final int BOXWIDTH;
private final int BOXHEIGHT;
private final String [] VALIDVALUES;
public SudokuPuzzle(int rows,int columns,int boxWidth,int boxHeight,String [] validValues) {
this.ROWS = rows;
this.COLUMNS = columns;
this.BOXWIDTH = boxWidth;
this.BOXHEIGHT = boxHeight;
this.VALIDVALUES = validValues;
this.board = new String[ROWS][COLUMNS];
this.mutable = new boolean[ROWS][COLUMNS];
initializeBoard();
initializeMutableSlots();
}
public SudokuPuzzle(SudokuPuzzle puzzle) {
this.ROWS = puzzle.ROWS;
this.COLUMNS = puzzle.COLUMNS;
this.BOXWIDTH = puzzle.BOXWIDTH;
this.BOXHEIGHT = puzzle.BOXHEIGHT;
this.VALIDVALUES = puzzle.VALIDVALUES;
this.board = new String[ROWS][COLUMNS];
for(int r = 0;r < ROWS;r++) {
for(int c = 0;c < COLUMNS;c++) {
board[r][c] = puzzle.board[r][c];
}
}
this.mutable = new boolean[ROWS][COLUMNS];
for(int r = 0;r < ROWS;r++) {
for(int c = 0;c < COLUMNS;c++) {
this.mutable[r][c] = puzzle.mutable[r][c];
}
}
}
public int getNumRows() {
return this.ROWS;
}
public int getNumColumns() {
return this.COLUMNS;
}
public int getBoxWidth() {
return this.BOXWIDTH;
}
public int getBoxHeight() {
return this.BOXHEIGHT;
}
public String [] getValidValues() {
return this.VALIDVALUES;
}
public void makeMove(int row,int col,String value,boolean isMutable) {
if(this.isValidValue(value) && this.isValidMove(row,col,value) && this.isSlotMutable(row, col)) {
this.board[row][col] = value;
this.mutable[row][col] = isMutable;
}
}
public boolean isValidMove(int row,int col,String value) {
if(this.inRange(row,col)) {
if(!this.numInCol(col,value) && !this.numInRow(row,value) && !this.numInBox(row,col,value)) {
return true;
}
}
return false;
}
public boolean numInCol(int col,String value) {
if(col <= this.COLUMNS) {
for(int row=0;row < this.ROWS;row++) {
if(this.board[row][col].equals(value)) {
return true;
}
}
}
return false;
}
public boolean numInRow(int row,String value) {
if(row <= this.ROWS) {
for(int col=0;col < this.COLUMNS;col++) {
if(this.board[row][col].equals(value)) {
return true;
}
}
}
return false;
}
public boolean numInBox(int row,int col,String value) {
if(this.inRange(row, col)) {
int boxRow = row / this.BOXHEIGHT;
int boxCol = col / this.BOXWIDTH;
int startingRow = (boxRow*this.BOXHEIGHT);
int startingCol = (boxCol*this.BOXWIDTH);
for(int r = startingRow;r <= (startingRow+this.BOXHEIGHT)-1;r++) {
for(int c = startingCol;c <= (startingCol+this.BOXWIDTH)-1;c++) {
if(this.board[r][c].equals(value)) {
return true;
}
}
}
}
return false;
}
public boolean isSlotAvailable(int row,int col) {
return (this.inRange(row,col) && this.board[row][col].equals("") && this.isSlotMutable(row, col));
}
public boolean isSlotMutable(int row,int col) {
return this.mutable[row][col];
}
public String getValue(int row,int col) {
if(this.inRange(row,col)) {
return this.board[row][col];
}
return "";
}
public String [][] getBoard() {
return this.board;
}
private boolean isValidValue(String value) {
for(String str : this.VALIDVALUES) {
if(str.equals(value)) return true;
}
return false;
}
public boolean inRange(int row,int col) {
return row <= this.ROWS && col <= this.COLUMNS && row >= 0 && col >= 0;
}
public boolean boardFull() {
for(int r = 0;r < this.ROWS;r++) {
for(int c = 0;c < this.COLUMNS;c++) {
if(this.board[r][c].equals("")) return false;
}
}
return true;
}
public void makeSlotEmpty(int row,int col) {
this.board[row][col] = "";
}
@Override
public String toString() {
String str = "Game Board:\n";
for(int row=0;row < this.ROWS;row++) {
for(int col=0;col < this.COLUMNS;col++) {
str += this.board[row][col] + " ";
}
str += "\n";
}
return str+"\n";
}
private void initializeBoard() {
for(int row = 0;row < this.ROWS;row++) {
for(int col = 0;col < this.COLUMNS;col++) {
this.board[row][col] = "";
}
}
}
private void initializeMutableSlots() {
for(int row = 0;row < this.ROWS;row++) {
for(int col = 0;col < this.COLUMNS;col++) {
this.mutable[row][col] = true;
}
}
}
}