Bonjour,

voici mon problème:

j'ai un tableau de 23x23 cases sur lequel se promene un robot qui doit ramasser des clous. Le robot ne peut pas sortir du tableau ni aller sur des cases de type EAU (<=> mur impossible a franchir).

Je voudrais réussir à créer une stratégie de déplacement pour le robot (gentiment nommé nono), utilisant une pile qui stocke les déplacements du robot... Pourriez-vous m'aider ?

Pour l'instant, je pense faire comme suit:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
 
public class Strategie4 {
 
    Direction tmp;
    private Direction dir [];
    public int var =0;
    private Plateau p;
    ArrayList pile;
    /**
     * 
     */
    public Strategie4(boolean tous, Plateau p) {
 
        pile = new ArrayList();
 
        this.dir = new Direction[4];
        this.dir[0]=Direction.Est;
        this.dir[1]=Direction.Nord;
        this.dir[2]=Direction.Ouest;
        this.dir[3]=Direction.Sud;
        this.p = p;
    }
    /**
     * 
     * @param d direction
     * @return l'indice du tableau dir correspondant a la direction d
     */
        int getDir(Direction d){
            int i;
            if(d == Direction.Est)return 0;
            if(d == Direction.Nord)return 1;
            if(d == Direction.Ouest)return 2;
            if(d == Direction.Sud)return 3;
            return 5;
        }
 
        /**
         * retourne vrai si on est pas deja passe par la
         * @param 
         * @return
         */
    public boolean dirValide(int i){
        int x,y;
        x = this.p.getR().getPosX();
        y = this.p.getR().getPosY();
        /*if(x > 0 && y > 0 
                && x < this.p.getPlateau().getLargeur()-1 
                && y < this.p.getPlateau().getLongueur()-1){*/
        switch (i){
        case 0 : x = Math.min(x+1,this.p.getPlateau().getLargeur()-1);break;
        case 1 : y = Math.max(y-1,0);break;
        case 2 : x = Math.max(x-1,0);break;
        case 3 : y = Math.min(y+1,this.p.getPlateau().getLongueur()-1);break;
        }
 
        return !this.passe(x,y) || this.isClou(x,y);
        //}
        //else return false;
    }
 
 
    /**
     * retourne vrai si la case n'a jamais ete visité et que ce n'est pas de l'eau
     * @param x
     * @param y
     * @return
     */
    public boolean libre(int x, int y){
        return p.getPlateau().getCase(x,y).getTypeCase() != TypeCase.PASSE
        && p.getPlateau().getCase(x,y).getTypeCase() != TypeCase.EAU;
    }
 
 
    public boolean passe(int x, int y){
        return p.getPlateau().getCase(x,y).getTypeCase() == TypeCase.PASSE;
 
    }
 
 
    public boolean isClou(int x, int y){
        return p.getPlateau().getCase(x,y).getTypeCase() == TypeCase.CLOU;
 
    }
 
 
    /**
     * deplace nono C'EST LA QU'IL Y A DES BUGS
     * @param i
     * @throws DeplacementImpossible
     * @throws JeuTermine
     * @throws JeuNonInitialise
     * @throws PasDeClou
     * @throws JeuNonTermine
     */
    public void deplacement(int i)
    throws DeplacementImpossible, JeuTermine, JeuNonInitialise, PasDeClou, JeuNonTermine{
        //int i,j=0;
        //i = (int)(Math.random()*100);
        i = i%4;
        this.tmp = this.dir[i];
 
        boolean ok=false;
        System.out.println();
        try{
            //TODO : si la case dans la direction ou je veux aller est prise 
            //je test les trois autres direction
            //si le resultat n'est pas bon dans ce cas je continue dans 
            //la premiere direction
 
            //        if(this.libre(p,x,y)){
            if(this.dirValide(i)){}/*
                System.out.print("ok ");
                Direction.afficher(this.dir[i]);
            p.avancer(this.dir[i]);
            //System.out.println("i = "+i);
            p.ramasserClou();
            }*/
        //C'EST CE QUI SUIS QUI NE MARCHE PAS
            else {
                if(this.dirValide((i+1)%4)){
                    i = ((i+1)%4);
                }
                else {
                    if(this.dirValide((i+2)%4)){
                        i = ((i+2)%4);                
                        }
                    else{ 
                        if(this.dirValide((i+3)%4)){
                            i = ((i+3)%4);                
                            }
                    }
                }
 
                this.var = this.getDir(this.dir[i%4]);
                p.avancer(this.dir[i%4]);
                p.ramasserClou();
            }
            p.avancer(this.dir[i%4]);
            p.ramasserClou();
 
            //}
            //else this.deplacement(p,++i);
        }
            catch(DeplacementImpossible e){
                //break;
 
                this.correction2();
                System.out.println(e);
            //break;
 
 
            }
 
 
        //    }
        //}
 
//    }
 
 
 
    }
 
 
    /**
     * algo de correction utilisé pour le deplacement
     * @throws JeuTermine
     * @throws JeuNonInitialise
     * @throws PasDeClou
     * @throws JeuNonTermine
     * @throws DeplacementImpossible
     */
    public void correction2() 
    throws JeuTermine, JeuNonInitialise, PasDeClou, JeuNonTermine, DeplacementImpossible{
        System.out.println("correction2");
        Direction d2 = null;
        int i,x,y;
        x = p.getR().getPosX();
        y = p.getR().getPosY();
        i = (int)(Math.random()*100);
        if(x==0 && y == 0){
            if(this.dir[this.var] == Direction.Sud){
            if(this.libre(x+1,y))
                d2 = Direction.Est;
            else //if(this.libre(x,y+1))
                d2 = Direction.Sud; 
            }
            else
                if(this.libre(x,y+1))
                    d2 = Direction.Sud;
                else //if(this.libre(x,y+1))
                    d2 = Direction.Est; 
 
        }
        else
            if(x == 0 && y == p.getPlateau().getLongueur()-1){
                if(this.dir[this.var] == Direction.Est){
                if(this.libre(x,y-1))
                    d2 = Direction.Nord;
                else 
                    d2 = Direction.Est;
                }
                else 
                    if(this.libre(x+1,y))
                        d2 = Direction.Est;
                    else 
                        d2 = Direction.Nord;
 
                }
 
            else
                if(y == 0 && x == p.getPlateau().getLargeur()-1){
                    if(this.dir[this.var] == Direction.Sud){
                    if(this.libre(x-1,y))
                        d2 = Direction.Ouest;
                    else 
                        d2 = Direction.Sud;
                }
                    else 
                        if(this.libre(x,y+1))
                            d2 = Direction.Sud;
                        else 
                            d2 = Direction.Ouest;
                    }
 
                else
                    if(y == p.getPlateau().getLongueur()-1 && x == p.getPlateau().getLargeur()-1){
                        if(this.dir[this.var] == Direction.Nord){
                        if(this.libre(x-1,y))
                            d2 = Direction.Ouest;
                        else 
                            d2 = Direction.Nord;
                    }
                        else
                            if(this.libre(x,y-1))
                                d2 = Direction.Nord;
                            else 
                                d2 = Direction.Ouest;
                        }
                    else
                        if(y == 0){
                            if(this.libre(x-1,y))
                                    d2 = Direction.Ouest;
                            else {
                                if(this.libre(x+1,y))
                                    d2 = Direction.Est;
                                else{ 
                                    if(p.getPlateau().getCase(x,y+1).getTypeCase() != TypeCase.EAU)
                                        d2 = Direction.Sud;
                                }
                            }
                        }
                        else 
                            if(x == 0){
                                if(this.libre(x,y+1))
                                        d2 = Direction.Sud;
                                else {
                                    if(this.libre(x,y-1))
                                        d2 = Direction.Nord;
                                    else{ 
                                        if(p.getPlateau().getCase(x+1,y).getTypeCase() != TypeCase.EAU)
                                            d2 = Direction.Est;
                                    }
                                }
                            }            
                            else 
                                if(x == p.getPlateau().getLargeur()-1){
                                    if(this.libre(x,y-1))
                                        d2 = Direction.Nord;
                                    else{
                                        if(this.libre(x,y+1))
                                                    d2 = Direction.Sud;
                                        else {if(p.getPlateau().getCase(x-1,y).getTypeCase() != TypeCase.EAU)
                                            d2 = Direction.Ouest;
                                        }
                                    }
                                }
                                else 
                                    if(y == p.getPlateau().getLongueur()-1){
                                        if(this.libre(x+1,y))
                                                d2 = Direction.Est;
                                        else{
                                            if(this.libre(x-1,y))
                                                    d2 = Direction.Ouest;
                                            else {
                                                if(p.getPlateau().getCase(x,y-1).getTypeCase() != TypeCase.EAU)
                                                    d2 = Direction.Nord;
                                            }
                                        }
                                    }
                                    else{
                                        if(this.libre(x+1,y))
                                            d2 = Direction.Est;
                                        else 
                                            if(this.libre(x,y+1))
                                                d2 = Direction.Sud;
                                            else 
                                                if(this.libre(x-1,y))
                                                    d2 = Direction.Ouest;
                                                else
                                                    if(this.libre(x,y-1))
                                                        d2 = Direction.Nord;
                                                    else {d2 = this.dir[i%4];
                                                    System.out.println("derniere correction");
 
                                                    }
                                        }
        if(d2 == null)d2 = this.dir[i%4];
        this.var = this.getDir(d2);
        //else this.var = (this.getDir(d2)+2) %4;
        p.avancer(d2);
        p.ramasserClou();
        this.deplacement(this.getDir(d2));
 
    }
 
 
 
    /**
     * @param args
     * @throws JeuNonTermine 
     * @throws JeuTermine 
     * @throws JeuNonTermine 
     * @throws JeuTermine 
     * @throws PasDeClou 
     * @throws JeuNonInitialise 
     * @throws DeplacementImpossible 
     * @throws  
     */
    public static void main(String[] args) throws JeuNonTermine, DeplacementImpossible, JeuNonInitialise, PasDeClou, JeuTermine {
 
 
        boolean regle;
        String res;
        regle = true;
        Plateau p = new Plateau(regle);
        Strategie4 s2 = new Strategie4(true,p);
        int i = 1;
        System.out.println("I8 " + i);
        while(true){
 
        try{
 
                s2.deplacement(s2.var);
            //    System.out.println(s2.p.getR().getNbClousRamasses());
                //s2.correction2(p);
        //        i= (int)Math.random()*100;
               // s2.correction(p,Direction.Est,i);
                //++i;
            }
            catch(JeuTermine e){
 
                System.out.println(e);
 
                if(!regle)
                    res = "Nbe clou ramassés = ";
                else
                    res = "Nbe tour de jeu = ";
                System.out.println(res + p.get_resultat());
                break;
 
            }
            catch(PasDeClou e){
                System.out.println(e);
            }
            catch(DeplacementImpossible e){
                //i=i+1;
                //s2.correction2(p);
                System.out.print("main ");
                System.out.println(e);
                //break;
                //s2.boucle();
                //s2.deplacement(p,++i);
 
 
            }
            catch(JeuNonInitialise e){
                System.out.println(e);
            }
    }
    }    
    }
Mais il y a des bugs comme vous avez pu le constater dans les commentaires... Pourriez vous m'éclairer svp ?

merci d'avance...