-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tile.java
88 lines (85 loc) · 2.28 KB
/
Tile.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
//import java.util.ArrayList;
//
//public class Tile {
// private int _row;
// private int _col;
// private int _square;
// private int _val;
// private ArrayList<Integer> candidates = new ArrayList<>();
// public Tile(int row, int col, int dim) {
// _col = col;
// _row = row;
// int sqSize = (int) Math.sqrt(dim);
// int sqRow = (row % sqSize) * sqSize;
// int sqCol = (col % sqSize);
// _square = sqRow + sqCol;
// _val = 0;
// }
//
//
// public int getCol() {
// return _col;
// }
// public int getRow() {
// return _row;
// }
// public int getSquare() {
// return _square;
// }
// public int getVal() {
// return _val;
// }
// public void setVal(int val) {
// _val = val;
// }
//
//}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Tile {
int _num = 0;
HashSet<Integer> candidates = new HashSet<>();
Board.Row _row;
Board.Column _column;
Board.Square _square;
Board _board;
public Tile(int num, Board b) {
_board = b;
if (num == 0) {
for (int i = 1; i < 10; i += 1) {
candidates.add(i);
}
}
this._num = num;
}
public void resetCandidates() {
if (_num == 0) {
for (int i = 1; i < 10; i += 1) {
candidates.add(i);
}
}
}
public void update() {
if (candidates != null && candidates.size() == 1) {
this._num = candidates.iterator().next();
addToGroup();
} else if (candidates != null) {
for (int candidate : candidates) {
if (_board.uniqueInGroup(_row).contains(candidate) || _board.uniqueInGroup(_column).contains(candidate) || _board.uniqueInGroup(_square).contains(candidate)) {
_num = candidate;
addToGroup();
return;
}
}
}
}
public void addToGroup() {
candidates = new HashSet<>();
this._row.inGroup.put(_num, _num);
this._column.inGroup.put(_num, _num);
this._square.inGroup.put(_num, _num);
_board.updateCandidates();
_board.updated = true;
}
}