142 lines
2.3 KiB
C
Raw Permalink Normal View History

/* Création du plateau de jeu avec les pastilles et le serpent
written by Yann KERAUDREN and Titouan LERICHE*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "plateau_init.h"
2023-12-07 16:16:15 +01:00
struct adresse* plateau_init(void) {
2023-12-07 16:16:15 +01:00
int ligne_pomme, colonne_pomme, i;
unsigned char* tete = NULL;
unsigned short* indice_queue = NULL;
unsigned short* taille_serpent = NULL;
2023-12-11 23:32:22 +01:00
int** corps_serpent = NULL;
2023-12-11 23:32:22 +01:00
int** plateau = NULL;
struct adresse* pointeur = NULL;
srand(time(NULL));
/* allocation du pointeur */
2023-12-11 23:32:22 +01:00
pointeur = malloc(sizeof( struct adresse));
2023-12-11 23:32:22 +01:00
/* allocation du tableau tete et queue */
tete = malloc(2 * sizeof(unsigned char));
indice_queue = malloc(sizeof(unsigned short));
2023-12-11 23:32:22 +01:00
/* allocation du plateau dans le tas */
plateau = calloc(LIGNES, sizeof(int*));
for ( i = 0; i < LIGNES; i++) {
2023-12-11 23:32:22 +01:00
plateau[i] = calloc(COLONNES, sizeof(int));
}
/* allocation du corps du serpent */
corps_serpent = malloc(TAILLE_SERPENT * sizeof(int*));
for ( i = 0; i < TAILLE_SERPENT; i++) {
corps_serpent[i] = malloc( 2 * sizeof(int));
}
/* allocation de la taille du serpent */
taille_serpent = malloc(sizeof(unsigned short));
/* positionnement du serpent et marquage de la tete et la queue */
2023-12-13 19:29:11 +01:00
tete[0] = ((LIGNES/2)+(TAILLE_SERPENT/2)-1);
2023-12-11 23:32:22 +01:00
tete[1] = COLONNES/2 ;
2023-12-11 23:32:22 +01:00
2023-12-11 23:32:22 +01:00
for (i = 0; i < TAILLE_SERPENT ; i++) {
2023-12-11 23:32:22 +01:00
plateau[((LIGNES/2)-5)+i][COLONNES/2] = 1;
corps_serpent[i][0] = ((LIGNES/2)-5) + i;
corps_serpent[i][1] = COLONNES/2;
}
/* positionnement alétoire des pommes */
for ( i = 0; i < NBR_POMME; i++) {
ligne_pomme = rand() % 40;
colonne_pomme = rand() % 60;
/* teste pour faire apparaitre exactement 5 pommes */
2023-12-11 23:32:22 +01:00
while (plateau[ligne_pomme][colonne_pomme] == 2 || plateau[ligne_pomme][colonne_pomme] == 1) {
ligne_pomme = rand() % 40;
colonne_pomme = rand() % 60;
}
/* le chiffre "2" definit une pomme */
2023-12-11 23:32:22 +01:00
plateau[ligne_pomme][colonne_pomme] = 2;
2023-12-12 17:39:55 +01:00
}
2023-12-11 23:32:22 +01:00
*taille_serpent = TAILLE_SERPENT;
*indice_queue = 0;
2023-12-12 17:39:55 +01:00
pointeur -> plateau = plateau;
pointeur -> tete = tete;
pointeur -> indice_queue = indice_queue;
pointeur -> corps_serpent = corps_serpent;
pointeur -> taille_serpent = taille_serpent;
2023-12-12 17:39:55 +01:00
return pointeur;
}