IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Problème génération et résolution labyrinthe


Sujet :

Langage Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2017
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2017
    Messages : 16
    Par défaut Problème génération et résolution labyrinthe
    Bonjour, j'essayes de générer et résoudre un labyrinthe avec ce code mais lorsque que je le lance j'ai ce message d'erreur : Exception in thread "main"
    java.lang.ArrayIndexOutOfBoundsException: 0
    at Labyrinthe.Labyrinth.main(Labyrinth.java:172)
    C:\Users\Jean-Baptiste\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)
    Voici mon code:
    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
    package Labyrinthe;
     
    import Labyrinthe.StdDraw;
     
    public class Labyrinth {
        private int n;                 // dimension of maze
        private boolean[][] north;     // is there a wall to north of cell i, j
        private boolean[][] east;
        private boolean[][] south;
        private boolean[][] west;
        private boolean[][] visited;
        private boolean done = false;
     
        public Labyrinth(int n) {
            this.n = n;
            StdDraw.setXscale(0, n+2);
            StdDraw.setYscale(0, n+2);
            init();
            generate();
        }
     
        private void init() {
            // initialize border cells as already visited
            visited = new boolean[n+2][n+2];
            for (int x = 0; x < n+2; x++) {
                visited[x][0] = true;
                visited[x][n+1] = true;
            }
            for (int y = 0; y < n+2; y++) {
                visited[0][y] = true;
                visited[n+1][y] = true;
            }
     
     
            // initialze all walls as present
            north = new boolean[n+2][n+2];
            east  = new boolean[n+2][n+2];
            south = new boolean[n+2][n+2];
            west  = new boolean[n+2][n+2];
            for (int x = 0; x < n+2; x++) {
                for (int y = 0; y < n+2; y++) {
                    north[x][y] = true;
                    east[x][y]  = true;
                    south[x][y] = true;
                    west[x][y]  = true;
                }
            }
        }
     
     
        // generate the maze
        private void generate(int x, int y) {
            visited[x][y] = true;
     
            // while there is an unvisited neighbor
            while (!visited[x][y+1] || !visited[x+1][y] || !visited[x][y-1] || !visited[x-1][y]) {
     
                // pick random neighbor 
                while (true) {
                    double r = StdRandom.uniform(4);
                    if (r == 0 && !visited[x][y+1]) {
                        north[x][y] = false;
                        south[x][y+1] = false;
                        generate(x, y + 1);
                        break;
                    }
                    else if (r == 1 && !visited[x+1][y]) {
                        east[x][y] = false;
                        west[x+1][y] = false;
                        generate(x+1, y);
                        break;
                    }
                    else if (r == 2 && !visited[x][y-1]) {
                        south[x][y] = false;
                        north[x][y-1] = false;
                        generate(x, y-1);
                        break;
                    }
                    else if (r == 3 && !visited[x-1][y]) {
                        west[x][y] = false;
                        east[x-1][y] = false;
                        generate(x-1, y);
                        break;
                    }
                }
            }
        }
     
        // generate the maze starting from lower left
        private void generate() {
            generate(1, 1);
     
    /*
            // delete some random walls
            for (int i = 0; i < n; i++) {
                int x = 1 + StdRandom.uniform(n-1);
                int y = 1 + StdRandom.uniform(n-1);
                north[x][y] = south[x][y+1] = false;
            }
     
            // add some random walls
            for (int i = 0; i < 10; i++) {
                int x = n/2 + StdRandom.uniform(n/2);
                int y = n/2 + StdRandom.uniform(n/2);
                east[x][y] = west[x+1][y] = true;
            }
    */
     
        }
     
     
     
        // solve the maze using depth-first search
        private void solve(int x, int y) {
            if (x == 0 || y == 0 || x == n+1 || y == n+1) return;
            if (done || visited[x][y]) return;
            visited[x][y] = true;
     
            StdDraw.setPenColor(StdDraw.BLUE);
            StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
            StdDraw.show();
            StdDraw.pause(30);
     
            // reached middle
            if (x == n/2 && y == n/2) done = true;
     
            if (!north[x][y]) solve(x, y + 1);
            if (!east[x][y])  solve(x + 1, y);
            if (!south[x][y]) solve(x, y - 1);
            if (!west[x][y])  solve(x - 1, y);
     
            if (done) return;
     
            StdDraw.setPenColor(StdDraw.GRAY);
            StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
            StdDraw.show();
            StdDraw.pause(30);
        }
     
        // solve the maze starting from the start state
        public void solve() {
            for (int x = 1; x <= n; x++)
                for (int y = 1; y <= n; y++)
                    visited[x][y] = false;
            done = false;
            solve(1, 1);
        }
     
        // draw the maze
        public void draw() {
            StdDraw.setPenColor(StdDraw.RED);
            StdDraw.filledCircle(n/2.0 + 0.5, n/2.0 + 0.5, 0.375);
            StdDraw.filledCircle(1.5, 1.5, 0.375);
     
            StdDraw.setPenColor(StdDraw.BLACK);
            for (int x = 1; x <= n; x++) {
                for (int y = 1; y <= n; y++) {
                    if (south[x][y]) StdDraw.line(x, y, x+1, y);
                    if (north[x][y]) StdDraw.line(x, y+1, x+1, y+1);
                    if (west[x][y])  StdDraw.line(x, y, x, y+1);
                    if (east[x][y])  StdDraw.line(x+1, y, x+1, y+1);
                }
            }
            StdDraw.show();
            StdDraw.pause(1000);
        }
     
     
     
        // a test client
        public static void main(String[] args) {
            int n = Integer.parseInt(args[0]);
            Labyrinth Labyrinthe = new Labyrinth(n);
            StdDraw.enableDoubleBuffering();
            Labyrinthe.draw();
            Labyrinthe.solve();
        }
     
    }

  2. #2
    Membre chevronné Avatar de Drowan
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2014
    Messages
    460
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2014
    Messages : 460
    Par défaut
    Cette erreur t'indique que tu essaye d'accéder à un emplacement qui n'existe pas.

    Par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    int tab[] = {0,1} //déclaratio et initialisation d'un tableau de taille 2
    int  a = tab[2] //Erreur la tableu ne contient que 2 valeurs tab[0] et tab[1]

  3. #3
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2017
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2017
    Messages : 16
    Par défaut
    Citation Envoyé par Drowan Voir le message
    Cette erreur t'indique que tu essaye d'accéder à un emplacement qui n'existe pas.

    Par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    int tab[] = {0,1} //déclaratio et initialisation d'un tableau de taille 2
    int  a = tab[2] //Erreur la tableu ne contient que 2 valeurs tab[0] et tab[1]
    Merci pour ton aide. Je suppose que c'est généré à cause de la ligne :

    int n = Integer.parseInt(args[0]);

    mais honnetement je ne vois pas ce que je dois mettre à la place de args[0]

  4. #4
    Membre chevronné Avatar de Drowan
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2014
    Messages
    460
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2014
    Messages : 460
    Par défaut
    args est le tableau des arguments passés au programme lors de son lancement. Si args[0] te génère cette erreur c'est que ce tableau est vide. Donc que tu ne passe pas d'argument à ton programme.

  5. #5
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2017
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2017
    Messages : 16
    Par défaut
    la solution est de mettre un chiffre à la place de args[0] par exemple :

    int n = 100;
    Maze maze = new Maze(n);
    StdDraw.enableDoubleBuffering();
    maze.draw();
    maze.solve();

    Merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème Génération fichier texte
    Par veenie dans le forum Oracle
    Réponses: 5
    Dernier message: 18/05/2006, 11h21
  2. [jsp - jasperreport] - problème génération rapport
    Par karibouxe dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 16/05/2006, 18h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo