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

C Discussion :

Génération labyrinthe aléatoire


Sujet :

C

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2020
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2020
    Messages : 4
    Points : 1
    Points
    1
    Par défaut Génération labyrinthe aléatoire
    Je dois faire un programme de génération d'un labyrinthe aléatoire de taille aléatoire dans un premier temps, puis avec des poids (chiffres de 0 à 9) sur chaque cases non murs. Le but est toujours avoir un chemin de généré d'un début jusqu'à une fin(sortie).
    Pourriez-vous m'aider à modifier mon code pour pouvoir trouver un chemin coups sûr ?

    Le code créé est le suivant :

    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
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
     
    int my_rand (void);
    int my_rand_2 (void);
    int my_rand_3 (void);
    int my_rand_4 (void);
    int my_rand_6 (void);
    int my_rand_nbL_1 (int nbL);
    int my_rand_nbC_1 (int nbC);
    int my_rand_nbL (int nbL);
    int my_rand_nbC (int nbC);
     
     
    int main (void)
    {
       int nbL = 0,nbC =0, taille =0,i=0,j=0,v=0,c=0,y=0,bibu=0,bibu_ancien=0;
       int xx=0,yy=0;
     
       int Labyrinthe[1000][1000];
     
       nbL=my_rand();
       nbC=my_rand();
     
     
       printf("nbL : %d \n", nbL);
       printf("nbC : %d \n", nbC);
     
     
       taille = nbL*nbC;
       printf("taille : %d \n", taille);
       printf("\n");
     
     
    /*Remplissage Labyrinthe primitif */
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                Labyrinthe[i][j]= '*' ;
            }
       }
     
    /*Affichage du Labyrinthe primitif*/
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                printf("%c \t",Labyrinthe[i][j]);
            }
            printf("\n");
       }
     
    printf("\n");
    printf("\n");
     
     
     
     
    /* Aleatoire */
     
       for (i=1;i<nbL;i=i+2)
       {
            for (j=1;j<nbC;j=j+2)
    	    {
                if (my_rand_2() == 0)
                { 
                    Labyrinthe[i][j]= '0' ;
                }
            }
       }
     
    /* Affichage Labyrinthe */
     
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                printf("%c \t",Labyrinthe[i][j]);
            }
            printf("\n");
       }
    printf("\n\n");
     
    /* Jonction verticale */
     
       for (i=1;i<nbL;i=i+2)
       {
            for (j=2;j<nbC;j=j+2)
    	    {
                if ((Labyrinthe[i][j-1]=='0') && (Labyrinthe[i][j+1]=='0') /* && (my_rand_3()==0) */ )
                { 
                    Labyrinthe[i][j]= '0' ;
                }
            }
       }
     
     
    /* Jonction horizontale */
     
       for (i=2;i<nbL;i=i+2)
       {
            for (j=1;j<nbC;j=j+2)
    	    {
                if ((Labyrinthe[i-1][j]=='0') && (Labyrinthe[i+1][j]=='0') /* && (my_rand_3()==0) */ )
                { 
                    Labyrinthe[i][j]= '0' ;
                }
            }
       }
     
    /*Recherche et creusement chemin*/
     
     
        i=1;
        j=1;
        while(i < nbL)
        {
            /*printf(" ca rentre i \n");*/
            while(j < nbC)
            {
                /*printf(" ca rentre j \n");*/
                if(Labyrinthe[i][j]=='0')
                {
                    printf("premier 0 detecte\n");
                    printf(" i : %d \n",i);
                    printf(" j : %d \n",j);
                    xx=i;
                    yy=j;
                    j=nbC-1;
                    i=nbL-1;
                }
                j++;
            }
            i++;
        }
     
     
     
    /*
        for (i=0;i<nbL;i=i+1)
        {
            for (j=0;j<nbC;j=j+1)
    	    {
                if(Labyrinthe[i][j]=='0')
                {
                    j=nbC-1;
                    i=nbL-1;
                }
            }
        }
    */
        printf(" xx : %d \n",xx);
        printf(" yy : %d \n",yy);
        for (i=xx;i<nbL;i=i+1)
        {
            /*printf(" ca rentre i 2\n");*/
            for (j=yy;j<nbC;j=j+1)
    	    {
                 /*printf(" ca rentre j 2\n");*/
                 if(Labyrinthe[i][j+1]=='0'){bibu++;}
                 else if ((Labyrinthe[i+1][j]== '0')&&(bibu==bibu_ancien)){bibu++;}
                 else if ((Labyrinthe[i][j-1]== '0')&&(bibu==bibu_ancien)){bibu++;}
                 else if ((Labyrinthe[i-1][j]== '0')&&(bibu==bibu_ancien)){bibu++;}
                 if(bibu==bibu_ancien)
                 {
                       printf("probleme avec bibu\n");
                       Labyrinthe[i][j] = 'T';
                       j=nbC-1 ;
                       i=nbL-1 ;
                 }
                 bibu_ancien==bibu;
            }
        }
        printf("bibu : %d\n",bibu);
     
     
    /*cas D*/
    /*
        v = my_rand_2();
        printf("v : %d \n", v);
        switch (v)
        {
            case 0 :
                    c = my_rand_nbC_1(nbC);
                    printf("c nbC : %d \n",c);
                    for (i=0;i<1;i++)
                    {
                        for (j=0;j<nbC;j++)
    	                {
                            if (j==c)
                            {
                                Labyrinthe[i][j]= 'D' ;
                            }
                        }
                    }
                    break;
            case 1 :
                    c = my_rand_nbL_1(nbL);
                    printf( "c nbL : %d \n", c);
                    for (i=0;i<nbL;i++)
                    {
                        for (j=0;j<1;j++)
    	                {
                            if (i==c)
                            {
                                Labyrinthe[i][j]= 'D' ;
                            }
                        }
                    }
                    break;
        }
    */
     
    /*cas F */
    /*
        y = my_rand_2();
        printf("y : %d \n", y);
        switch (y)
        {
            case 0 :
                    c = my_rand_nbC(nbC);
                    printf("c nbC : %d \n",c);
                    for (i=(nbL-1);i>(nbL-2);i--)
                    {
                            for (j=0;j<nbC;j++)
    	                    {
                                if (j==c)
                                {
                                    Labyrinthe[i][j]= 'F' ;
                                }
                            }
                    }
                    break;
            case 1 :
                    c = my_rand_nbL(nbL);
                    printf( "c nbL : %d \n", c);
                    for (i=0;i<nbL;i++)
                    {
                            for (j=(nbC-1);j>(nbC-2);j--)
    	                    {
                                if (i==c)
                                {
                                    Labyrinthe[i][j]= 'F' ;
                                }
                            }
                    }
                    break;
        }
    */
        fflush(stdout);
     
    /* Remplissage du chemin */
    /*
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                if ((my_rand_3()==0) && (Labyrinthe [i][j] != 'D') && (Labyrinthe  [i][j] != 'F'))
                {
                    Labyrinthe[i][j]='0';
                }
                else if ((my_rand_3() != 0) && (Labyrinthe[i-1][j]==0) && (Labyrinthe[i][j-1]!=0)&& (Labyrinthe [i][j] != 'D') && (Labyrinthe  [i][j] != 'F'))
                {
                    Labyrinthe[i][j]='0';
                }
                else if ((my_rand_3() != 0) && (Labyrinthe[i][j-1]==0) && (Labyrinthe[i-1][j]!=0)&& (Labyrinthe [i][j] != 'D') && (Labyrinthe  [i][j] != 'F'))
                {
                    Labyrinthe[i][j]='0';
                }
                else if (((Labyrinthe [i-1][j] == 'D') || (Labyrinthe [i][j-1] == 'D')) && (Labyrinthe  [i][j] != 'F'))
                {
                    Labyrinthe[i][j]='0';
                }
                else if ((my_rand_3()!=0) && (Labyrinthe[i][j] != '0') && (Labyrinthe [i][j] != 'D') && (Labyrinthe  [i][j] != 'F'))
                {
                    Labyrinthe[i][j]= '*' ;
                }
            }
       }
    */
     
     
    /* Affichage Labyrinthe */
     
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                printf("%c \t",Labyrinthe[i][j]);
            }
            printf("\n");
       }
     
     
       return (EXIT_SUCCESS);
    }
    /*Fin de la fonction main */
     
    /* Les fonctions a parts */  
     
    int my_rand (void)
    {
       static int first = 0;
       int yu=0,pair = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       do{
            yu = rand ()% 9 + 6;
            pair=yu%2;
       }while(pair!=1);
       return (yu);
    }
     
    int my_rand_2 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 2);
    }
     
     
    int my_rand_3 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 3);
    }
     
    int my_rand_4 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 4);
    }
     
    int my_rand_6 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 6);
    }
     
     
    int my_rand_nbL_1 (int nbL)
    {
       static int first = 0;
       int tau1=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       tau1 = rand ()% (nbL-1);
       return (tau1);
    }
     
    int my_rand_nbC_1 (int nbC)
    {
       static int first = 0;
       int tau2=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       tau2 = rand ()% (nbC-1);
       return (tau2);
    }
     
    int my_rand_nbL (int nbL)
    {
       static int first = 0;
       int tau1=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       tau1 = rand ()% nbL;
       return (tau1);
    }
     
    int my_rand_nbC (int nbC)
    {
       static int first = 0;
       int tau2=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       tau2 = rand ()% nbC;
       return (tau2);
    }

  2. #2
    Membre éclairé Avatar de Bayard
    Inscrit en
    Juin 2002
    Messages
    859
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 859
    Points : 714
    Points
    714
    Par défaut
    Bonsoir,

    Pouvez-vous me dire ce que fait ce code, ligne 174 ?

    Est-ce que vous avez-voulu coder ?


    Cordialement
    Si tu ne vis pas ce que tu penses alors tu penses ce que tu vis.

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2020
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2020
    Messages : 4
    Points : 1
    Points
    1
    Par défaut Rep :Bayard
    Oui c est exact dsl.

    En revanche pour m'aider je suis tout ouïe/vue à des explications/exemple sur une matrice fixe, et pas forcément en utilisant un tableau statique.

    Par ailleurs les 'cas D' et 'cas F' sont utilisable (même s'ils sont parfois faux ou incohérents en résultats à l'écran).

  4. #4
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 689
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 689
    Points : 30 983
    Points
    30 983
    Billets dans le blog
    1
    Par défaut
    Bonjour
    Citation Envoyé par UjanB Voir le message
    Pourriez-vous m'aider à modifier mon code pour pouvoir trouver un chemin coups sûr ?
    https://fr.wikipedia.org/wiki/Mod%C3..._de_labyrinthe
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  5. #5
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2020
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2020
    Messages : 4
    Points : 1
    Points
    1
    Par défaut
    Merci

  6. #6
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 689
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 689
    Points : 30 983
    Points
    30 983
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par UjanB
    Je me posais alors la question de savoir s'il était juste possible que vous résolviez un petit bug sur mon programme (manquant d'être mieux structuré).

    Aussi, je me demander comment gère-t-on les tableaux dynamiques en deux dimensions ? Autrement dit comment doit-je adapter mon programme si je veux travailler avec un tableau dynamique.

    Par ailleurs, je me demandais comment je pourrais faire en utilisant des listes chaînées.

    (Je me situe plus en tant que débutant qu'expert confirmé mais je suis très curieux par ce langage de programmation)

    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
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
     
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
     
     
    int my_rand (void);
    int my_rand_2 (void);
    int my_rand_3 (void);
    int my_rand_4 (void);
    int my_rand_6 (void);
    int my_rand_10 (void);
    int my_rand_nbL_1 (int nbL);
    int my_rand_nbC_1 (int nbC);
    int my_rand_nbL (int nbL);
    int my_rand_nbC (int nbC);
     
     
    int main (void)
    {
       int nbL = 0,nbC = 0,nbL_anc=0,nbC_anc=0, taille =0,taille_corrige = 0,i=0,j=0,cpt=2,cptNbZones=0,val=0,x=0,y=0,m=0,n=0,c=0,cptD=0,cptF=0,yuu=0,cmpt=0,rr=0,compteur_global=0;
     
       int Labyrinthe[1000][1000];
     
       srand(time(NULL));
     
       do{
       nbL=my_rand();
       nbC=my_rand();
     
     
       printf("nbL : %d \n", nbL);
       printf("nbC : %d \n", nbC);
     
     
       taille = nbL*nbC;
       printf("taille : %d \n", taille);
       printf("\n");
     
       if(nbL%2==0)
       {
            nbL_anc=nbL;
            nbL=nbL-1;    
       }
       else if(nbC%2==0)
       {
            nbC_anc=nbC;
            nbC=nbC-1;
       }
       taille_corrige = nbL*nbC;
     
     
    /*Remplissage Labyrinthe primitif */
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                Labyrinthe[i][j]= 1 ;
            }
       }
     
    /*Affichage du Labyrinthe primitif*/
       printf(" Laby primitif :  \n");
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                printf("%d \t",Labyrinthe[i][j]);
            }
            printf("\n");
       }
     
    printf("\n");
    printf("\n");
     
    /* Algo creation labyrinthe aleatoire*/
    /* Remplissage des nombres sur chaques cases de 2 en 2*/
     
     
        for(i=1;i<nbL-1;i+=2)
        {
            for(j=1;j<nbC-1;j+=2)
            {
                Labyrinthe[i][j]=cpt;
                cpt++;
                cptNbZones++;
            }
        }
        printf("nb de Zones = %d \n", cptNbZones);
     
     
    /*Jonction horizontale ou verticale */
     
        while(cptNbZones>1)
        {
            i = rand ()%(nbL-2) + 1;
            j = rand ()%(nbC-2) + 1;
            printf("i = %d \n", i);
            printf("j = %d \n", j);
            /* Suite ...*/
            if (((i%2 ==0) && (j%2 ==0)) || ((i%2 !=0) && (j%2 !=0)))
            {
                printf("cul de sac \n");
            }
            else if((i%2 ==0) && (j%2 !=0)) /*Jonction verticale*/
            {
                printf("jonction verticale\n");
                if(Labyrinthe[i+1][j]!=Labyrinthe[i-1][j])
                {
                    val=Labyrinthe[i+1][j];
                    Labyrinthe[i][j]=Labyrinthe[i-1][j];
                    Labyrinthe[i+1][j]=Labyrinthe[i][j];
                    cptNbZones--;
                    for(x=0;x<nbL;x+=1)
                    {
                        for(y=0;y<nbC;y+=1)
                        {
                            if(Labyrinthe[x][y]==val)
                            {    
                                Labyrinthe[x][y]=Labyrinthe[i][j];
                            }
                        }
                    }
                }
            }
     
            if((i%2 !=0) && (j%2 ==0)) /*Jonction horizontale*/
            {
                printf("jonction horizontale\n");
                if(Labyrinthe[i][j+1]!=Labyrinthe[i][j-1])
                {
                    val=Labyrinthe[i][j+1];
                    Labyrinthe[i][j]=Labyrinthe[i][j-1];
                    Labyrinthe[i][j+1]=Labyrinthe[i][j];
                    cptNbZones--;
                    for(x=0;x<nbL;x+=1)
                    {
                        for(y=0;y<nbC;y+=1)
                        {
                            if(Labyrinthe[x][y]==val)
                            {  
                                Labyrinthe[x][y]=Labyrinthe[i][j];
                            }
                        }
                    }
                }
                printf("nb de Zones = %d \n", cptNbZones);
            } 
     
     
     
            /*Affichage Labyrinthe*/
            for (i=0;i<nbL;i++)
            {
                for (j=0;j<nbC;j++)
    	        {
                   printf("%d \t",Labyrinthe[i][j]);
                }
                printf("\n");
            }
     
        }
     
     
    /*Laby sans poids*/
    /* Tableau du labyrinthe qui passe caractere */
        for (i=0;i<nbL;i++)
        {
            for (j=0;j<nbC;j++)
    	    {
                if(Labyrinthe[i][j]==1)
                {
                    Labyrinthe[i][j]='*';
                }
                else
                {
                    Labyrinthe[i][j]='0';
                }
            }
        }
     
     
    /*cas D*/
     
        m = my_rand_2();
        printf("m : %d \n", m);
        switch (m)
        {
            case 0 :
                    printf("case D horizontale \n");
                    do{
                        c = my_rand_nbC_1(nbC);
                        printf("c nbC : %d \n",c);
                        for (i=0;i<1;i++)
                        {
                            for (j=1;j<nbC;j+=2)
    	                    {
                                if ((j==c) && (Labyrinthe[i+1][j]=='0'))
                                {
                                    Labyrinthe[i][j]= 'D' ;
                                    cptD++;
                                }
                            }
                        }   
                    }
                    while(cptD<1);
                    break;
     
            case 1 :
                    printf("case D verticale \n");
                    do{
                        c = my_rand_nbL_1(nbL);
                        printf( "c nbL : %d \n", c);
                        for (i=1;i<nbL;i+=2)
                        {   
                            for (j=0;j<1;j++)
    	                    {
                                if ((i==c) && (Labyrinthe[i][j+1]=='0'))
                                {
                                    Labyrinthe[i][j]= 'D' ;
                                    cptD++;
                                }
                            }
                        }
                    }
                    while(cptD<1);
                    break;
            default :
                    break;
        }
     
     
    /*cas F */
     
        n = my_rand_2();
        printf("n : %d \n", n);
        switch (n)
        {
            case 0 :
                    printf("case F horizontale \n");
                    do{
                        c = my_rand_nbC(nbC);
                        printf("c nbC : %d \n",c);
                        for (i=(nbL-1);i>(nbL-2);i--)
                        {
                            for (j=1;j<nbC;j+=2)
    	                    {
                                if ((j==c) && (Labyrinthe[i-1][j]=='0'))
                                {
                                    Labyrinthe[i][j]= 'F' ;
                                    cptF++;
                                }
                            }
                        }
                    }
                    while(cptF<1);
                    break;
            case 1 :
                    printf("case F verticale \n");
                    do{
                        c = my_rand_nbL(nbL);
                        printf( "c nbL : %d \n", c);
                        for (i=1;i<nbL;i+=2)
                        {
                            for (j=(nbC-1);j>(nbC-2);j--)
    	                    {
                                if ((i==c) && (Labyrinthe[i][j-1]=='0'))
                                {
                                    Labyrinthe[i][j]= 'F' ;
                                    cptF++;
                                }
                            }
                        }
                    }
                    while(cptF<1);
                    break;
            default :
                    break;
        }
     
    /* Ca c est pour la verif du Laby : */
     
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                if(Labyrinthe[i][j]=='0')
                {
                    compteur_global++;
                }
            }
       }  
     
     
     
    /* Affichage Labyrinthe sans poids */
     
       printf("\n");
       printf("Affichage final sans poids\n");
       printf("\n");
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                printf("%c \t",Labyrinthe[i][j]);
            }
            printf("\n");
       }
     
    /*Laby avec poids*/
    /*Affectation des poids et on met les etoiles */
     
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                /*
                if(Labyrinthe[i][j]==1)
                {
                    Labyrinthe[i][j]== 42 ;
                }
                */
                if(Labyrinthe[i][j]=='D')
                {
                    Labyrinthe[i][j]== 68 ;
                }
                else if(Labyrinthe[i][j]=='F')
                {
                    Labyrinthe[i][j]== 70 ;
                }
                else if(Labyrinthe[i][j] != '*')
                {
                    /*printf("ca rentre\n");*/
                    do{
                    yuu= rand()% 10;
                    /* printf("yuu : %d \n",yuu);*/
                    Labyrinthe[i][j]= 48 + yuu;
                    cmpt++;
                    }while(cmpt<1);
                }
            }
       }
     
    /* Affichage Labyrinthe avec poids */
     
       printf("\n");
       printf("Affichage final avec poids\n");
       printf("\n");
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                printf("%c \t", Labyrinthe[i][j]);
            }
            printf("\n");
       }   
     
        fflush(stdout); 
     
       }while(compteur_global >= taille_corrige -2);
     
     
       return (EXIT_SUCCESS);
    }
    /*Fin de la fonction main */
     
    /* Les fonctions a parts */  
     
    int my_rand (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 12 + 5);
    }
     
    int my_rand_2 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 2);
    }
     
     
    int my_rand_3 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 3);
    }
     
    int my_rand_4 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 4);
    }
     
    int my_rand_6 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 6);
    }
     
    /*
    int my_rand_10 (void)
    {
       static int first = 0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       return (rand ()% 10);
    }
    */
     
    int my_rand_nbL_1 (int nbL)
    {
       static int first = 0;
       int tau1=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       tau1 = rand ()% (nbL-1);
       return (tau1);
    }
     
    int my_rand_nbC_1 (int nbC)
    {
       static int first = 0;
       int tau2=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       tau2 = rand ()% (nbC-1);
       return (tau2);
    }
     
    int my_rand_nbL (int nbL)
    {
       static int first = 0;
       int tau1=0,pair=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       do{
            tau1 = rand ()% nbL;
            pair=tau1%2;
       }while(pair!=1);
       return (tau1);
    }
     
    int my_rand_nbC (int nbC)
    {
       static int first = 0;
       int tau2=0,pair=0;
       if (first == 0)
       {
          srand (time (NULL));
          first = 1;
       }
       do{
            tau2 = rand ()% nbC;
            pair=tau2%2;
       }while(pair!=1);
       return (tau2);
    }
     
     
    /*
    void afficheLaby(int Labyrinthe[nbL][nbC])
    {
       int i,j;
       for (i=0;i<nbL;i++)
       {
            for (j=0;j<nbC;j++)
    	    {
                printf("%d \t",Labyrinthe[i][j]);
            }
            printf("\n");
       }
    }
    */
    Alors t'as l'air bien sympa mais le principe d'un forum c'est de poster en public et pas en privé. Déjà parce que t'as plus de chances d'avoir des réponses variées qui se complèteront, parce que tes soucis et leurs résolutions peuvent aider d'autres intervenants et parce que la personne en MP n'est pas forcément disponible.

    Sinon beaucoup de questions qui méritent chacune un chapitre dans un livre de programmation (les listes chainées surtout). Donc concentrons-nous déjà sur ce code (apprendre à marcher avant de vouloir voler)
    Déjà t'as pas compris le principe du srand(time(NULL)) car tu en mets de partout. Or justement non. La fonction srand() a pour but d'initialiser le moteur du rendu aléatoire donc on ne l'appelle qu'une seule fois pour "INITialiser" (généralement dans le main). Ainsi ça t'évitera tes astuces à coup de static. Astuces pas mal pensées (au-moins tu as pigé ce qu'est une static) mais complètement inutiles. Et accessoirement ça permet aussi de tester un programme dans certains cas particuliers (quand par exemple on veut des nombres aléatoires mais toujours les mêmes on donne alors une graine fixe à srand() qui produira alors la même suite).

    Ensuite c'est quoi toutes ces fonctions my_rand_X dans tous les sens qui sont toutes les mêmes ? Fondamentalement c'est quoi la différence entre my_rand_nbL et my_rand_nbC ???
    Le principe d'une fonction est d'être indépendante de son environnement. Une fonction "my_rand" génèrera donc un nombre aléatoire à chaque appel et ensuite c'est l'appelant qui affectera ce nombre à une ligne ou une colonne. Et pareil entre my_rand_nbL et my_rand_nbL_1. Tu n'as pas l'impression que appeler my_rand_nbL_1(10) et appeler my_rand_nbL(9) c'est exactement la même chose ?

    C'est bien de faire des fonctions (ça découpe les problèmes) mais faut quand-même les faire assez synthétiques pour pouvoir les utiliser de façon variée (ce qui est le rôle des paramètres qu'on leur passe). Parce que si tu écris autant de fonction qu'il y a de cas d'appel possible c'est que tu as raté une étape dans leurs compréhension. Donc tu écris une seule et unique fonction my_rand() qui recevra 2 paramètres "a" et "b" et qui renvoie un nombre aléatoire entre "a" et "b" et ensuite tu l'appelles dans différentes configurations en lui passant à chaque fois les valeurs que tu veux pour obtenir le nombre aléatoire dont tu as besoin.

    Pour les tableaux 2D dynamiques faudra que tu commences déjà par assimiler les pointeurs. Parce que ça se passe à coup de pointeur sur pointeur. Il faut d'abord allouer (malloc) la première dimension (les lignes) puis, ensuite, pour chaque ligne, allouer les colonnes. Avec test de chaque allocation et programmation d'une routine subsidiaire si un seul échoue (généralement on libère ce qui a été alloué et on arrête). Ainsi imagine un tableau 20*50, tu alloues d'abord 20 puis pour chaque ligne[0] jusqu'à ligne[19] tu alloues 50. Sauf que ça plante à ligne[18] !!!???!!! Faut alors tout libérer ce qui a été alloué inutilement.
    Donc devant ce genre de programmation assez lourde, bien souvent, on travaille en 1D. On alloue donc (pour cet exemple) 1000 d'un coup (une seule allocation à vérifier) puis ensuite on travaille dans l'algo en 2D mais en mémoire en 1D (transformation des coordonnées 2D en coordonnées 1D et inversement).

    Et si tu veux voir les listes chainées, tu as un exemple complet ici.
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  7. #7
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2020
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 24
    Localisation : France, Morbihan (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2020
    Messages : 4
    Points : 1
    Points
    1
    Par défaut
    Merci

Discussions similaires

  1. Génération de labyrinthe aléatoire en Javascript
    Par Ghost_47 dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 14/04/2015, 04h37
  2. [Math / Random] Génération nombre aléatoire compris entre deux bornes
    Par oligig dans le forum Collection et Stream
    Réponses: 5
    Dernier message: 30/06/2013, 17h23
  3. Génération nombre aléatoire
    Par pixel016 dans le forum WinDev
    Réponses: 12
    Dernier message: 15/01/2011, 15h41
  4. Problème génération nombre aléatoires
    Par Pim'Ouss dans le forum Général Java
    Réponses: 3
    Dernier message: 15/10/2010, 12h25
  5. Génération texte aléatoire
    Par MrEddy dans le forum Langage
    Réponses: 3
    Dernier message: 03/01/2006, 11h47

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