-
Notifications
You must be signed in to change notification settings - Fork 1
/
TableroH.java
270 lines (219 loc) · 5.56 KB
/
TableroH.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
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
import java.util.LinkedList;
import java.util.ArrayList;
/**
*@version 1.0
*@author Reyes Vera
*/
public class TableroH extends Tablero {
private int[][] idAreas;
private ArrayList<Area> areas;
public TableroH(int medida) {
super(medida,medida);
idAreas = new int[medida][medida];
for (int i = 0; i < files;++i){
for (int j= 0;j < files;++j) {
Casilla cas1 = new Casilla();
this.setCasilla(cas1,i,j);
idAreas[i][j] = -1;
}
}
areas = new ArrayList<Area>();
}
//Borrar, solo testeo
public void setResultToValue() {
for (int i=0; i<files; i++) {
for (int j=0; j<files; j++) {
tauler[i][j].valor = tauler[i][j].solucion;
}
}
}
public LinkedList<Casilla> getAllCasillas() {
LinkedList<Casilla> res = new LinkedList<Casilla>();
for (int i=0; i<this.files; i++) {
for (int j=0; j<this.files; j++) {
res.add(this.tauler[i][j]);
}
}
return res;
}
public LinkedList<Area> getAllAreas() {
return new LinkedList<Area>(areas);
}
public void afegirArea(Area a, int ress) {
a.set_res(ress);
areas.add(a);
}
public void setAreaRes(int x, int y, int res) {
getAreaByPos(x,y).set_res(res);
}
public void setAreaRes(int id, int res) {
getArea(id).set_res(res);
}
public void setid(int c, int x, int y) {
idAreas[x][y] = c;
areas.get(c).afegir_casella(tauler[x][y]);
}
public void setId(int c, int x, int y) {
idAreas[x][y] = c;
}
public void colocaRes() {
for (int i = 0; i < areas.size();++i) areas.get(i).calcular_resultat();
}
public int size() {
return this.files;
}
public int getAreaSize(int x, int y) {
return this.getAreaByPos(x,y).get_tamany();
}
public int getNumAreas() {
return areas.size();
}
public Area getArea(int n) { //n = area, retorna una llista
return areas.get(n);
}
public Boolean getAreaNum(int n) { //n = area, retorna una llista
if (areas.size() > n)
return true;
else return false;
}
public void config(){
for (int i = 0; i < files; ++i){
for (int j = 0; j < files; ++j){
areas.get(idAreas[i][j]).afegir_casella(tauler[i][j]);
}
}
}
public Boolean areaContains(Area area, int x, int y) {
int id = area.get_posicio();
return id == idAreas[x][y];
}
public Area getAreaByPos(int x, int y) {
return areas.get(this.idAreas[x][y]);
}
public Boolean casillaIsFija(int x, int y) {
return this.tauler[x][y].getFija();
}
public LinkedList<Integer> getCasillaDomain(int x, int y) {
Casilla casilla = this.getCasilla(x,y);
LinkedList<Integer> list = new LinkedList<Integer>();
int tmp = casilla.getCan(0);
for (int i=1; tmp != -1; i++) {
list.add(tmp);
tmp = casilla.getCan(i);
}
return list;
}
public Casilla getCasilla(int x, int y) {
return this.tauler[x][y];
}
public void setCasillaVal(int x, int y, int num) {
if (! this.tauler[x][y].getFija()) this.tauler[x][y].valor = num;
}
public void setCasillaFija(int val, int x, int y) {
this.tauler[x][y].fija = true;
this.tauler[x][y].solucion = val;
this.tauler[x][y].valor = val;
}
public int getCasillaVal(int x, int y) {
return this.tauler[x][y].valor;
}
public void setCasillaSol(int x, int y, int valor) {
this.tauler[x][y].solucion = valor;
}
public int getCasillaSol(int x, int y) {
return this.tauler[x][y].solucion;
}
public Boolean areaCheck(int area) {
return areas.get(area).correcte();
}
public Boolean tableroCheck() {
for (int i = 0; i < areas.size(); ++i) {
if (! areaCheck(i)) return false;
}
return true;
}
public Boolean numerosCheck() { //numeros no repes
Boolean vect[] = new Boolean[files];
for (int i = 0; i < files; ++i){
for (int k = 0; k < files; ++k) vect[k] = false;
for (int j = 0; j < files; ++j) {
if (vect[(tauler[i][j].valor) -1 ]) return false;
vect[(tauler[i][j].valor)-1] = true;
}
}
for (int i = 0; i < files; ++i){
for (int k = 0; k < files; ++k) vect[k] = false;
for (int j = 0; j < files; ++j){
if (vect[(tauler[j][i].valor) -1 ]) return false;
vect[(tauler[j][i].valor)-1] = true;
}
}
return true;
}
public int getAreaID(int x, int y) {
return this.idAreas[x][y];
}
public Area getArea(int x, int y) { // Devuelve el area asociada a la posicion x,y.
int a = this.idAreas[x][y];
return areas.get(a);
}
public int calcuDif() {
int var = 0;
for (int i = 0; i < files; ++i){
for (int j = 0; j < files; ++j){
if (tauler[i][j].fija) ++var;
}
}
if (var >= 1 && var <= 16) var = 0;
else if (var > 16 && var <= 49) var = 1;
else var = 2;
return var;
}
public String getDomains () {
String res = "";
for (int i = 0; i < files;++i){
for (int j = 0; j < files; ++j){
res = res + getCasillaDomain(i,j) + " ";
}
res = res + "\n";
}
return res;
}
public String getSolucion () {
String res = "";
for (int i = 0; i < files;++i){
for (int j = 0; j < files; ++j){
res = res + getCasillaSol(i,j) + " ";
}
res = res + "\n";
}
return res;
}
public String getAreas () {
String res = "";
for (int i = 0; i < files;++i){
for (int j = 0; j < files; ++j){
res = res + idAreas[i][j] + " ";
}
res = res + "\n";
}
return res;
}
public String toString() { //devuelve los contenidos de un tablero en una string
String res = "";
for (int i = 0; i < files;++i){
for (int j = 0; j < files; ++j){
res = res + getCasillaVal(i,j) + " ";
}
res = res + "\n";
}
for (int i = 0; i < files;++i){
for (int j = 0; j < files; ++j){
res = res + getCasillaSol(i,j) + " ";
}
res = res + "\n";
}
for (int i = 0; i < areas.size();++i) res = res + areas.get(i);
return res;
}
}