-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTComponent.java
More file actions
310 lines (275 loc) · 6.11 KB
/
TComponent.java
File metadata and controls
310 lines (275 loc) · 6.11 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import java.util.HashSet;
import java.util.Iterator;
public class TComponent {
HashSet<Grid> comp;
int left;
int right;
int top;
int bottom;
public TComponent(){
left = 99999;
right = -1;
top = 99999;
bottom = -1;
comp = new HashSet<Grid>();
}
public TComponent(HashSet<Grid> c){
comp = new HashSet<Grid>();
Iterator iter = c.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
Grid addtoG = new Grid(ng.getRow(), ng.getCol(), ng.getValue());
comp.add(addtoG);
if( ng.getRow()< left){
left = ng.getRow();
}
if( ng.getRow() > right){
right = ng.getRow();
}
if( ng.getCol() < top){
top = ng.getCol();
}
if ( ng.getCol() > bottom){
bottom = ng.getCol();
}
}
}
public HashSet<Grid> rotate90(){
HashSet<Grid> r90 = new HashSet<Grid>();
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
Grid newg = new Grid(ng.getRow(), ng.getCol(), ng.getValue());
int r = newg.getCol();
int c = -newg.getRow();
newg.setRow(r);
newg.setCol(c);
r90.add(newg);
}
return r90;
}
public void update(){
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid nextG = (Grid)iter.next();
nextG.setDim(nextG.getRow()-left, nextG.getCol()-top);
}
right = right-left;
bottom = bottom-top;
left=0;
top=0;
}
public HashSet<Grid> rotate180(){
HashSet<Grid> r180 = new HashSet<Grid>();
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
Grid newg = new Grid(ng.getRow(), ng.getCol(), ng.getValue());
int r = -newg.getRow();
int c = -newg.getCol();
newg.setRow(r);
newg.setCol(c);
r180.add(newg);
}
return r180;
}
public HashSet<Grid> rotate270(){
HashSet<Grid> r270 = new HashSet<Grid>();
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
Grid newg = new Grid(ng.getRow(), ng.getCol(), ng.getValue());
int r = -newg.getCol();
int c = newg.getRow();
newg.setRow(r);
newg.setCol(c);
r270.add(newg);
}
return r270;
}
public HashSet<Grid> rlReflect(){
HashSet<Grid> rlR = new HashSet<Grid>();
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
Grid newg = new Grid(ng.getRow(), ng.getCol(), ng.getValue());
int r = newg.getRow();
int c = -newg.getCol();
newg.setRow(r);
newg.setCol(c);
rlR.add(newg);
}
return rlR;
}
public HashSet<Grid> tbReflect(){
HashSet<Grid> tbR = new HashSet<Grid>();
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
Grid newg = new Grid(ng.getRow(), ng.getCol(), ng.getValue());
int r = -newg.getRow();
int c = newg.getCol();
newg.setRow(r);
newg.setCol(c);
tbR.add(newg);
}
return tbR;
}
public boolean isRLSym(){
boolean flag = true;
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
if(ng.getValue()!= this.getGrid(ng.getRow(), (bottom-ng.getCol())).getValue()){
flag = false;
}
}
return flag;
}
public boolean isTBSym(){
boolean flag = true;
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = ((Grid)iter.next());
if(ng.getValue()!= this.getGrid((right-ng.getRow()), ng.getCol()).getValue()){
flag = false;
}
}
return flag;
}
public int size(){
return comp.size();
}
public int boxSize(){
return (right-left+1)*(bottom-top+1);
}
public void add(Grid g){
comp.add(g);
if( g.getRow()< left){
left = g.getRow();
}
if( g.getRow() > right){
right = g.getRow();
}
if( g.getCol() < top){
top = g.getCol();
}
if ( g.getCol() > bottom){
bottom = g.getCol();
}
} //add a grid to the component
public int[] getDim(){
int[] dim = new int[4];
dim[0] = left;
dim[1] = right;
dim[2] = top;
dim[3] = bottom;
return dim;
}
public int getLeft(){
return left;
}
public int getRight(){
return right;
}
public int getTop(){
return top;
}
public int getBottom(){
return bottom;
}
public void clear(){
Iterator iter = comp.iterator();
while(iter.hasNext()){
((Grid)iter.next()).setValue('.');
}
}
public boolean isClear(){
boolean flag = true;
Iterator iter = comp.iterator();
while(iter.hasNext()){
char value = ((Grid)iter.next()).getValue();
if(value!='.'){
flag = false;
}
}
return flag;
}
public void delete(){
comp.clear();
}
public Grid getGrid(int row, int col){
Pair p = new Pair(row, col);
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid nextG = (Grid)iter.next();
if (nextG.getDim().equals(row, col)){
return nextG;
}
}
Grid g = new Grid(row, col, ' ');
return g;
}
public Grid getGrid(int row, int col, char c){
Pair p = new Pair(row, col);
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid nextG = (Grid)iter.next();
if (nextG.getDim().equals(row, col)){
return nextG;
}
}
Grid g = new Grid(row, col, '.');
return g;
}
public HashSet<Grid> getComp(){
return comp;
}
public boolean fullyMarked(){
boolean flag = true;
Iterator iter = comp.iterator();
while(iter.hasNext()){
char value = ((Grid)iter.next()).getValue();
if(value=='.'){
flag = false;
}
}
return flag;
}
public void print(){
Grid g = new Grid();
for(int i = left; i <= right; i++){
for(int j = top; j <= bottom; j++){
g = getGrid(i, j, '.');
System.out.print(g.getValue());
}
System.out.println(" ");
}
}
public boolean isSingle(){
boolean flag = true;
Iterator iter = comp.iterator();
char value = ((Grid)iter.next()).getValue();
while(iter.hasNext()){
Grid nextG = (Grid)iter.next();
if(nextG.getValue()!=value){
flag = false;
}
}
return flag;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof TComponent)) return false;
TComponent Tobj = (TComponent) o;
boolean flag = true;
Iterator iter = comp.iterator();
while(iter.hasNext()){
Grid ng = (Grid)iter.next();
if( !ng.equals(Tobj.getGrid(ng.getRow(), ng.getCol())))
flag = false;
}
return flag;
}
}