-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabellone.java
232 lines (206 loc) · 6.45 KB
/
Tabellone.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
import java.util.Random;
public class Tabellone {
private int n;
private int M;
private Cella[][] celle;
private Cella[] posizioni;
private Cella[] fine;
public Tabellone(int n, int M, boolean[] start) throws Exception{
if(n<M){
throw new Exception("n deve essere più grande di M");
}
this.n = n;
this.M = M;
int cont = 0;
for(int i = 0; i<start.length; i++){
if(start[i]){
cont++;
}
}
if(start.length!=n || cont != M){
throw new Exception("array di start non valido");
}
inizializzazioneTabellone(n,start);
}
public Tabellone(int n, int M) throws Exception{
if(n<M){
throw new Exception("n deve essere più grande di M");
}
this.n = n;
this.M = M;
boolean[] start=createBooleanArray(n, M);
inizializzazioneTabellone(n,start);
}
public Tabellone(){
Random rand = new Random();
n = rand.nextInt(50)+1;
M = rand.nextInt(n);
if(M==0){
M=1;
}
boolean[] start = createBooleanArray(n, M);
inizializzazioneTabellone(n,start);
}
public Tabellone(Tabellone altro){
this.n = altro.getn();
this.M = altro.getM();
this.celle = altro.getCelle();
this.posizioni = altro.getPosizioni();
this.fine = altro.getFine();
}
private void inizializzazioneTabellone(int n,boolean[] start) {
celle = new Cella[n][n];
for (int x=0; x<n;x++){
for (int y=0; y<n; y++){
celle[x][y] = new Cella(x,y);
}
}
for(int x=0; x<n;x++){
for(int y=0; y<n; y++){
if(x!=0){
celle[x][y].addAdiacente(celle[x-1][y]);
}
if(x!=n-1){
celle[x][y].addAdiacente(celle[x+1][y]);
}
if(y!=0){
celle[x][y].addAdiacente(celle[x][y-1]);
}
if(y!=n-1){
celle[x][y].addAdiacente(celle[x][y+1]);
}
}
}
posizioni = new Cella[M];
fine = new Cella[M];
int id = 0;
for(int y = 0; y<n; y++){
if(start[y]){
Veicolo v = new Veicolo(id);
celle[0][y].addStart(v);
celle[n-1][n-1-y].addFinish(v);
posizioni[id]= celle[0][y];
fine[id] = celle[n-1][n-1-y];
id++;
}
}
}
private boolean[] createBooleanArray(int n, int m) {
boolean[] arr = new boolean[n];
Random rand = new Random();
for (int i = 0; i < m; i++) {
arr[i] = true;
}
for (int i = n - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
boolean temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
public Tabellone getNextStato(int id, char d, Tabellone in){
Cella cella = in.posizioni[id];
Veicolo v = cella.getCar();
Tabellone ret = new Tabellone(in);
if(d == 'n'){
return ret;
}
else if(d == 'u'){
Cella su = ret.celle[cella.getX()][cella.getY()-1];
if(cella.getAdiacenti().contains(su)){
su.setOccupato(v);
ret.celle[cella.getX()][cella.getY()-1] = su;
cella.freeCella();
ret.celle[cella.getX()][cella.getY()] = cella;
ret.posizioni[id]=su;
}
return ret;
}
else if(d == 'r'){
Cella destra = ret.celle[cella.getX()+1][cella.getY()];
if(cella.getAdiacenti().contains(destra)){
destra.setOccupato(v);
ret.celle[cella.getX()+1][cella.getY()] = destra;
cella.freeCella();
ret.celle[cella.getX()][cella.getY()]=cella;
ret.posizioni[id]=destra;
}
return ret;
}
else if(d == 'd'){
Cella giu = ret.celle[cella.getX()][cella.getY()+1];
if(cella.getAdiacenti().contains(giu)){
giu.setOccupato(v);
ret.celle[cella.getX()][cella.getY()+1] = giu;
cella.freeCella();
ret.celle[cella.getX()][cella.getY()]=cella;
ret.posizioni[id]=giu;
}
return ret;
}
else {
Cella sinistra = ret.celle[cella.getX()-1][cella.getY()];
if(cella.getAdiacenti().contains(sinistra)){
sinistra.setOccupato(v);
ret.celle[cella.getX()-1][cella.getY()] = sinistra;
cella.freeCella();
ret.celle[cella.getX()][cella.getY()]=cella;
ret.posizioni[id]=sinistra;
}
return ret;
}
}
public Tabellone getNextStato(int[][] ids){
Tabellone ret = new Tabellone(this);
for(int i=0; i<ids.length;i++){
if (ids[i][1]==0){
ret=getNextStato(ids[i][0],'n',ret);
}
else if(ids[i][1]==1){
ret=getNextStato(ids[i][0],'u',ret);
}
else if(ids[i][1]==2){
ret=getNextStato(ids[i][0],'r',ret);
}
else if(ids[i][1]==3){
ret=getNextStato(ids[i][0],'d',ret);
}
else{
ret=getNextStato(ids[i][0],'l',ret);
}
}
return ret;
}
public boolean checkWinCon(){
boolean flag = true;
for(int i=0; i<posizioni.length; i++){
if(posizioni[i].getFinish()==false || posizioni[i].getCar()!=posizioni[i].getFinishCar()){
flag = false;
}
}
return flag;
}
public int getEuristicaStato(){
int tmp = 0;
for(int i=0; i<M; i++){
tmp = tmp + (int)((Math.abs(posizioni[i].getX()-fine[i].getX()) + Math.abs(posizioni[i].getY()-fine[i].getY()))/2);
}
return tmp;
}
public int getn(){
return n;
}
public int getM(){
return M;
}
public Cella[][] getCelle(){
return celle;
}
public Cella[] getPosizioni(){
return posizioni;
}
public Cella[] getFine(){
return fine;
}
}