Accueil > > > ÉCRITURE DE LABYRINTHE
ÉCRITURE DE LABYRINTHE
Information sur la source
Description
un simple code qui créé un bmp représentant un labyrinthe récursif. libre à vous ensuite de créer le moteur 3d pour vous déplacer dedans (je l'ai fait, c'est trop chouette !!). il faut juste spécifier en dur dans le code la largeur et la hauteur du labyrinthe désiré et hop ! le labyrinthe créé n'a qu'une seule sortie attention à ne pas spécifier des tailles trop importantes, votre pile a toutes les chances d'exploser et votre mémoire a toutes le chances d'être pleine ! mais en dessous de 4 millions de pixels, aucun problème. la capture d'écran est en jpg alors excusez le mauvais rendu, ce format n'aime pas vraiment les fortes variations de couleur. le capture d'écran représente le labyrinthe 128*128, il est bien assez compliqué.
Source
- #include <stdio.h>
- #include <stdlib.h>
-
- #define CTOI(C) (*(int*)&C)
- #define ABS(n) (n<=0)?-n:n
-
-
- int complement(unsigned int nb)
- {
- switch(nb%4)
- {
- case 0 : return 0;
- default : return 4-nb%4;
- }
- }
-
- void set(unsigned char *data,
- unsigned long i,
- unsigned long j,
- unsigned char r,
- unsigned char g,
- unsigned char b,
- unsigned long ligne_size)
- {
- data[j*ligne_size+i*3]=b;
- data[j*ligne_size+i*3+1]=g;
- data[j*ligne_size+i*3+2]=r;
- }
-
-
- void draw_line(unsigned char *data, // les deux points doivent être sur une même "ligne"
- unsigned long x1, // et dessine un couloir
- unsigned long y1,
- unsigned long x2,
- unsigned long y2,
- unsigned long ligne_size)
- {
- //printf("%d %d %d %d\n",x1,y1,x2,y2);
- unsigned long i;
- if (x1==x2)
- {
- if (y1>y2)
- for (i=y2; i<y1; i++)
- set(data,x1,i,1,67,188,ligne_size);
- else
- for (i=y1; i<y2; i++)
- set(data,x1,i,1,67,188,ligne_size);
- }
- else // y1==y2
- {
- if (x1>x2)
- for (i=x2; i<x1; i++)
- set(data,i,y1,1,67,188,ligne_size);
- else
- for (i=x1; i<x2; i++)
- set(data,i,y1,1,67,188,ligne_size);
- }
- }
-
-
- void draw_labyrinthe(unsigned char *data,
- unsigned long departx,
- unsigned long departy,
- unsigned long coin1x,
- unsigned long coin1y,
- unsigned long coin2x,
- unsigned long coin2y,
- unsigned long ligne_size)
- {
- // condition d'arrêt
- if ((ABS(coin1x-coin2x)>3) && (ABS(coin1y-coin2y)>3))
- {
- // on se déplace au milieu de la zone
- draw_line(data, departx, departy, (int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2),ligne_size);
- if (departy==(int)((coin1y+coin2y)/2))
- {
- draw_labyrinthe(data,(int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), coin1x, coin1y, coin2x, (int)((coin1y+coin2y)/2), ligne_size); //bas
- draw_labyrinthe(data,(int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), coin1x, (int)((coin1y+coin2y)/2), coin2x, coin2y, ligne_size); // haut
- }
- else
- {
- draw_labyrinthe(data, (int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), coin1x, coin1y, (int)((coin1x+coin2x)/2), coin2y, ligne_size); //gauche
- draw_labyrinthe(data, (int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), (int)((coin1x+coin2x)/2), coin1y, coin2x, coin2y, ligne_size); //droite
- }
- }
- }
-
- put_exit(unsigned char *data, unsigned long largeur, unsigned long hauteur,unsigned long ligne_size)
- {
- unsigned long x=rand()%largeur;
- while ((data[(hauteur-2)*ligne_size+x*3]!=1) &&
- (data[(hauteur-2)*ligne_size+x*3+1]!=67) &&
- (data[(hauteur-2)*ligne_size+x*3+2]!=188))
- x++;
- //printf("%d %d", x, hauteur-1);
- set(data, x, hauteur-1, 1,67,188, ligne_size);
- }
-
- int main(int argc, char **argv)
- {
- unsigned int largeur=2000, hauteur=2000;
-
- FILE *fichier=fopen("labyrinthe.bmp", "w");
- unsigned char header[54]={0};
- unsigned long ligne_size=largeur*3+complement(largeur*3);
- unsigned long datasize=hauteur* ligne_size;
- //printf("%ld", datasize);
-
- header[0]='B';
- header[1]='M';
- CTOI(header[2])=54+datasize;
- CTOI(header[10])=54;
- CTOI(header[14])=40;
- CTOI(header[18])=largeur;
- CTOI(header[22])=hauteur;
- CTOI(header[26])=1;
- CTOI(header[28])=24;
- CTOI(header[34])=datasize;
- fwrite(&header,1,54,fichier);
-
- char *data=(char*)calloc(datasize,1);
- if (data==NULL) return 1;
-
- /* on colorie tout en 2,150,20*/
- unsigned int i,j;
- for (j=0; j<hauteur; j++)
- for (i=0; i<largeur; i++)
- set(data, i,j,2,150,20, ligne_size);
-
- srand(2);
- draw_labyrinthe(data, (int)largeur/2, 1, 0, hauteur, largeur, 1, ligne_size);
-
- set(data, (int)largeur/2, 1, 1,100,188, ligne_size);
- put_exit(data,largeur,hauteur, ligne_size);
-
- fwrite(data, 1, largeur*hauteur*3, fichier);
- free(data);
-
- fclose(fichier);
- return 0;
- }
#include <stdio.h>
#include <stdlib.h>
#define CTOI(C) (*(int*)&C)
#define ABS(n) (n<=0)?-n:n
int complement(unsigned int nb)
{
switch(nb%4)
{
case 0 : return 0;
default : return 4-nb%4;
}
}
void set(unsigned char *data,
unsigned long i,
unsigned long j,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned long ligne_size)
{
data[j*ligne_size+i*3]=b;
data[j*ligne_size+i*3+1]=g;
data[j*ligne_size+i*3+2]=r;
}
void draw_line(unsigned char *data, // les deux points doivent être sur une même "ligne"
unsigned long x1, // et dessine un couloir
unsigned long y1,
unsigned long x2,
unsigned long y2,
unsigned long ligne_size)
{
//printf("%d %d %d %d\n",x1,y1,x2,y2);
unsigned long i;
if (x1==x2)
{
if (y1>y2)
for (i=y2; i<y1; i++)
set(data,x1,i,1,67,188,ligne_size);
else
for (i=y1; i<y2; i++)
set(data,x1,i,1,67,188,ligne_size);
}
else // y1==y2
{
if (x1>x2)
for (i=x2; i<x1; i++)
set(data,i,y1,1,67,188,ligne_size);
else
for (i=x1; i<x2; i++)
set(data,i,y1,1,67,188,ligne_size);
}
}
void draw_labyrinthe(unsigned char *data,
unsigned long departx,
unsigned long departy,
unsigned long coin1x,
unsigned long coin1y,
unsigned long coin2x,
unsigned long coin2y,
unsigned long ligne_size)
{
// condition d'arrêt
if ((ABS(coin1x-coin2x)>3) && (ABS(coin1y-coin2y)>3))
{
// on se déplace au milieu de la zone
draw_line(data, departx, departy, (int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2),ligne_size);
if (departy==(int)((coin1y+coin2y)/2))
{
draw_labyrinthe(data,(int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), coin1x, coin1y, coin2x, (int)((coin1y+coin2y)/2), ligne_size); //bas
draw_labyrinthe(data,(int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), coin1x, (int)((coin1y+coin2y)/2), coin2x, coin2y, ligne_size); // haut
}
else
{
draw_labyrinthe(data, (int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), coin1x, coin1y, (int)((coin1x+coin2x)/2), coin2y, ligne_size); //gauche
draw_labyrinthe(data, (int)((coin1x+coin2x)/2), (int)((coin1y+coin2y)/2), (int)((coin1x+coin2x)/2), coin1y, coin2x, coin2y, ligne_size); //droite
}
}
}
put_exit(unsigned char *data, unsigned long largeur, unsigned long hauteur,unsigned long ligne_size)
{
unsigned long x=rand()%largeur;
while ((data[(hauteur-2)*ligne_size+x*3]!=1) &&
(data[(hauteur-2)*ligne_size+x*3+1]!=67) &&
(data[(hauteur-2)*ligne_size+x*3+2]!=188))
x++;
//printf("%d %d", x, hauteur-1);
set(data, x, hauteur-1, 1,67,188, ligne_size);
}
int main(int argc, char **argv)
{
unsigned int largeur=2000, hauteur=2000;
FILE *fichier=fopen("labyrinthe.bmp", "w");
unsigned char header[54]={0};
unsigned long ligne_size=largeur*3+complement(largeur*3);
unsigned long datasize=hauteur* ligne_size;
//printf("%ld", datasize);
header[0]='B';
header[1]='M';
CTOI(header[2])=54+datasize;
CTOI(header[10])=54;
CTOI(header[14])=40;
CTOI(header[18])=largeur;
CTOI(header[22])=hauteur;
CTOI(header[26])=1;
CTOI(header[28])=24;
CTOI(header[34])=datasize;
fwrite(&header,1,54,fichier);
char *data=(char*)calloc(datasize,1);
if (data==NULL) return 1;
/* on colorie tout en 2,150,20*/
unsigned int i,j;
for (j=0; j<hauteur; j++)
for (i=0; i<largeur; i++)
set(data, i,j,2,150,20, ligne_size);
srand(2);
draw_labyrinthe(data, (int)largeur/2, 1, 0, hauteur, largeur, 1, ligne_size);
set(data, (int)largeur/2, 1, 1,100,188, ligne_size);
put_exit(data,largeur,hauteur, ligne_size);
fwrite(data, 1, largeur*hauteur*3, fichier);
free(data);
fclose(fichier);
return 0;
}
Historique
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[TECHDAYS2012] OUI J'Y SERAI![TECHDAYS2012] OUI J'Y SERAI! par JeremyJeanson
Bonsoir, Certes, je l'annonce avec un peu de retard, mais je serai effectivement au Techdays demain. Comme l'an dernier, je participerai au programme ATE (Ask The Expert). Si vous avez des questions Workflow, WCF, AppFabric ou plus généralement .net, n'hé...
Cliquez pour lire la suite de l'article par JeremyJeanson TFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICESTFS INTEGRATION TOOLS - SUIVI DES SYNCHRONISATIONS AVEC REPORTING SERVICES par vfabing
Afin de s'assurer du bon fonctionnement des différentes synchronisations effectuées par les TFS Integration Tools, 2 rapports sont présents dès l'installation. Il suffit alors d'effectuer les manipulations suivantes pour pouvoir les visualiser : Loca...
Cliquez pour lire la suite de l'article par vfabing CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks
Forum
RE : ARBRE BINAIRERE : ARBRE BINAIRE par pacotheking
Cliquez pour lire la suite par pacotheking
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|