LXir
21 mars 2004, 10h21
Salut,
J'ai un probleme avec le code suivant qui doit generer un labyrinthe aléatoire (main.c utilise les bibliothèques allegro et mur.bmp est un simple bitmap 20*20 pxls)
main.c :
#include <stdlib.h>
#include "gen_laby.h"
#include <allegro.h>
int laby[40][30],a,b;
BITMAP *mur;
// Fonction main
int main()
{
// Initialisation d'allegro
allegro_init();
// Mise en place du clavier
install_keyboard();
// Mise en place de la souris
if (install_mouse() == -1)
{
allegro_message("Erreur ! %s", allegro_error) ;
return 1 ;
}
// Définition de la profondeur de couleur
set_color_depth(16);
// Mise en place du mode graphique
if (set_gfx_mode(GFX_AUTODETECT, 800, 600, 0, 0) != 0)
{
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Impossible d'initialiser le mode vidéo !\n%s\n", allegro_error);
return 1;
}
mur=load_bitmap("mur.bmp",NULL);
genere_laby(38,28,&laby[0][0],100);
for (b=0;b<28;b++)
for (a=0;a<38;a++)
{
if (laby[a][b]==MUR)
draw_sprite(screen,mur,a*20,b*20);
}
// Boucle principale
while (!key[KEY_ESC])
{
}
return 0;
**********************************
gen_laby.h :
#include <stdlib.h>
#include <mem.h>
#define HAUT 1
#define DROITE 2
#define BAS 3
#define GAUCHE 4
#define VIDE 0
#define MUR 1
// Fonction qui génère le labyrinthe de dimension dim_x, dim_y
// à enregistrer dans le tableau de taille [dim_x][dim_y] vers
// lequel pointe *lab à partir de la clef.
void genere_laby(int dim_x, int dim_y, int *lab, int clef)
{
int init[dim_x][dim_y], laby[dim_x][dim_y], poss[4], nbr_poss, a, b, c;
int x, y, direction;
// Initialisation du generateur de nombres aleatoires
srand(clef);
// Initialisations
memset(poss, 0, 4*sizeof(int) );
memset(init, 0, dim_x*dim_y*sizeof(int) );
memset(laby, MUR, dim_x*dim_y*sizeof(int)) ;
// Premier tunnel
x=1;
y=1;
init[x][y]=1;
laby[x][y]=VIDE;
while (1)
{
nbr_poss=0;
c=0;
if ((y-2>=0)&&(y-2<dim_y))
if (init[x][y-2]==0)
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if (init[x][y+2]==0)
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if (init[x-2][y]==0)
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if (init[x+2][y]==0)
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
if (nbr_poss==0)
break;
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
init[x][y-2]=1;
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
y=y-2;
}
if (poss[direction]==BAS)
{
init[x][y+2]=1;
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
y=y+2;
}
if (poss[direction]==DROITE)
{
init[x+2][y]=1;
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
x=x+2;
}
if (poss[direction]==GAUCHE)
{
init[x-2][y]=1;
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
x=x-2;
}
}
// On creuse le labyrinthe
for (a=0;a<dim_x;a++)
for (b=0;b<dim_y;b++)
if (init[a][b]==0)
{
// Initialisation du tunnel
x=a;
y=b;
init[x][y]=1;
laby[x][y]=VIDE;
nbr_poss=0;
c=0;
// On relie le tunnel au labyrinthe
if ((y-2>=0)&&(y-2<dim_y))
if ((laby[x][y-1]+laby[x][y-2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if ((laby[x][y+1]+laby[x][y+2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if ((laby[x-1][y]+laby[x-2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if ((laby[x+1][y]+laby[x+2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
}
if (poss[direction]==BAS)
{
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
}
if (poss[direction]==DROITE)
{
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
}
if (poss[direction]==GAUCHE)
{
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
}
// On creuse le tunnel
while (1)
{
nbr_poss=0;
c=0;
if ((y-2>=0)&&(y-2<dim_y))
if (init[x][y-2]==0)
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if (init[x][y+2]==0)
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if (init[x-2][y]==0)
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if (init[x+2][y]==0)
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
if (nbr_poss==0)
break;
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
init[x][y-2]=1;
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
y=y-2;
}
if (poss[direction]==BAS)
{
init[x][y+2]=1;
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
y=y+2;
}
if (poss[direction]==DROITE)
{
init[x+2][y]=1;
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
x=x+2;
}
if (poss[direction]==GAUCHE)
{
init[x-2][y]=1;
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
x=x-2;
}
}
// On relie la fin du tunnel au labyrinthe
if ((y-2>=0)&&(y-2<dim_y))
if ((laby[x][y-1]+laby[x][y-2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if ((laby[x][y+1]+laby[x][y+2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if ((laby[x-1][y]+laby[x-2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if ((laby[x+1][y]+laby[x+2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
}
if (poss[direction]==BAS)
{
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
}
if (poss[direction]==DROITE)
{
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
}
if (poss[direction]==GAUCHE)
{
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
}
}
memmove(lab, laby, dim_x*dim_y*sizeof(int) );
return;
}
lors de l'éxecution, windows renvoie une erreur.
Merci de m'aider.
J'ai un probleme avec le code suivant qui doit generer un labyrinthe aléatoire (main.c utilise les bibliothèques allegro et mur.bmp est un simple bitmap 20*20 pxls)
main.c :
#include <stdlib.h>
#include "gen_laby.h"
#include <allegro.h>
int laby[40][30],a,b;
BITMAP *mur;
// Fonction main
int main()
{
// Initialisation d'allegro
allegro_init();
// Mise en place du clavier
install_keyboard();
// Mise en place de la souris
if (install_mouse() == -1)
{
allegro_message("Erreur ! %s", allegro_error) ;
return 1 ;
}
// Définition de la profondeur de couleur
set_color_depth(16);
// Mise en place du mode graphique
if (set_gfx_mode(GFX_AUTODETECT, 800, 600, 0, 0) != 0)
{
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Impossible d'initialiser le mode vidéo !\n%s\n", allegro_error);
return 1;
}
mur=load_bitmap("mur.bmp",NULL);
genere_laby(38,28,&laby[0][0],100);
for (b=0;b<28;b++)
for (a=0;a<38;a++)
{
if (laby[a][b]==MUR)
draw_sprite(screen,mur,a*20,b*20);
}
// Boucle principale
while (!key[KEY_ESC])
{
}
return 0;
**********************************
gen_laby.h :
#include <stdlib.h>
#include <mem.h>
#define HAUT 1
#define DROITE 2
#define BAS 3
#define GAUCHE 4
#define VIDE 0
#define MUR 1
// Fonction qui génère le labyrinthe de dimension dim_x, dim_y
// à enregistrer dans le tableau de taille [dim_x][dim_y] vers
// lequel pointe *lab à partir de la clef.
void genere_laby(int dim_x, int dim_y, int *lab, int clef)
{
int init[dim_x][dim_y], laby[dim_x][dim_y], poss[4], nbr_poss, a, b, c;
int x, y, direction;
// Initialisation du generateur de nombres aleatoires
srand(clef);
// Initialisations
memset(poss, 0, 4*sizeof(int) );
memset(init, 0, dim_x*dim_y*sizeof(int) );
memset(laby, MUR, dim_x*dim_y*sizeof(int)) ;
// Premier tunnel
x=1;
y=1;
init[x][y]=1;
laby[x][y]=VIDE;
while (1)
{
nbr_poss=0;
c=0;
if ((y-2>=0)&&(y-2<dim_y))
if (init[x][y-2]==0)
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if (init[x][y+2]==0)
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if (init[x-2][y]==0)
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if (init[x+2][y]==0)
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
if (nbr_poss==0)
break;
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
init[x][y-2]=1;
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
y=y-2;
}
if (poss[direction]==BAS)
{
init[x][y+2]=1;
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
y=y+2;
}
if (poss[direction]==DROITE)
{
init[x+2][y]=1;
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
x=x+2;
}
if (poss[direction]==GAUCHE)
{
init[x-2][y]=1;
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
x=x-2;
}
}
// On creuse le labyrinthe
for (a=0;a<dim_x;a++)
for (b=0;b<dim_y;b++)
if (init[a][b]==0)
{
// Initialisation du tunnel
x=a;
y=b;
init[x][y]=1;
laby[x][y]=VIDE;
nbr_poss=0;
c=0;
// On relie le tunnel au labyrinthe
if ((y-2>=0)&&(y-2<dim_y))
if ((laby[x][y-1]+laby[x][y-2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if ((laby[x][y+1]+laby[x][y+2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if ((laby[x-1][y]+laby[x-2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if ((laby[x+1][y]+laby[x+2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
}
if (poss[direction]==BAS)
{
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
}
if (poss[direction]==DROITE)
{
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
}
if (poss[direction]==GAUCHE)
{
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
}
// On creuse le tunnel
while (1)
{
nbr_poss=0;
c=0;
if ((y-2>=0)&&(y-2<dim_y))
if (init[x][y-2]==0)
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if (init[x][y+2]==0)
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if (init[x-2][y]==0)
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if (init[x+2][y]==0)
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
if (nbr_poss==0)
break;
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
init[x][y-2]=1;
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
y=y-2;
}
if (poss[direction]==BAS)
{
init[x][y+2]=1;
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
y=y+2;
}
if (poss[direction]==DROITE)
{
init[x+2][y]=1;
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
x=x+2;
}
if (poss[direction]==GAUCHE)
{
init[x-2][y]=1;
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
x=x-2;
}
}
// On relie la fin du tunnel au labyrinthe
if ((y-2>=0)&&(y-2<dim_y))
if ((laby[x][y-1]+laby[x][y-2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=HAUT;
c++;
}
if ((y+2>=0)&&(y+2<dim_y))
if ((laby[x][y+1]+laby[x][y+2])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=BAS;
c++;
}
if ((x-2>=0)&&(x-2<dim_x))
if ((laby[x-1][y]+laby[x-2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=GAUCHE;
c++;
}
if ((x+2>=0)&&(x+2<dim_x))
if ((laby[x+1][y]+laby[x+2][y])==(MUR+VIDE))
{
nbr_poss++;
poss[c]=DROITE;
c++;
}
direction=rand()%nbr_poss;
if (poss[direction]==HAUT)
{
laby[x][y-1]=VIDE;
laby[x][y-2]=VIDE;
}
if (poss[direction]==BAS)
{
laby[x][y+1]=VIDE;
laby[x][y+2]=VIDE;
}
if (poss[direction]==DROITE)
{
laby[x+1][y]=VIDE;
laby[x+2][y]=VIDE;
}
if (poss[direction]==GAUCHE)
{
laby[x-1][y]=VIDE;
laby[x-2][y]=VIDE;
}
}
memmove(lab, laby, dim_x*dim_y*sizeof(int) );
return;
}
lors de l'éxecution, windows renvoie une erreur.
Merci de m'aider.