Serpentsss

Description

Voici un serpent :
il ne faut rien attraper, il y a juste plusieurs serpents !
il faut survivre, les "autres" serpents sont diriges pas algorithme :
  • 2 qui bouge n'importe ou au hasard
  • 1 qui vous "suit"

Maintenant a vous d'inventer d'autre alogrithme (dites les moi !)

Bon alors il y a des bugs ! des fois un serpent travers un autres.
J'ai fais ce programme juste pour montrer que les serpents, on peut en mettre beaucoup ! (dans l'exemple il y en a 4) et que c'est interresant de faire des algorithmes pour un serpent !

Je tiens a remarquer que l'algorithme bebete ... he bien moi je trouve que le serpent a l'aire naturel ! poutant l'algo est tres simple ! Faites moi part de votre avis.

Source / Exemple :


#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <time.h>
#include <stdlib.h>

/******************************************************/
#define TRUE 			1
#define FALSE 		0
/******************************************************/
#define SIZE_GROUND_X                       37
#define SIZE_GROUND_Y                       20

#define SIZE_SNAKE_BEGIN										5
#define SIZE_MAX_SNAKE                      20
#define NB_ITERATION_FOR_EXPAND_SNAKE       10
/******************************************************/
#define INVALID_NUM_SNAKE  			-1
#define QUIT 										1
#define LOST 										2
/******************************************************/
typedef unsigned UINT;

typedef struct TagCOOR
	{
	UINT x;
	UINT y;
	}COOR,*P_COOR;

typedef struct TagSNAKE
	{
	P_COOR 	snake;
	P_COOR 	pHead,pTail;
	P_COOR 	adresseEndSnake;
	COOR    dir;
	int    	sizeSnake;
	int     (*AdvanceSnake)(int [][SIZE_GROUND_Y],struct TagSNAKE*,struct TagSNAKE*);

	int    	colorSnake;
	int     numSnake;
	int     boolQuitIfLost;
	int     expandSnake;

	}SNAKE,*P_SNAKE;

/******************************************************/
#define Malloc(type,nb) (type*) _Malloc(sizeof(type)*(nb))
void *_Malloc(size_t size)
{
void *p;
if((p = malloc(size)) == NULL)
	{
	printf("Erreur !\nImpossible d'allouer %d octets de memoires\n",size);
	exit(1);
	}
return p;
}

/******************************************************/
/*

  • Une case represente 2 caracteres => cela forme un "carre"
  • Il y a une cadre, d'ou les '1+...'
  • /
void Gotoxy(COOR *pCoor) { gotoxy(3+(pCoor->x << 1),2+pCoor->y); } /******************************************************/ void DrawCadre(int color) { int i; textcolor(color); /* ligne du haut */ gotoxy(1,1); i = 5+2*SIZE_GROUND_X; while(--i) cprintf("Û"); /* ligne du bas */ gotoxy(1,2+SIZE_GROUND_Y); i = 5+2*SIZE_GROUND_X; while(--i) cprintf("Û"); /* ligne a gauche et a droite */ for(i=2;i<=1+SIZE_GROUND_Y;i++) { gotoxy(1,i); cprintf("ÛÛ"); gotoxy(3+2*SIZE_GROUND_X,i); cprintf("ÛÛ"); } } /******************************************************/ int GetValueGround(int ppGround[][SIZE_GROUND_Y],COOR *pCoor) { return ppGround[pCoor->x][pCoor->y]; } /******************************************************/ void SetValueGround(int ppGround[][SIZE_GROUND_Y],COOR *pCoor,int value) { ppGround[pCoor->x][pCoor->y] = value; } /******************************************************/ void NewSnake( int ppGround[][SIZE_GROUND_Y], P_SNAKE pSnake, int colorSnake, int numSnake, int boolQuitIfLost, int (*func)(int [][SIZE_GROUND_Y],P_SNAKE,P_SNAKE) ) { P_COOR snake; pSnake->snake = snake = Malloc(COOR,SIZE_MAX_SNAKE); pSnake->pHead = snake; pSnake->pTail = snake; pSnake->sizeSnake = SIZE_SNAKE_BEGIN - 1; pSnake->dir.x = 1; pSnake->dir.y = 0; pSnake->adresseEndSnake = snake + SIZE_MAX_SNAKE - 1; pSnake->numSnake = numSnake; pSnake->AdvanceSnake = func; pSnake->colorSnake = colorSnake; pSnake->boolQuitIfLost = boolQuitIfLost; pSnake->expandSnake = NB_ITERATION_FOR_EXPAND_SNAKE; snake->x = 1; snake->y = numSnake; ppGround[1][1] = numSnake; } /******************************************************/ void PrintTailSnake(int ground[][SIZE_GROUND_Y],P_SNAKE pSnake) { Gotoxy(pSnake->pTail); cprintf(" "); SetValueGround(ground,pSnake->pTail,0); pSnake->pTail = (pSnake->pTail >= pSnake->adresseEndSnake ? pSnake->snake : pSnake->pTail + 1); } /******************************************************/ int PrintHeadSnake(int ground[][SIZE_GROUND_Y],P_SNAKE pSnake) { P_COOR pOldHead; pOldHead = pSnake->pHead; pSnake->pHead = (pSnake->pHead >= pSnake->adresseEndSnake ? pSnake->snake : pSnake->pHead + 1); pSnake->pHead->x = (pOldHead->x + pSnake->dir.x + SIZE_GROUND_X) % SIZE_GROUND_X; pSnake->pHead->y = (pOldHead->y + pSnake->dir.y + SIZE_GROUND_Y) % SIZE_GROUND_Y; if(GetValueGround(ground,pSnake->pHead) != 0) { return 0; } Gotoxy(pSnake->pHead); cprintf("ÛÛ"); SetValueGround(ground,pSnake->pHead,pSnake->numSnake); return 1; } /******************************************************/ void DeleteSnake(int ground[][SIZE_GROUND_Y],P_SNAKE pSnake) { while((--(pSnake->sizeSnake)) >= 0) { PrintTailSnake(ground,pSnake); } free(pSnake->snake); } /******************************************************/ void PrintSnake(int ground[][SIZE_GROUND_Y],P_SNAKE pSnake) { int i; i = pSnake->sizeSnake; textcolor(pSnake->colorSnake); while((--i) >= 0) { PrintHeadSnake(ground,pSnake); } } /******************************************************/ void NewGround(int ground[][SIZE_GROUND_Y]) { int i,j; for(i=0;i<SIZE_GROUND_X;i++) { for(j=0;j<SIZE_GROUND_X;j++) { ground[i][j] = 0; } } } /******************************************************/ int serpent( int ground[][SIZE_GROUND_Y], unsigned temp, int colorCadre, int bkcolor, int nbSnake, P_SNAKE tabSnake, P_SNAKE pSnakeGood ) { int i; /* initialisations */ textbackground(bkcolor); clrscr(); DrawCadre(colorCadre); for(i=0;i<nbSnake;i++) { PrintSnake(ground,tabSnake + i); } do { for(i=0;i<nbSnake;i++) { P_SNAKE pSnake; pSnake = tabSnake + i; /* si le serpent est 'invalide' */ if(pSnake->numSnake == INVALID_NUM_SNAKE) { continue; } textcolor(pSnake->colorSnake); if(!(-- pSnake->expandSnake)) { pSnake->expandSnake = NB_ITERATION_FOR_EXPAND_SNAKE; if((++ pSnake->sizeSnake) >= SIZE_MAX_SNAKE) { int boolQuit; /* si le serpent est trop long */ boolQuit = pSnake->boolQuitIfLost; DeleteSnake(ground,pSnake); if(boolQuit) { return ; } } PrintHeadSnake(ground,pSnake); } if(pSnake->AdvanceSnake(ground,pSnake,pSnakeGood)) { return; } /* on efface la queue */ PrintTailSnake(ground,pSnake); /* on affiche la nouvelle tete */ if(!PrintHeadSnake(ground,pSnake)) { int boolQuit; /* on perd */ boolQuit = pSnake->boolQuitIfLost; DeleteSnake(ground,pSnake); pSnake->numSnake = INVALID_NUM_SNAKE; if(boolQuit) { /* si on veut quitter lorque ce serpent est mort */ return ; } } } /* on deplace le curseur pendant l'attente */ gotoxy(1,1); delay(temp); }while(1 == 1); } /******************************************************/ /******************************************************/ /******************************************************/ int AdvanceSnakePlayer(int ground[][SIZE_GROUND_Y],P_SNAKE pSnake,P_SNAKE pSnakeGood) { P_COOR pDir; (void) ground; (void) pSnakeGood; pDir = &(pSnake->dir); if(kbhit()) { switch(getch()) { case '5': /* ATTENT */ getch(); break; case '4': /* GAUCHE */ pDir->x = -1; pDir->y = 0; break; case '6': /* DROITE */ pDir->x = +1; pDir->y = 0; break; case '8': /* HAUT */ pDir->x = 0; pDir->y = -1; break; case '2': /* BAS */ pDir->x = 0; pDir->y = +1; break; case 'q': /* on veut quitter a tout prix */ return QUIT; } } return 0; } /******************************************************/ int AdvanceSnakeAlgo1(int ground[][SIZE_GROUND_Y],P_SNAKE pSnake,P_SNAKE pSnakeGood) { (void) ground; (void) pSnakeGood; /*
  • on change la direction (X -> Y ou Y -> X)
  • selon la valeur d'un 'rand'
  • Ici il y a 1 chance sur 16 :
  • on fait le modulo 16 ( & 15 pour etre plus rapide !)
  • /
if(!(rand() & 15)) { P_COOR pDir; /* 'val' vaut soit +1 soit -1 */ int val; val = (rand() & 1) ? +1 : -1; pDir = &(pSnake->dir); /* si le serpent se deplace suivant l'axe verticale */ if(pDir->x) { pDir->x = 0; pDir->y = val; } /* si le serpent se deplace suivant l'axe horizontale */ else { pDir->x = val; pDir->y = 0; } } return 0; } /******************************************************/ int AdvanceSnakeAlgo2(int ground[][SIZE_GROUND_Y],P_SNAKE pSnake,P_SNAKE pSnakeGood) { (void) ground; /*
  • le serpent suit le joueur
  • /
pSnake->dir.x = pSnakeGood->dir.x; pSnake->dir.y = pSnakeGood->dir.y; return 0; } /******************************************************/ /******************************************************/ /******************************************************/ int main(void) { /* la plateau de jeu */ int ground[SIZE_GROUND_X][SIZE_GROUND_Y]; SNAKE tabSnake[4]; /* initialisation du random pour les algorithmes */ randomize(); NewGround(ground); /* * * * * * * * * * * * * * * * */ /* UN */ NewSnake( ground, tabSnake + 0, YELLOW, 2, TRUE, AdvanceSnakePlayer ); /* * * * * * * * * * * * * * * * */ /* DEUX */ NewSnake( ground, tabSnake + 1, LIGHTGREEN, 6, FALSE, AdvanceSnakeAlgo1 ); /* * * * * * * * * * * * * * * * */ /* TROIS */ NewSnake( ground, tabSnake + 2, RED, 12, FALSE, AdvanceSnakeAlgo2 ); /* * * * * * * * * * * * * * * * */ /* QUATRE */ NewSnake( ground, tabSnake + 3, LIGHTGREEN, 16, FALSE, AdvanceSnakeAlgo1 ); serpent( ground, /* le plateau de jeu */ 5000, /* delai */ LIGHTBLUE, /* couleur du cadre */ BLACK, /* couleur du fond */ 4, /* nombre de serpents mechants */ tabSnake, /* tous les serpents */ tabSnake+0 /* le serpent 'gentil' */ ); return 0; }

Codes Sources

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.