85 lines
1.5 KiB
C
85 lines
1.5 KiB
C
/* Fonction qui sert à aggrandir la taille du serpent lorsque celui-ci mange une pomme
|
|
|
|
Written by Yann KERAUDREN and Titouan LERICHE */
|
|
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "augmentation_serpent.h"
|
|
|
|
|
|
|
|
void augmentation_serpent (struct adresse* pointeur) {
|
|
|
|
|
|
int** corps_serpent = pointeur -> corps_serpent;
|
|
|
|
unsigned short* indice_queue = pointeur -> indice_queue;
|
|
|
|
unsigned char* tete = pointeur -> tete;
|
|
|
|
unsigned short* taille_serpent = pointeur -> taille_serpent;
|
|
|
|
int** corps_serpent2 = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* augmentation de la taille du serpent */
|
|
|
|
|
|
*taille_serpent = *taille_serpent + 1;
|
|
|
|
|
|
|
|
|
|
/* création d'un nouveau tableau */
|
|
|
|
corps_serpent2 = malloc( *taille_serpent * sizeof(int*));
|
|
|
|
for ( i = 0; i < *taille_serpent; i++) {
|
|
|
|
corps_serpent2[i] = malloc( 2 * sizeof(int));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* attributions des anciennes cases du corps dans le nouveau tableau et ajout de la nouvelle tête au bonne endroit*/
|
|
|
|
for ( i = 0 ; i < *indice_queue; i++) {
|
|
|
|
corps_serpent2[i][0]= corps_serpent[i][0] ;
|
|
corps_serpent2[i][1]= corps_serpent[i][1] ;
|
|
|
|
}
|
|
|
|
|
|
corps_serpent2[*indice_queue][0] = tete[0];
|
|
corps_serpent2[*indice_queue][1] = tete[1];
|
|
|
|
|
|
for ( i = *indice_queue ; i < *taille_serpent - 1; i++) {
|
|
|
|
corps_serpent2[i+1][0] = corps_serpent[i][0];
|
|
corps_serpent2[i+1][1] = corps_serpent[i][1];
|
|
|
|
}
|
|
|
|
*indice_queue = *indice_queue + 1;
|
|
|
|
pointeur -> corps_serpent = corps_serpent2;
|
|
|
|
|
|
for ( i = 0; i < *taille_serpent - 1; i++) {
|
|
|
|
free( corps_serpent[i]);
|
|
}
|
|
|
|
free(corps_serpent);
|
|
|
|
|
|
}
|