Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 11/12/2016, à 21:43

brindillus_1er

Début de roguelike

Bonjour à tous,
je développe depuis quelque temps un roguelike, et j'aimerais avoir vos avis. Le projet n'en est encore qu'à ses débuts, la version ci-jointe génère une dizaine de donjons, et vous permet de vous déplacer à travers ceux-ci au moyen des escaliers (< et >), et...c'est tout pour l'instant.

Voilà, donc si vous avez un peu de temps pour le tester, faites-moi part de vos avis, si vous avez des idées d'améliorations, des remarques, etc. Je prends tout.

Et aussi si quelqu'un pouvait m'expliquer comment joindre des fichiers ce serait sympa, parce que là c'est pas terrible, il faut copier le code (c'est du C), compiler et tester. Donc si une âme charitable peut m'éclairer, je lui en serais reconnaissant.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define couleur(param) printf("\033[%sm",param)

#define COTE 36

#define TAILLE (int) COTE/6

int grille[COTE*COTE][COTE][COTE], schema[TAILLE][TAILLE], schema_chiffre[TAILLE][TAILLE];
int X_D = 0, Y_D = 3, X_F, Y_F;
char danger[6][50] = {"Faible", "Normale", "Elevée", "Très élevée", "Impossible", "Fuyez, pauvres fous!"};

typedef struct Joueur Joueur;

struct Joueur
{
	int pos_x, pos_y, pos_z;
};
Joueur J;

void InitGrille(int z)
{
	int i, j;
	for (i = 0; i < COTE; i++)
	{
		for(j = 0; j < COTE; j++)
			grille[z][i][j] = 1;
	}
}

void AffGrille(int z)
{
	/*-1: joueur
	  0 : sol (vide)
	  1 : mur
	  2 : escalier niv précédent
	  3 : escalier niv suivant
	  4 : coffre nul
	  5 : coffre moyen
	  6 : coffre top
	*/
	int i, j, etage;
	etage = (int) z/6;
	printf("\033[H\033[2J");
	for (i = 0; i < COTE; i++)
	{
		for(j = 0; j < COTE; j++)
		{
			if (grille[z][i][j] == -1)
			{
				couleur("39");
				printf("@ ");
			}
			if (grille[z][i][j] == 1)
			{
				couleur("34");
				printf("# ");
			}
			else if (grille[z][i][j] == 0)
			{
				couleur("39");
				printf(". ");
			}
			else if (grille[z][i][j] == 2)
			{
				couleur("32");
				printf("< ");
			}
			else if (grille[z][i][j] == 3)
			{
				couleur("32");
				printf("> ");
			}
			else if (grille[z][i][j] >= 4 && grille[z][i][j] <= 6)
			{
				couleur("32");
				printf("! ");
			}
		}
		couleur("37");	
		if (i == COTE-1)
			printf("\tEtage actuel : %i", -etage);
		else if (i == COTE-2)
			printf("\tDangerosité : %s", danger[etage/6]);
		printf("\n");
	}
}

void AffSchema_chiffre()
{
	int i, j;
	for (i = 0; i < TAILLE; i++)
	{
		for(j = 0; j < TAILLE; j++)
			printf("%i ", schema_chiffre[i][j]);
		printf("\n");
	}
}

void CreerSchema(int x_debut, int y_debut)
{
	/*0 : mur
      1 : case de salle
	  2 : couloir
	*/
	
	int i, j, next_x, next_y, test = 0, x, y, dir;

	do
	{
		X_F = rand()%((TAILLE-1)-0+1)+0;
		Y_F = rand()%((TAILLE-1)-0+1)+0;
	}while(X_F == x_debut && Y_F == y_debut);
	
	X_D = x_debut;
	Y_D = y_debut;
	
	schema[y_debut][x_debut] = 1;
	x = x_debut;
	y = y_debut;						
	do
	{
		if (schema[y][x] == 1 || schema[y][x] == 2)
		{
			dir = 0;
			while (dir == 0 || (dir == 1 &&(x+1) > (TAILLE-1)) || (dir == 2 &&(x-1) < 0) || (dir == 3 && (y+1) > (TAILLE-1)) || (dir == 4 && (y-1) < 0))
				dir = rand()%(4-1+1)+1;
			switch(dir)
			{
				case 1 :
					x++;
					break;
				case 2 :
					x--;
					break;
				case 3 :
					y++;
					break;
				default :
					y--;
					break;		
			}
			test++;
			schema[y][x] = rand()%(2-1+1)+1;
		}
	}while(schema[Y_F][X_F] != 1);
	schema[y_debut][x_debut] = 1;
}

void CreerSchema_chiffre()
{
	int i, j, gauche, droite, haut, bas;
	for (i = 0; i < TAILLE; i++)
	{
		for (j = 0; j < TAILLE; j++)
			schema_chiffre[i][j] = schema[i][j];
	}
	
	for (i = 0; i < TAILLE; i++)
	{
		for (j = 0; j < TAILLE; j++)
		{
			if (schema_chiffre[i][j] == 2)
			{
				gauche = 0;
				droite = 0;
				haut = 0;
				bas = 0;
				if (i > 0)
				{
					if (schema[i-1][j] != 0)
						haut = 1;
				}
				if (i < TAILLE-1)
				{
					if (schema[i+1][j] != 0)
						bas = 1;
				}
				if (j > 0)
				{
					if (schema[i][j-1] != 0)
						gauche = 1;
				}
				if (j < TAILLE-1)
				{
					if (schema[i][j+1] != 0)
						droite = 1;
				}
				if (gauche && droite && haut && bas)
					schema_chiffre[i][j] = 8;
				else if (gauche && droite && haut)
					schema_chiffre[i][j] = 4;
				else if (bas && gauche && droite)
					schema_chiffre[i][j] = 5;
				else if (gauche && haut && bas)
					schema_chiffre[i][j] = 6;
				else if (droite && haut && bas)
					schema_chiffre[i][j] = 7;
				else if (haut && gauche)
					schema_chiffre[i][j] = 9;
				else if (gauche && bas)
					schema_chiffre[i][j] = 10;
				else if (bas && droite)
					schema_chiffre[i][j] = 11;
				else if (droite && haut)
					schema_chiffre[i][j] = 12;
				else if (gauche && droite)
					schema_chiffre[i][j] = 2;
				else if (haut && bas)
					schema_chiffre[i][j] = 3;
				else if (droite)
					schema_chiffre[i][j] = 13;
				else if (gauche)
					schema_chiffre[i][j] = 14;
				else if (haut)
					schema_chiffre[i][j] = 15;
				else if (bas)
					schema_chiffre[i][j] = 16;
			}
		}
	}
}

void PlacerEscaliers(int z)
{
	int x_debut = X_D*TAILLE+(rand()%((TAILLE-1)-0+1)+0), x_fin = X_F*TAILLE+(rand()%((TAILLE-1)-0+1)+0);
	int y_debut = Y_D*TAILLE+(rand()%((TAILLE-1)-0+1)+0), y_fin = Y_F*TAILLE+(rand()%((TAILLE-1)-0+1)+0);
	grille[z][y_debut][x_debut] = 2;
	grille[z][y_fin][x_fin] = 3;
}

void PlacerObjets(int z)
{
	int x, y, nb_coffres;

	if (z%5 != 0)
	{
		nb_coffres = rand()%(1-0+1)+0;
		nb_coffres += rand()%(1-0+1)+0;
		while (nb_coffres > 0)
		{
			do
			{
				x = rand()%((COTE-1)-0+1)+0;
				y = rand()%((COTE-1)-0+1)+0;
			}while(grille[z][y][x] != 0);
			grille[z][y][x] = 4;
			nb_coffres --;
		}
	}
	else if (z%5 == 0 && (z == 5 || z == 15 || z == 25 || z == 35))
	{
		do
		{
			x = rand()%((COTE-1)-0+1)+0;
			y = rand()%((COTE-1)-0+1)+0;
		}while(grille[z][y][x] != 0);
		grille[z][y][x] = 5;
	}
	else
	{
		do
		{
			x = rand()%((COTE-1)-0+1)+0;
			y = rand()%((COTE-1)-0+1)+0;
		}while(grille[z][y][x] != 0);
		grille[z][y][x] = 6;
	}
}

void CreerGrille(int z)
{
	int i, j, coord[2], val_case;
	
	CreerSchema(X_D, Y_D);
	CreerSchema_chiffre();
	
	for (i = 0; i < COTE; i++)
	{
		for(j = 0; j < COTE; j++)
			grille[z][i][j] = 1;
	}
	
	for (i = 0; i < COTE; i++)
	{
		for (j = 0; j < COTE; j++)
		{
			val_case = schema_chiffre[(int) i/6][(int) j/6];
			//printf("%i", val_case);
			coord[0] = i%6;
			coord[1] = j%6;
			
			// 0 : vide 1: mur
			if (val_case == 1)
				grille[z][i][j] = 0;
			if (coord[0] <= 3 && coord[0] >= 2 && coord[1] <= 3 && coord[1] >= 2 && val_case != 0)
				grille[z][i][j] = 0;
			else if ((coord[0] == 0 || coord[0] == 1) && (coord[1] == 2 || coord[1] == 3))
			{
				if (val_case == 3 || val_case == 4 || val_case == 6 || val_case == 7 || val_case == 8 || val_case == 9 || val_case == 12 || val_case == 15)
					grille[z][i][j] = 0;
			}
			else if ((coord[0] == 4 || coord[0] == 5) && (coord[1] == 2 || coord[1] == 3))
			{
				if (val_case == 3 || val_case == 5 || val_case == 6 || val_case == 7 || val_case == 8 || val_case == 10 || val_case == 11 || val_case == 16)
					grille[z][i][j] = 0;
			}
			else if ((coord[1] == 0 || coord[1] == 1) && (coord[0] == 2 || coord[0] == 3))
			{
				if (val_case == 2 || val_case == 4 || val_case == 5 || val_case == 6 || val_case == 8 || val_case == 9 || val_case == 10 || val_case == 14)
					grille[z][i][j] = 0;
			}
			else if ((coord[1] == 4 || coord[1] == 5) && (coord[0] == 2 || coord[0] == 3))
			{
				if (val_case == 2 || val_case == 4 || val_case == 5 || val_case == 7 || val_case == 8 || val_case == 11 || val_case == 12 || val_case == 13)
					grille[z][i][j] = 0;
			}
		}
		//printf("\n");
	}
	PlacerEscaliers(z);
	PlacerObjets(z);
	X_D = X_F;
	Y_D = Y_F;
}

void Manuel()
{
	printf("Manuel");
}

/*int Menu()
{
	char test;
	printf("\t\t\t[J]OUER\t[M]ANUEL\t[Q]UITTER\n");
	do
	{
		system("/bin/stty raw");
		test = getchar();
		system("/bin/stty cooked");
	}while(test != 'j' && test != 'm' && test != 'q');
	if (test == 'm')
		Manuel();
	else if (test == 'q')
	{
		couleur("0");
		printf("\033[H\033[2J\n\n");
		exit(0);
	}
	else
		return 0;
}*/

void Bienvenue()
{
	char test;
	printf("\033[H\033[2J\n\n");
	printf("\tM       M  I  N     N      A      S S S S\tI  T T T T T  H     H  I  L\n");
	printf("\tM M   M M  I  N N   N     A A     S      \tI      T      H     H  I  L\n");
	printf("\tM  M M  M  I  N  N  N    A   A    S S S S\tI      T      H H H H  I  L\n");
	printf("\tM   M   M  I  N   N N   A A A A         S\tI      T      H     H  I  L\n");
	printf("\tM       M  I  N     N  A       A  S S S S\tI      T      H     H  I  L L L L\n\n");
	printf("\t\tDéveloppé par Brindillus 1er\n\n");//\t\t(Appuyez sur Entrée pour continuer)\n\n");
	/*do
	{
		system("/bin/stty raw");
		test = getchar();
		system("/bin/stty cooked");
	}while(test != 13);
	Menu();*/
}

int Deplacement(int z)
{
	char depl;
	int x, y, max = COTE-1, oldz = z;
	do
	{
		system("/bin/stty raw");
		depl = getchar();
		system("/bin/stty cooked");
	}while(depl != 'x' && (depl != 'z' && J.pos_y<=0) && (depl != 'q' && J.pos_x<=0) && (depl != 's' && J.pos_y>=max) && (depl != 'd' && J.pos_x>=max));
	x = J.pos_x;
	y = J.pos_y;
	if (depl == 'd' && grille[z][J.pos_y][J.pos_x+1] == 0)
		J.pos_x ++;
	else if (depl == 's' && grille[z][J.pos_y+1][J.pos_x] == 0)
		J.pos_y ++;
	else if (depl == 'q' && grille[z][J.pos_y][J.pos_x-1] == 0)
		J.pos_x --;
	else if (depl == 'z' && grille[z][J.pos_y-1][J.pos_x] == 0)
		J.pos_y--;
	else if ((depl == 'd' && grille[z][J.pos_y][J.pos_x+1] == 3) || (depl == 's' && grille[z][J.pos_y+1][J.pos_x] == 3) ||
	(depl == 'q' && grille[z][J.pos_y][J.pos_x-1] == 3) || (depl == 'z' && grille[z][J.pos_y-1][J.pos_x] == 3))
	{
		J.pos_z ++;
		z++;
	}
	else if ((depl == 'd' && grille[z][J.pos_y][J.pos_x+1] == 2) || (depl == 's' && grille[z][J.pos_y+1][J.pos_x] == 2) ||
	(depl == 'q' && grille[z][J.pos_y][J.pos_x-1] == 2) || (depl == 'z' && grille[z][J.pos_y-1][J.pos_x] == 2))
	{
		J.pos_z --;
		z--;
	}
	else if (depl == 'x')
	{
		couleur("0");
		//Menu();
		exit(0);
	}
	if (x != J.pos_x || y != J.pos_y)
		grille[z][y][x] = 0;
	grille[z][J.pos_y][J.pos_x] = -1;
	if (oldz < z)
		grille[z-1][J.pos_y][J.pos_x] = 0;
	else if (oldz > z)
		grille[z+1][J.pos_y][J.pos_x] = 0;
	return z;
}

int main()
{
	srand(time(NULL));
	int z;
	J.pos_x = 2, J.pos_y = 20, J.pos_z = 1;
	couleur("40");
	Bienvenue();
	
	for (z = 1; z < 10; z++)
	{
		InitGrille(z);
		CreerGrille(z);
		//printf("%i", z);
	}
	z = 1;
	grille[z][J.pos_y][J.pos_x] = -1;
	while (1)
	{
		z = Deplacement(z);
		AffGrille(z);
	}
	return 0;
}

Hors ligne