This commit is contained in:
2026-02-15 19:53:55 +01:00
parent 2162b3eadd
commit 6a9a4cc27b
6 changed files with 35 additions and 27 deletions
@@ -13,7 +13,7 @@ struct string *my_str_init(const char* s, size_t size){
rslt->size = 0; rslt->size = 0;
} }
else{ else{
rslt->data = malloc(size); rslt->data = malloc((1 + size) * sizeof(char*));
if(rslt->data == NULL){ if(rslt->data == NULL){
free(rslt); free(rslt);
return NULL; return NULL;
+15 -14
View File
@@ -1,11 +1,10 @@
//#include <stdio.h> #include <stdio.h>
#include "demon.h"
#include "villager.h"
#include "weapons.h"
//#include "demon.h" int main()
//#include "villager.h" {
//#include "weapons.h"
//int main()
//{
/* /*
char gun_name[50] = "BFG"; char gun_name[50] = "BFG";
struct gun *gun = init_gun(gun_name, 50, FAR, 16); struct gun *gun = init_gun(gun_name, 50, FAR, 16);
@@ -39,7 +38,8 @@
struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE); struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
update_demon_hp(demon, 200); update_demon_hp(demon, 200);
*/ */
/* char name[50] = "Isabelle"; /*
char name[50] = "Isabelle";
char gun_name[50] = "BFG"; char gun_name[50] = "BFG";
struct gun *gun = init_gun(gun_name, 50, FAR, 16); struct gun *gun = init_gun(gun_name, 50, FAR, 16);
struct villager *villager = struct villager *villager =
@@ -62,14 +62,14 @@
init_villager(name, 50, gun, 1); init_villager(name, 50, gun, 1);
// Heal // Heal
villager->cur_HP -= 40; villager->cur_HP -= 12;
pp_villager(villager); pp_villager(villager);
putchar('\n'); putchar('\n');
heal(villager); heal(villager);
pp_villager(villager); pp_villager(villager);
destroy_villager(villager); destroy_villager(villager);
*/ */
/* char name[50] = "Isabelle"; char name[50] = "Isabelle";
char gun_name[50] = "BFG"; char gun_name[50] = "BFG";
struct gun *gun = init_gun(gun_name, 50, FAR, 16); struct gun *gun = init_gun(gun_name, 50, FAR, 16);
struct villager *villager = struct villager *villager =
@@ -81,7 +81,7 @@
walk(villager, 50); walk(villager, 50);
pp_villager(villager); pp_villager(villager);
destroy_villager(villager); destroy_villager(villager);
*/
/* /*
// Init Villager // Init Villager
char name[50] = "Isabelle"; char name[50] = "Isabelle";
@@ -202,8 +202,9 @@
destroy_villager(villager); destroy_villager(villager);
destroy_demon(demon); destroy_demon(demon);
*/
}*/ }
/*
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -315,4 +316,4 @@ int main()
return 0; return 0;
} }
*/
@@ -50,6 +50,15 @@ void shoot(struct villager *villager, struct demon *demon){
} }
//villager shoot le demon //villager shoot le demon
//Si les degats sont <= 0
if((villager->gun->damage + villager->gun_mastery) <= 0 ){
update_demon_hp(demon, 0);
printf("BAM! %s lost 0 HP.\n", demon->name);
return ;
}
//Si les degats sont > 0
villager->gun->current_magazine --; villager->gun->current_magazine --;
if((villager->distance >= demon->range) && (villager->gun->range >= demon->range)){ if((villager->distance >= demon->range) && (villager->gun->range >= demon->range)){
@@ -71,7 +80,7 @@ void heal(struct villager *villager){
villager->medicines --; villager->medicines --;
int gain = villager->cur_HP * 1.25; int gain = villager->HP_max * 0.25;
villager->cur_HP += gain; villager->cur_HP += gain;
if (villager->cur_HP > villager->HP_max) villager->cur_HP = villager->HP_max; if (villager->cur_HP > villager->HP_max) villager->cur_HP = villager->HP_max;
@@ -116,6 +125,7 @@ enum distance walk(struct villager *villager, int direction){
//printf("Distance act: %d\n", villager->distance); //printf("Distance act: %d\n", villager->distance);
villager->distance -= direction; villager->distance -= direction;
//printf("Distance apres chg: %d\n",villager->distance); //printf("Distance apres chg: %d\n",villager->distance);
printf("You decided to move.\n");
return villager->distance; return villager->distance;
} }
+6 -6
View File
@@ -59,13 +59,13 @@ int main(){
free(v->animal); free(v->animal);
free(v); free(v);
*/ */
/*
struct vector *v = vector_init(2); struct vector *v = vector_init(10);
v = vector_resize(v, 4); v = vector_resize(v, 5);
printf("Resized capacity: %zu\n", v->capacity); printf("Resized capacity: %zu\n", v->capacity);
free(v->animal); free(v->animal);
free(v); free(v);
*/
/* /*
struct vector *v = vector_init(2); struct vector *v = vector_init(2);
@@ -105,12 +105,12 @@ int main(){
struct animal *a = animal_from_fish("Blue", f); struct animal *a = animal_from_fish("Blue", f);
free_animal(a); free_animal(a);
*/ */
struct vector *v = vector_init(5); /* struct vector *v = vector_init(5);
struct fish *f = fish_init("Trout", 8, 4, 1); struct fish *f = fish_init("Trout", 8, 4, 1);
struct animal *a = animal_from_fish("Green", f); struct animal *a = animal_from_fish("Green", f);
v = vector_append(v, a); v = vector_append(v, a);
vector_destroy(v); vector_destroy(v);
*/
} }
@@ -34,8 +34,6 @@ struct vector *vector_append(struct vector *v, struct animal *animal){
if(v == NULL || animal == NULL) return NULL; if(v == NULL || animal == NULL) return NULL;
if (v->capacity <= v->size) v = vector_resize(v, v->capacity * 2); if (v->capacity <= v->size) v = vector_resize(v, v->capacity * 2);
// TODO A voir si le realloc ajoute de l'espace a gauche ou a droite
// car sinon y a ecrasement de data
v->animal[v->size] = animal; v->animal[v->size] = animal;
v->size ++; v->size ++;
return v; return v;
@@ -70,5 +68,4 @@ void vector_destroy(struct vector *v){
free(v->animal); free(v->animal);
free(v); free(v);
//free_animal(v->animal);
} }