-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.pde
143 lines (127 loc) · 3.94 KB
/
Grid.pde
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
class Grid implements Comparator<Grid>{
Cell[] cellArray;
int GRID_SIZE;
int GRID_UNIT_SIZE;
int index;
float CELL_WIDTH;
float prob;
String GRID_CODE;
Grid(String grid_code, int grid_size, int grid_unit_size, float cell_width) {
this.GRID_CODE = grid_code;
this.GRID_SIZE = grid_size;
this.GRID_UNIT_SIZE = grid_unit_size;
this.CELL_WIDTH = cell_width;
this.cellArray = new Cell[grid_size*grid_size];
for (int i=0; i<GRID_CODE.length();i++) {
int cell_value = Integer.parseInt(Character.toString(GRID_CODE.charAt(i)));
cellArray[i] = new Cell(i, cell_value,CELL_WIDTH);
}
}
Cell[] getCellArray() {
return this.cellArray;
}
int score() {
int score = 0;
for (Cell cell : cellArray) {
if (checkCell(cell))
score++;
}
return score;
}
int compare(Grid g1, Grid g2) {
return g1.score()-g2.score();
}
ArrayList <Cell> getEmptyCells() {
ArrayList <Cell> empty_cells = new ArrayList<Cell>();
for(int i=0; i<this.cellArray.length ; i++) {
if (this.cellArray[i].getNum()==0)
empty_cells.add(this.cellArray[i]);
}
return empty_cells;
}
int validCellsNum() {
int valid_cells_num = 0;
for (Cell c : this.cellArray) {
if (checkCell(c))
valid_cells_num++;
}
return valid_cells_num;
}
boolean colValid(Cell cell) {
for (int i = cell.getIndex()%9; i<=(cell.getIndex()%9)+72; i=i+GRID_SIZE) {
if (cell.getIndex()!=cellArray[i].getIndex() &&
cellArray[i].getNum()!=0 &&
cell.getNum()==cellArray[i].getNum())
return false;
}
return true;
}
boolean rowValid(Cell cell) {
int starting_row_index = (int) Math.floor(cell.getIndex()/9)*9;
for (int i = starting_row_index; i<=starting_row_index+8; i=i+1) {
if (cell.getIndex()!=cellArray[i].getIndex() &&
cellArray[i].getNum()!=0 &&
cell.getNum()==cellArray[i].getNum())
return false;
}
return true;
}
boolean unitValid(Cell cell) {
int cell_index = cell.getIndex();
int col_index = cell_index - (cell_index%3);
int starting_unit_index = col_index - 9*(((int) Math.floor(col_index/9))%3);
for (int i=0; i<3; i++) {
int start_cell_index = starting_unit_index + (9*i);
for (int j=0; j<3; j++) {
if (cell.getIndex()!=cellArray[start_cell_index+j].getIndex() &&
cellArray[start_cell_index+j].getNum()!=0 &&
cell.getNum()==cellArray[start_cell_index+j].getNum())
return false;
}
}
return true;
}
boolean checkCell(Cell cell) {
if (cell.getNum()==0 || cell.getNum()>9)
return false;
return colValid(cell) && rowValid(cell) && unitValid(cell);
}
void switchCells(Cell c1, Cell c2) {
int c1_num = c1.getNum();
c1.setNum(c2.getNum());
c2.setNum(c1_num);
}
void draw() {
int index = 0;
float y_start = 50;
for(int i=0; i<GRID_SIZE; i++) {
float x_start = 50;
for (int j=0; j<GRID_SIZE ; j++) {
stroke(0,0,0);
strokeWeight(1);
cellArray[index].setCoordinates(x_start, y_start);
strokeWeight(3);
cellArray[index].draw(x_start, y_start);
if(!this.checkCell(cellArray[index]) && cellArray[index].num!=0) {
stroke(255, 0, 0);
line(cellArray[index].x_pos+5, cellArray[index].y_pos+2, cellArray[index].x_pos+5, cellArray[index].y_pos+30);
}
x_start=x_start+CELL_WIDTH;
index++;
}
y_start=y_start+CELL_WIDTH;
}
y_start = 50;
for(int i=0; i<GRID_SIZE; i = i+GRID_UNIT_SIZE) {
float x_start = 50;
for (int j=0; j<GRID_SIZE ; j = j+GRID_UNIT_SIZE) {
stroke(0,0,0);
strokeWeight(5);
noFill();
rect(x_start, y_start, GRID_UNIT_SIZE*CELL_WIDTH, GRID_UNIT_SIZE*CELL_WIDTH);
x_start=x_start+GRID_UNIT_SIZE*CELL_WIDTH;
}
y_start=y_start+GRID_UNIT_SIZE*CELL_WIDTH;
}
}
}