This commit is contained in:
2026-02-11 13:20:47 +01:00
commit d3645f663c
30 changed files with 403 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
CC=gcc
SRC = my_strings.c main.c
CFLAGS= -Wall -Wextra -Werror
LDFLAGS= -fsanitize=address -g
# Computes the basic main for tests
main: ${SRC}
gcc $(CFLAGS) $^ -o main -lm
#Check for memory errors/leaks and to use gdb
debug: ${SRC}
gcc $(CFLAGS) $^ -o debug $(LDFLAGS)
clean:
$(RM) main debug

View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include "my_strings.h"
int main(){
// struct string *str = my_str_init("Hello World!", 12);
// printf("data: %.*s\n", (int)str->size, str->data);
//struct string *str = my_str_init("Hello World!", 12);
//my_str_destroy(str);
//struct string *str = my_str_init("Hello Alien World!", 18);
//my_str_puts(str);
// my_str_destroy(str);
struct string *str = my_str_init("Hello ", 6);
str = my_str_n_cat(str, "Alien World!", 13);
my_str_puts(str);
my_str_destroy(str);
}

View File

@@ -0,0 +1,66 @@
#include "my_strings.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//' -> ' est un racc pour *rslt.data
struct string *my_str_init(const char* s, size_t size){
struct string *rslt = malloc(sizeof(struct string));
if(rslt == NULL) return NULL;
if(size == 0 || s == NULL) {
rslt->data = NULL;
rslt->size = 0;
}
else{
rslt->data = malloc(size);
if(rslt->data == NULL){
free(rslt);
return NULL;
}
memcpy(rslt->data, s, size);
rslt->size = size;
}
return rslt;
}
void my_str_destroy(struct string *str){
if(str == NULL) return;
free(str->data);
free(str);
}
void my_str_puts(struct string *str){
if(str == NULL) return;
if(str->data == NULL || str->size == 0)printf("\n");
for (size_t i = 0; i < str->size; i++) putchar(*((str->data) + i));
}
struct string *my_str_n_cat(struct string *str1, const char *str2, size_t str2_len){
if (str2 == NULL || str2_len == 0) return str1;
if (str1 == NULL){
struct string *rslt = my_str_init(str2, str2_len);
return rslt;
}
str1->data = realloc(str1->data, (str1->size + str2_len) * sizeof(char));
if(str1 == NULL) return str1;
//printf("test");
int index = 0;
while (*(str2 + index)){
str1->data[str1->size + index] = str2[index];
index ++;
}
str1->size = str1->size + index;
return str1;
}

View File

@@ -0,0 +1,17 @@
#ifndef MY_STRINGS_H
#define MY_STRINGS_H
#include <strings.h>
struct string{
char *data;
size_t size;
};
struct string *my_str_init(const char* s, size_t size);
void my_str_destroy(struct string *str);
void my_str_puts(struct string *str);
struct string *my_str_n_cat(struct string *str1, const char *str2, size_t str2_len);
#endif

View File

@@ -0,0 +1,15 @@
CC=gcc
SRC = radar.c main.c
CFLAGS= -Wall -Wextra -Werror
LDFLAGS= -fsanitize=address -g -lm
# Computes the basic main for tests
main: ${SRC}
gcc $(CFLAGS) $^ -o main -lm
#Check for memory errors/leaks and to use gdb
debug: ${SRC}
gcc $(CFLAGS) $^ -o debug $(LDFLAGS)
clean:
$(RM) main debug

Binary file not shown.

View File

@@ -0,0 +1,43 @@
#include <stdio.h>
#include "radar.h"
int main(){
// compute_distance
struct point p1 = { 0.0, 0.0 };
struct point p2 = { 3.0, 4.0 };
double dist = compute_distance(p1, p2);
printf("Distance: %.2f\n", dist);
// is_in_area
struct point center = { 5.0, 5.0 };
double radius = 3.0;
struct point inside_point = { 7.0, 7.0 };
struct point outside_point = { 9.0, 9.0 };
int inside = is_in_area(center, radius, inside_point);
int outside = is_in_area(center, radius, outside_point);
printf("Inside: %d\n", inside);
printf("Outside: %d\n", outside);
// detect_points
center.x = 10.0;
center.y = 10.0;
double range = 5.0;
struct point points[] = {
{ 11.0, 11.0 },
{ 10.0, 10.0 },
{ 20.0, 20.0 },
{ 14.0, 13.0 }
};
size_t size = sizeof(points) / sizeof(struct point);
size_t found = detect_points(center, range, points, size);
printf("Total found: %zu\n", found);
}

View File

@@ -0,0 +1,31 @@
#include "radar.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double compute_distance(struct point a, struct point b){
return sqrt( pow((b.x - a.x),2) + pow((b.y - a.y),2) );
}
int is_in_area(struct point center, double radius, struct point point){
int rslt = compute_distance(center, point);
if(rslt > radius) return 0;
return 1;
}
size_t detect_points(struct point center, double range, struct point *interesting_points, size_t nb_points){
size_t found = 0;
for(size_t i = 0; i < nb_points; i++){
if(is_in_area(center, range,interesting_points[i]) == 1){
printf("{ %f, %f }\n",interesting_points[i].x, interesting_points[i].y);
found ++;
}
}
return found;
}

View File

@@ -0,0 +1,16 @@
#ifndef RADAR_H
#define RADAR_H
#include <stdio.h>
struct point
{
double x;
double y;
};
double compute_distance(struct point a, struct point b);
int is_in_area(struct point center, double radius, struct point point);
size_t detect_points(struct point center, double range, struct point* interesting_points, size_t nb_points);
#endif

View File

@@ -0,0 +1,16 @@
CC=gcc
SRC = villager.c demon.c weapons.c print.c main.c
CFLAGS= -Wall -Wextra -Werror
LDFLAGS= -fsanitize=address -g -lm
# Computes the basic main for tests
main: ${SRC}
gcc $(CFLAGS) $^ -o main -lm
#Check for memory errors/leaks and to use gdb
debug: ${SRC}
gcc $(CFLAGS) $^ -o debug $(LDFLAGS)
clean:
$(RM) main debug

View File

@@ -0,0 +1,37 @@
#ifndef DEMON_H
#define DEMON_H
#include "villager.h"
struct villager;
enum demon_category
{
PARASITE = 1,
THREAT,
NIGHTMARE,
CALAMITY,
TOM_NOOK
};
struct demon {
enum demon_category category;
char name[50];
int damage;
enum distance range;
int cur_HP;
int HP_max;
};
struct demon *init_demon(enum demon_category category, char name[50], int HP_max, int damage, enum distance range);
void pp_demon(struct demon *demon);
void destroy_demon(struct demon *demon);
void basic_attack(struct demon *demon, struct villager *target);
void draining_attack(struct demon *demon, struct villager *target);
void heavy_attack(struct demon *demon, struct villager *target);
void chase(struct demon *demon, struct villager *villager);
void update_demon_hp(struct demon *demon, int amount);
void demon_action(struct demon *demon, struct villager *villager);
#endif

Binary file not shown.

View File

@@ -0,0 +1,14 @@
#include <stdio.h>
#include "demon.h"
#include "villager.h"
#include "weapons.h"
int main()
{
char gun_name[50] = "BFG";
struct gun *gun = init_gun(gun_name, 50, FAR, 16);
printf("Gun name: %s\nDamage: %d\nRange: %d\nMagazine: %d/%d\n", gun->name, gun->damage, gun->range, gun->current_magazine, gun->magazine_size);
}

View File

@@ -0,0 +1,28 @@
#ifndef VILLAGER_H
#define VILLAGER_H
#include "weapons.h"
struct demon;
struct villager{
char name[50];
int cur_HP;
int HP_max;
// struct gun;
int gun_mastery;
int medicines;
enum distance distance;
};
struct villager *init_villager(char name[50], int HP_max, struct gun *gun, int gun_mastery);
void pp_villager(struct villager *villager);
void destroy_villager(struct villager *villager);
void update_villager_hp(struct villager *villager, int amount);
void shoot(struct villager *villager, struct demon *demon);
void heal(struct villager *villager);
enum distance walk(struct villager *villager, int direction);
int prepare_medicine(struct villager *villager);
#endif

View File

@@ -0,0 +1,17 @@
#include "weapons.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
struct gun *init_gun(char name[50], int damage, enum distance range, int magazine_size){
struct gun *rslt = malloc(sizeof( struct gun));
//mettre protection
//init des var du struct
strcpy(rslt->name, name);
rslt->damage = damage;
rslt->range = range;
rslt->magazine_size = magazine_size;
return rslt;
}

View File

@@ -0,0 +1,24 @@
#ifndef WEAPONS_H
#define WEAPONS_H
enum distance
{
CLOSE,
NEAR,
FAR,
};
struct gun {
char name[50];
int damage;
enum distance range;
int magazine_size;
int current_magazine;
};
struct gun *init_gun(char name[50], int damage, enum distance range, int magazine_size);
void reload(struct gun *gun);
void destroy_gun(struct gun *gun);
#endif