Mentions légales du service

Skip to content
Snippets Groups Projects
Commit ab715ab0 authored by SZCZEPANSKI Marin's avatar SZCZEPANSKI Marin
Browse files

delete uselesss files

parent 64837cc6
No related branches found
No related tags found
No related merge requests found
#include "array_seq.h"
sequence* create_sequence (void)
{
/* BUGMULTILINE: not creating the malloc, returning local address */
sequence* seq = (sequence*)malloc(sizeof(sequence));
seq->length = 0;
return seq;
/* BUGALTERNATE
* sequence seq;
* seq.length = 0;
* return &seq;
*/
}
void seq_pp (sequence* seq)
{
printf ("< ");
for (int i=0; i < seq->length-1; i++) {
printf ("%d, ", seq->arr[i]);
}
if (seq->length > 0) {
/* BUGLINEDELETEMORE: forgetting to check there is at least one element */
printf ("%d ", seq->arr[seq->length-1]);
}
/* BUGALSOREMOVE */
printf (">\n");
}
void seq_add_first (sequence* seq, item i)
{
assert (seq->length < LMAX);
for (int i=seq->length; i>0; i--) {
/* BUGLINE: doing the moves in the wrong ordering
* for (int i=0; i<seq->length; i++) {
*/
seq->arr[i] = seq->arr[i-1];
}
seq->arr[0] = i;
seq->length++;
/* BUGLINEDELETE: forgetting to increase length of sequence */
}
void seq_add_last (sequence* seq, item i)
{
assert (seq->length < LMAX);
seq->arr[seq->length] = i;
seq->length++;
}
void seq_add (sequence* seq, item i)
{
seq_add_last (seq, i);
}
void seq_remove_idx (sequence* seq, int idx)
{
assert (idx >= 0 && idx < seq->length);
for (int i=idx; i<seq->length-1; i++) {
/* BUGLINE: going to far in the list, but bug will probably be silent
* for (int i=idx; i<seq->length; i++) {
*/
seq->arr[i] = seq->arr[i+1];
}
seq->length--;
}
int seq_search (sequence* seq, item i)
{
/* search the item */
int idx=0;
for (; idx<seq->length; idx++) {
if (seq->arr[idx] == i) return idx;
}
return -1;
}
void seq_remove (sequence* seq, item i)
{
int idx = seq_search (seq, i);
if (idx != -1) {
/* BUGLINEDELETEMORE: forgetting to check if item belongs to sequence */
seq_remove_idx (seq, idx);
}
/* BUGALSOREMOVE */
}
bool seq_contains (sequence* seq, item i)
{
return seq_search (seq, i) != -1;
}
#ifndef ARRAY_SEQ_H
#define ARRAY_SEQ_H
/*************************************************/
/* Library of sets and sequences based on arrays */
/*************************************************/
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define LMAX 256
typedef int item;
struct sequence_s {
item arr[LMAX];
unsigned int length;
};
typedef struct sequence_s sequence;
sequence* create_sequence (void);
void seq_pp (sequence *seq); /* pretty printer for a sequence */
void seq_add_first (sequence *seq, item i);
void seq_add_last (sequence *seq, item i);
void seq_add (sequence *seq, item i);
void seq_remove (sequence *seq, item i);
void seq_remove_idx (sequence *seq, int index);
bool seq_contains (sequence *seq, item i);
#endif//ARRAY_SEQ_H
/*
* Triggers and resolutions related to items.
*/
#include "events_player.h"
int walks_on_item() {
int x = player_x;
int y = player_y;
map * cmap = current_map();
// quick check to avoid useless looping
if (item(y, x) == NO_ITEM) {
return 0;
}
/* we loop over the possible items until we see one for which the character
corresponds to where the player is standing.
*/
int i = 0;
while (i < MAX_POSSIBLE_ITEMS) {
if (list[i]->code == item(y, x)) {
/* we found an item.
My first algo teacher would say it's very bad to exist a loop
like this, but this whole code is for educational purposes,
isn't it?
*/
return 1;
}
i++;
}
// nothing found
return 0;
}
void pick_item(int trig) {
// here the trig parameter is not used
/* How to add an item to the inventory, knowing some are stackable?
1. if item of this type and space to stack = stack
2. if there is a free space = new instance
3. if no item of this type and no place in inventory = exit
Goal: we shouldn't have two instances with stack space left
*/
int x = player_x;
int y = player_y;
map * cmap = current_map();
/* because the trigger function was called before, we are guaranteed
that the player is currently on an item
*/
itemtype item_code = item(y, x);
if (add_item_to_inventory(item_code)) {
cmap->items[y * cmap->width + x] = NO_ITEM;
}
}
int death_imminent() {
return (health_points < 1);
}
void death(int trig) {
printf("You die.\n");
level_failed();
}
#pragma once
#include "common.h"
#include "map.h"
#include "items.h"
// Trigger function: returns 1 if the player is standing on an item, 0 otherwise
int walks_on_item();
// Resolution function: picks the item the player is walking on and adds it to their inventory
void pick_item(int trig);
int death_imminent();
void death(int trig);
/**
* A way to model an inventory. Each possible item only exists once,
* and the player has "intances" of items.
*
* Concepts used:
* - malloc and free
* - pointers and structures, pointers of pointers
*
* Possible bugs:
* - pointers, double pointers, triple pointers in structures
* - malloc, not freeing correctly
* - misusing the functions of string.h and stdio.h
* - reading an incorrect items.txt
*
* Game design related tip:
* Inventories exist in many types of games, but the most complex
* are those of RPGs.
*/
#include "common.h"
/***********************************/
/* Methods and functions */
/***********************************/
void load_items() {
FILE * fp;
fp = fopen("items.txt", "r");
if (fp == NULL) {
fprintf(stderr, "Could not find or open items.txt.\n");
exit(EXIT_FAILURE);
}
/* for each item, we read:
- name
- description
- character code (for the ASCII map)
- stack limit
- properties
- a blank line or EOF
We assume the file items.txt is correct! */
char buffer[MAX_LINE_LENGTH];
int i = 0;
while (fgets(buffer, MAX_LINE_LENGTH, fp) != NULL && i < MAX_POSSIBLE_ITEMS) {
list[i] = (type *) malloc(sizeof(type));
// name
list[i]->name = malloc(sizeof(char) * MAX_LINE_LENGTH);
strcpy(list[i]->name, buffer);
// description
fgets(buffer, MAX_LINE_LENGTH, fp);
list[i]->description = malloc(sizeof(char) * MAX_LINE_LENGTH);
strcpy(list[i]->description, buffer);
// character code
fgets(buffer, MAX_LINE_LENGTH, fp);
list[i]->code = atoi(buffer);
// stack limit
fgets(buffer, MAX_LINE_LENGTH, fp);
list[i]->stack_limit = atoi(buffer);
// properties (can be zero or more)
list[i]->properties = malloc(sizeof(char *) * MAX_POSSIBLE_ITEMS);
int j = 0;
fgets(buffer, MAX_LINE_LENGTH, fp);
if (feof(fp)) {
list[i]->properties[j] = NULL;
j = MAX_POSSIBLE_ITEMS;
}
// while we don't encounter a blank line
while (fp != NULL && strncmp(buffer, "\n", 1) && j < MAX_POSSIBLE_ITEMS) {
list[i]->properties[j] = malloc(sizeof(char) * MAX_LINE_LENGTH);
strcpy(list[i]->properties[j], buffer); // possible bug: strcpy(buffer, list[i]->properties[j]);
j++;
fgets(buffer, MAX_LINE_LENGTH, fp);
if (feof(fp)) {
if (j < MAX_POSSIBLE_ITEMS) {
list[i]->properties[j] = NULL;
}
j = MAX_POSSIBLE_ITEMS;
}
}
if (j < MAX_POSSIBLE_ITEMS) {
list[i]->properties[j] = NULL;
}
i++;
}
if (i < MAX_POSSIBLE_ITEMS) {
list[i] = NULL;
}
fclose(fp);
}
void print_items_list() {
int i = 0;
int j;
while (list[i] != NULL && i < MAX_POSSIBLE_ITEMS) {
// name
printf("\nItem, name : %s", list[i]->name);
// description
printf("\tDescription: %s", list[i]->description);
// ASCII code
printf("\tCode: %c\n", list[i]->code);
// stack limit
printf("\tStack limit: %d\n", list[i]->stack_limit);
// properties
j = 0;
while (list[i]->properties[j] != NULL && j < MAX_POSSIBLE_ITEMS) {
printf("\tProperty %d : %s", j+1, list[i]->properties[j]);
j++;
}
i++;
}
printf("\n");
}
void free_list() {
int i = 0;
int j;
while (list[i] != NULL && i < MAX_POSSIBLE_ITEMS) {
// name
free(list[i]->name);
// description
free(list[i]->description);
// stack limit was not allocated
// properties
j = 0;
while (list[i]->properties[j] != NULL && j < MAX_POSSIBLE_ITEMS) {
free(list[i]->properties[j]);
j++;
}
free(list[i]->properties);
free(list[i]);
i++;
}
}
unsigned get_item_count(itemtype it) {
unsigned count = 0;
for (int i = 0; i < INVENTORY_SIZE; i++) {
if (inventory[i].type != NULL && inventory[i].type->code == it)
count = count + inventory[i].stack;
}
return count;
}
void set_item_count(itemtype it, unsigned count) {
int diff = get_item_count(it) - count;
int i = 0;
if (diff < 0) {
for (int i = 0; i < diff; i++)
add_item_to_inventory(it);
}
if (diff > 0) {
for (int i = 0; i < diff; i++)
remove_item_from_inventory(it);
}
}
bool add_item_to_inventory(itemtype item_code) {
int i = 0;
bool item_picked = false;
// 1. if item of this type and space to stack = stack
while (i < INVENTORY_SIZE && !item_picked) {
if (inventory[i].type != NULL
&& inventory[i].type->code == item_code
&& inventory[i].stack < inventory[i].type->stack_limit) {
inventory[i].stack++;
item_picked = true;
}
i++;
}
// 2. if there is a free space = new instance
// BUGLINE: item_picked = 0; (resetting the wrong variable)
i = 0;
while (i < INVENTORY_SIZE && !item_picked) {
/* we choose to represent a free inventory space as
an item with type NULL
*/
if (inventory[i].type == NULL) {
inventory[i].stack = 1;
/* now we need to set the type. The trigger function guanrantees
the item is valid
*/
int j = 0;
while (j < MAX_POSSIBLE_ITEMS) {
if (list[j]->code == item_code) {
inventory[i].type = list[j];
j = MAX_POSSIBLE_ITEMS;
}
j++;
}
item_picked = true;
}
i++;
}
/* if item_picked == 0 here, it means we have no space for the item
in which case we do nothing and the item stays on the map */
return item_picked;
}
bool remove_item_from_inventory(itemtype it) {
bool item_removed = false;
int i = 0;
while (i < INVENTORY_SIZE && !item_removed) {
if (inventory[i].type != NULL && inventory[i].type->code == it) {
inventory[i].stack--;
if (inventory[i].stack < 1) {
inventory[i].type = NULL;
}
item_removed = true;
}
}
return item_removed;
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
NO_ITEM,
I_GOLD,
ITEMS_N,
} itemtype;
/* The maximum number of different items to be defined,
and the maximum number of properties a type of item can hold. */
#define MAX_POSSIBLE_ITEMS 20
/* The maximum number of characters a line from items.txt can have. */
#define MAX_LINE_LENGTH 500
/* The common structure to any item */
typedef struct item_type {
// name of this item
char * name;
// a short description
char * description;
/* Code representing the item on the map
must correspond to the definition of the item in itemtype */
int code;
/* pointer to any properties constant and shared by all instances of an item
e.g. attack for a sword, health restored for a healing potion */
char ** properties;
/* How many instances of this type of item can be stacked in one inventory slot */
unsigned stack_limit;
} type;
typedef struct item_instance {
// type of the item
type * type;
// number of stacked items in this instance (must always be <= stack_limit)
unsigned stack;
} instance;
/* In this simplified example, the possible items are stored in a text file (items.txt).
This function loads the items in memory. */
void load_items() ;
/* Prints all items known in memory. */
void print_items_list();
/* Frees every allocated memory of the list given in argument,
including the list itself.*/
void free_list();
/* Returns the total number of items of this type in the inventory */
unsigned get_item_count(itemtype it);
/* Sets the total number of items of a type in the inventory */
void set_item_count(itemtype it, unsigned count);
/* Tries to add an item in the inventory. Returns true on success, false if there was no place */
bool add_item_to_inventory(itemtype it);
/* Tries to remove an instance of an item in the inventory.
Return true on success, false if the item was not found in the inventory */
bool remove_item_from_inventory(itemtype it);
Gold Coin
It's shiny! Allows you to buy equipment.
1
100
#include "linkedlist_seq.h"
list* ll_create (void)
{
list* l = (list*)malloc(sizeof(list));
return l;
}
cell* create_cell(void)
{
cell* cel = (cell*)malloc(sizeof(cell));
/* BUGLINE: allocating the size of a pointer instead of the structure
* cell* cel = (cell*)malloc(sizeof(*cell));
*/
return cel;
}
void list_pp (list *l) {
cell *c;
c = l->head;
if (!c) {
printf ("<empty>\n");
return;
}
while (c->next!=NULL) {
printf("%d, ",c->value);
c = c->next;
}
printf("%d\n",c->value);
}
int ll_length (list *l)
{
int len=0;
cell* cel = l->head;
while (cel) {
/* BUGLINEMORE: forgetting to add brackets
* while (cel)
* */
len++;
cel = cel->next;
}
/* BUGALSOREMOVE */
return len;
}
/* BUGIDEA: passing a list instead of list* as parameter */
/* BUGIDEA: having cell as a local variable */
void ll_add_first (list *l, item i) {
cell* cel = create_cell();
cel->value = i;
cel->next = l->head;
l->head = cel;
}
void ll_add_last (list *l, item i) {
if (!l->head) {
ll_add_first (l, i);
return;
/* BUGLINEDELETE: forgetting the return, hence adding the first item twice. */
}
cell* tail = l->head;
while (tail->next) { tail = tail->next; }
tail->next = create_cell();
tail->next->value = i;
tail->next->next = NULL;
}
void ll_add (list *l, item i)
{
ll_add_first(l,i);
}
cell* generic_search (list *l, item i, cell** prec)
{
cell* cel = l->head;
*prec = NULL;
while (cel && cel->value != i) {
*prec = NULL;
cel = cel->next;
}
return cel; /* will be non null if item i found */
}
void ll_remove (list *l, item i)
{
cell *prec;
cell *cel = generic_search(l, i, &prec);
if (!cel) return;
if (!prec) {
l->head = cel->next;
} else {
prec->next = cel->next;
}
free(cel);
/* BUGLINEDELETE: memory leak */
}
bool ll_contains (list *l, item i)
{
cell* useless;
return generic_search(l, i, &useless) != NULL;
}
#ifndef LINKEDLIST_SEQ_H
#define LINKEDLIST_SEQ_H
/************************************************************/
/* Library for sequences of integers based on linked lists */
/************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef int item;
struct cell_s {
item value;
struct cell_s *next;
};
typedef struct cell_s cell;
struct list_s {
cell *head;
};
typedef struct list_s list;
list* ll_create (void);
void list_pp (list *l); /* pretty printer for a linked list */
int ll_length (list *l);
void ll_add_first (list *l, item i);
void ll_add_last (list *l, item i);
void ll_add (list *l, item i);
void ll_remove (list *l, item i);
bool ll_contains (list *l, item i);
#endif//LINKEDLIST_SEQ_H
#include "maze.h"
void place_vertical_wall(square_t **walls, int x, int y, int height) {
printf("place wall v x:%d y:%d l:%d\n", x, y, height);
// JAI ECRIT CA LOL CA MARCHE QUE QUAND CEST 0 JPP for(int j=y; j<height; j++) {
for(int j=0; j<height; j++) {
//walls[x][y] = 1; INVERSION ROW-COLUMN MAJOR
walls[x][y+j].vertical_wall = 1;
}
display_maze_internal(walls, 6, 5);
}
void place_horizontal_wall(square_t **walls, int x, int y, int width) {
printf("place wall h x:%d y:%d l:%d\n", x, y, width);
// PAREIL QUE AU DESSUS MAIS MDR
for(int i=0; i<width; i++) {
//walls[i][y] = 1; INVERSION VERTICAL HORIZONTAL
//walls[x][y] = 1; INVERSION ROW-COLUMN MAJOR
walls[x+i][y].horizontal_wall = 1;
}
display_maze_internal(walls, 6, 5);
}
void place_horizontal_door(square_t **walls, int x, int y, int width) {
int door_pos = rand() % width;
//walls[x+door_pos][y] = 0; INVERSION VERTICAL HORIZONTAL
//walls[y+door_pos][x] = 0; INVERSION ROW-COLUMN MAJOR
walls[x+door_pos][y].horizontal_wall = 0;
}
void place_vertical_door(square_t **walls, int x, int y, int height) {
int door_pos = rand() % height;
//walls[x][y+door_pos] = 0; INVERSION VERTICAL HORIZONTAL LINVERSE EN FAIT
walls[x][y+door_pos].vertical_wall = 0;
}
void divide_chamber(square_t **walls, int x, int y, int width, int height) {
// Stop when we have a corridor
// IL FAUT METTRE <= et pas ==
printf("DIVIDE x:%d y:%d width:%d height:%d\n", x, y, width, height);
if (width <= 1 || height <= 1) {
return;
}
// Choose side to divide
if (width >= height) {
// Place vertical wall
int vertical_wall = x + 1 + rand() % (width-1);
printf("v: %d x: %d y: %d\n", vertical_wall, x, y);
place_vertical_wall(walls, vertical_wall, y, height);
place_vertical_door(walls, vertical_wall, y, height);
divide_chamber(walls, x, y, vertical_wall-x, height);
divide_chamber(walls, vertical_wall, y, width-vertical_wall, height);
} else {
// Place horizontal wall
int horizontal_wall = y + 1 + rand() % (height-1);
printf("h: %d x: %d y: %d\n", horizontal_wall, x, y);
place_horizontal_wall(walls, x, horizontal_wall, width);
place_horizontal_door(walls, x, horizontal_wall, width);
divide_chamber(walls, x, y, width, horizontal_wall-y);
divide_chamber(walls, x, horizontal_wall, width, height-horizontal_wall);
}
display_maze_internal(walls, 6, 5);
}
maze_t generate_maze(int width, int height) {
// Walls allocation, at the beginning all walls are empty
maze_t maze;
maze.width = width+1;
maze.height = height+1;
maze.walls = malloc(sizeof(square_t*) * maze.width);
square_t** walls = maze.walls;
for(int i=0; i < maze.width; i++) { // WIDTH AU LIEU DE MAZE.WIDTH
maze.walls[i] = calloc(maze.height, sizeof(square_t));
}
// Enclose the maze
// ICI JAVAIS FAIT UN BUG DE FOR NESTED WALLS[i][j] = 1
place_horizontal_wall(walls, 0, 0, width);
place_horizontal_wall(walls, 0, height, width); // WIDTH AU LIEU DE HEIGHT
place_vertical_wall(walls, 0, 0, height);
place_vertical_wall(walls, width, 0, height);
display_maze(maze);
// Recursively divide the chambers
divide_chamber(walls, 0, 0, maze.width-1, maze.height-1);
// Place the two exits
// TODO
return maze;
}
void display_maze_internal(square_t** walls, int width, int height) {
for(int j=0; j < height; j++) {
for(int i=0; i < width; i++) {
/*
if (walls[i][j].horizontal_wall) {
if (walls[i][j].vertical_wall) {
printf("+");
} else {
printf("-");
}
} else if (walls[i][j].vertical_wall) {
printf("|");
} else {
printf(".");
}
*/
printf("%d %d, ", walls[i][j].vertical_wall, walls[i][j].horizontal_wall);
}
printf("\n");
}
printf("\n");
}
void display_maze(maze_t maze) {
display_maze_internal(maze.walls, maze.width, maze.height);
}
#ifndef MAZE_H
#define MAZE_H
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int horizontal_wall;
int vertical_wall;
} square_t;
typedef struct {
int width;
int height;
square_t **walls; // an 2d array of walls in a column major way
} maze_t;
// Allocate a new generate maze from the given size
maze_t generate_maze(int width, int height);
void display_maze_internal(square_t** walls, int width, int height);
void display_maze(maze_t maze);
#endif
#include "monster.h"
void talk_monster(monster *mon)
{
if (strcmp (mon->name, "Shopkeeper")) {
printf("You cannot talk to a %s.\n", mon->name);
return;
}
printf("Welcome to my humble shop traveler.\nDo you want to buy something?\n");
return;
}
void reduce_health(int dmg)
{
health_points = max(0, health_points-dmg);
printf("Your health points are now %d.\n", health_points);
}
void receive_damage(monster *mon)
{
int dmg = mon->weapon;
printf("You receive %d damage.\n", dmg); /* BUGIDEA: forgetting the 'dmg' argument */
reduce_health (dmg);
}
/* returns if monster is defeated, otherwise level_failed will be called */
void fight_monster(monster *mon, equipment eq)
{
int dmg;
/* fight until player or monster dies */
while (mon->health_points > 0 && health_points > 0) {
/* attack */
dmg = max(0, eq.weapon - mon->armor);
printf ("You inflict %d damage to the %s.\n", dmg, mon->name);
mon->health_points -= dmg;
/* BUGIDEA: forgetting to remove damage to the monster */
if (mon->health_points > 0) {
/* retaliates */
dmg = max(0, mon->weapon - eq.armor);
printf ("The %s inflicts %d damage to you.\n", mon->name, dmg);
reduce_health (dmg);
}
}
/* monster dies */
if (mon->health_points <= 0) {
printf ("The %s dies.\n", mon->name);
}
/* si le joueur est mort alors l'échec sera vu au prochain apply_event,
une fois revenu dans la main loop */
}
#ifndef MONSTER_H
#define MONSTER_H
#include "common.h"
typedef struct {
int weapon;
int armor;
} equipment;
typedef struct {
int pos_y, pos_x;
int health_points;
int armor;
int weapon;
char *name;
} monster;
void talk_monster(monster* mon);
void fight_monster(monster*, equipment);
#endif//MONSTER_H
#include "shop.h"
int gold=0;
const int weapon_price[WEAPONS_N] = {0, 1, 2, 5};
const int armor_price[ARMORS_N] = {0, 2, 4, 8};
int buy_generic(const int price[], int num_prices)
{
gold = get_item_count(I_GOLD);
int idx=num_prices-1;
while (idx > 0 && price[idx] > gold) { idx--; };
gold = gold - price[idx];
if (!idx) {
printf ("You do not have enough gold for that.\n");
} else {
printf ("You buy something for %d gold.\n",price[idx]);
printf ("You now have %d gold.\n",gold);
set_item_count(I_GOLD, gold);
}
return idx;
}
weapon_type buy_weapon(void)
{
printf("You try to buy a weapon...\n");
int widx = buy_generic (weapon_price, WEAPONS_N);
return (weapon_type)widx;
}
armor_type buy_armor(void)
{
printf("You try to buy armor...\n");
int aidx = buy_generic (armor_price, ARMORS_N);
return (armor_type)aidx;
}
#ifndef SHOP_H
#define SHOP_H
#include "common.h"
typedef enum {
NO_WEAPON,
DAGGER,
SHORT_SWORD,
LONG_SWORD,
WEAPONS_N
} weapon_type;
typedef enum {
NO_ARMOR,
LEATHER_JACKET,
CHAINMAIL,
FULLPLATE,
ARMORS_N
} armor_type;
extern int gold;
weapon_type buy_weapon(void);
armor_type buy_armor(void);
#endif//SHOP_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment