-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuBoard.java
169 lines (137 loc) · 3.63 KB
/
SudokuBoard.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
import java.lang.StringBuilder;
import java.util.List;
import java.util.LinkedList;
import java.util.Stack;
import java.lang.RuntimeException;
public class SudokuBoard {
public static final int WIDTH = 9;
public static final int HEIGHT = 9;
private Cell[][] board;
public SudokuBoard(int[][] values) {
this();
if(values.length != WIDTH && values[0].length != HEIGHT) {
throw new RuntimeException("illegal state");
}
for (int x=0;x<WIDTH;x++) {
for (int y=0;y<HEIGHT;y++) {
if (values[y][x] != 0) {
if (values[y][x] >= 1 && values[y][x] <=9) {
makeMove(x,y,values[y][x] - 1);
} else {
throw new RuntimeException("illegal state");
}
}
}
}
}
public SudokuBoard() {
board = new Cell[WIDTH][HEIGHT];
for (int x=0;x<WIDTH;x++) {
for (int y=0;y<HEIGHT;y++) {
board[x][y] = new EmptyCell(x,y);
}
}
}
public List<Cell> getAffectingCells(Cell cell) {
List<Cell> result = new LinkedList<>();
for (int x=0;x<WIDTH;x++) {
if (x != cell.getX()) {
result.add(board[x][cell.getY()]);
}
}
for (int y=0;y<HEIGHT;y++) {
if (y != cell.getY()) {
result.add(board[cell.getX()][y]);
}
}
int[] quadrant = {cell.getX()/3,cell.getY()/3};
for (int x = 3*quadrant[0];x < 3*quadrant[0] + 3;x++) {
for (int y = 3*quadrant[1];y < 3*quadrant[1] + 3;y++) {
if (x != cell.getX() && y != cell.getY()) {
result.add(board[x][y]);
}
}
}
return result;
}
public Move makeMove(int x,int y,int value) {
Cell cell = this.board[x][y];
if (!(cell instanceof EmptyCell)) {
throw new RuntimeException("illegal state");
} else {
return new Move(this,(EmptyCell)cell,new Value((EmptyCell)cell,value));
}
}
public Move makeMove(EmptyCell cell,int value) {
return this.makeMove(cell.getX(),cell.getY(),value);
}
public Cell getCell(int x,int y) {
return board[x][y];
}
public void setCell(int x,int y,Cell cell) {
board[x][y] = cell;
}
public List<Cell> getAllCells() {
List<Cell> result = new LinkedList<>();
for (int x=0;x<WIDTH;x++) {
for (int y=0;y<HEIGHT;y++) {
result.add(getCell(x,y));
}
}
return result;
}
public List<EmptyCell> getEmptyCells() {
List<EmptyCell> result = new LinkedList<>();
for (int x=0;x<WIDTH;x++) {
for (int y=0;y<HEIGHT;y++) {
Cell cell = getCell(x,y);
if (cell instanceof EmptyCell) {
result.add((EmptyCell)cell);
}
}
}
return result;
}
public List<Value> getValueCells() {
List<Value> result = new LinkedList<>();
for (int x=0;x<WIDTH;x++) {
for (int y=0;y<HEIGHT;y++) {
Cell cell = getCell(x,y);
if (cell instanceof Value) {
result.add((Value)cell);
}
}
}
return result;
}
public String toString() {
StringBuilder result = new StringBuilder("\n");
for (int y=0;y<HEIGHT;y++) {
for (int x=0;x<WIDTH;x++) {
if (board[x][y].isValue()) {
result.append(" "+Integer.toString(board[x][y].getValue()+1));
} else {
result.append(" E");
}
}
result.append("\n");
}
return result.toString();
}
public static void main(String[] args) {
SudokuBoard board = new SudokuBoard();
System.out.println(board);
System.out.println(board.getCell(1,1).possibleValues());
System.out.println(board.getCell(0,8).possibleValues());
try(Move move=board.makeMove(0,0,1)){
try(Move move2=board.makeMove(0,1,2)){
System.out.println(board);
System.out.println(board.getCell(1,1).possibleValues());
System.out.println(board.getCell(0,8).possibleValues());
}
}
System.out.println(board);
System.out.println(board.getCell(1,1).possibleValues());
System.out.println(board.getCell(0,8).possibleValues());
}
}