83 lines
1.4 KiB
C
Raw 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>
#define LIGNES 40
#define COLONNES 60
#define NBR_POMME 5
#define TAILLE_SERPENT 10
2023-12-07 16:16:15 +01:00
int** plateau_init(void) {
int ligne_pomme, colonne_pomme, i;
int** tableau = NULL;
srand(time(NULL));
/* allocation du tableau dans le tas */
tableau = calloc(LIGNES, sizeof(double));
for ( i = 0; i < LIGNES; i++) {
tableau[i] = calloc(COLONNES, sizeof(int));
}
/* positionnement du serpent et marquage de la tete et la queue */
tableau[(LIGNES/2)-5][COLONNES/2] = -2;
tableau[((LIGNES/2)-5)+TAILLE_SERPENT-1][COLONNES/2] = -1;
for (i = 1; i < TAILLE_SERPENT - 1; i++) {
tableau[((LIGNES/2)-5)+i][COLONNES/2] = 1;
}
/* 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 */
while (tableau[ligne_pomme][colonne_pomme] == 2 || tableau[ligne_pomme][colonne_pomme] == 1) {
ligne_pomme = rand() % 40;
colonne_pomme = rand() % 60;
}
/* le chiffre "2" definit une pomme */
tableau[ligne_pomme][colonne_pomme] = 2;
}
return tableau;
}