-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.java
More file actions
89 lines (83 loc) · 2.41 KB
/
Group.java
File metadata and controls
89 lines (83 loc) · 2.41 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
import java.util.ArrayList;
import java.util.HashMap;
public class Group {
private int tileIndex = 0;
private Tile[] _tiles;
public HashMap<Integer, Integer> inGroup = new HashMap<>();
private boolean _solved;
public char _grpType;
public Group(int dim, char type) {
_grpType = type;
_tiles = new Tile[dim];
_solved = false;
}
public Group(Tile[] tiles, int dim, char type) {
_grpType = type;
_tiles = new Tile[dim];
for (Tile t : tiles) {
this.addTile(t);
}
_solved = false;
}
public boolean isValid() {
HashMap<Integer, Integer> occurrences = new HashMap<>();
if (_tiles != null){
for (Tile t : _tiles) {
if (t._num != 0) {
if (occurrences.containsKey(t._num)){
return false;
} else {
occurrences.put(t._num, 1);
}
}
}
}
return true;
}
public boolean checkSolved() {
if (!isValid()) {
return false;
} else {
HashMap<Integer, Integer> occurrences = new HashMap<>();
if (_tiles != null){
for (Tile t : _tiles) {
if (t._num != 0) {
return false;
} else if(occurrences.containsKey(t._num)) {
return false;
} else {
occurrences.put(t._num, 1);
}
}
}
return occurrences.size() == 9;
}
}
public Tile[] get_tiles() {
return _tiles;
}
public void addTile(Tile tile) {
if (tileIndex >= _tiles.length) {
System.exit(1);
} else {
if (_grpType == 'c') {
tile._column = (Board.Column) this;
} else if (_grpType == 'r') {
tile._row = (Board.Row) this;
} else if (_grpType == 's') {
tile._square = (Board.Square) this;
}
if (tile._num != 0) {
this.inGroup.put(tile._num, tile._num);
}
_tiles[tileIndex] = tile;
this.tileIndex += 1;
}
}
public void setTile(Tile t, int index) {
_tiles[index] = t;
}
public Tile getTile(int index) {
return _tiles[index];
}
}