Mentions légales du service

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

Ajout des trois bugs de l'exercice (en #ifdef)

parent 38f94a44
No related branches found
No related tags found
No related merge requests found
......@@ -176,25 +176,11 @@ int collision(int y, int x) {
void touch(void)
{
int y, x;
player_in_dir_n(&y, &x, 1); /* talk at distance 2 */
map * m = current_map();
int i = coord_idx(y, x);
if (m->floor[i] == ON_SWI) {
printf("This switch is already activated.\n");
return;
} else if (!(m->floor[i] == OFF_SWI)) {
printf("There is no switch to touch in front of you.\n");
return;
}
m->floor[i] = ON_SWI;
i = swi_number(y, x);
printf("You activate switch %d\n", i);
char buffer[1];
sprintf(buffer, "%d", i);
strcat(played_order, buffer);
player_in_dir_n(&y, &x, 1);
// @AGDB this bug is just an inversion of x and y, but the result is funny
#ifdef inverse_xy
activate_switch(x, y);
#else
activate_switch(y, x);
#endif
}
......@@ -4,12 +4,6 @@
#include "agdbentures.h"
#include <stdlib.h>
// the width of a screen, in tiles
#define SCREEN_WIDTH 21
// the height of the screen, in tiles
#define SCREEN_HEIGHT 7
/**
* pos_x : current x position of the character
* pos_y : current y position of the character
......
......@@ -2,13 +2,13 @@
const char *str_map =
"\
+---------------+---+\n\
| X X | |\n\
| |---|\n\
| > D @\n\
| |---|\n\
| X X | |\n\
+---------------+---+\n\
+-----+--+\n\
|X X|~~|\n\
| +--+\n\
|> D @\n\
| +--+\n\
|X X|~~|\n\
+-----+--+\n\
";
// x position of the character
......@@ -20,14 +20,17 @@ int player_y = -1;
// Initial direction: facing East
int player_direction = 6;
// nomber of hit/health points
int health_points = 20;
// when true, the main loop ends. Should only be used in level_failed and level_success
bool exit_main_loop = false;
// @AGDB in the exercise we forget the +1 so the string doesn't end in \0 and played_order becomes corrupted
#ifdef string_comparison
const char correct_order[N_SWITCHES] = "2103";
char played_order[N_SWITCHES] = "";
#else
const char correct_order[N_SWITCHES+1] = "2103";
char played_order[N_SWITCHES+1] = "";
#endif
void level_failed(void)
{
......
......@@ -7,8 +7,11 @@
#include <assert.h>
#include <stdbool.h>
// the width of a screen, in tiles
#define SCREEN_WIDTH 10
#define max(a,b) (a>b?a:b)
// the height of the screen, in tiles
#define SCREEN_HEIGHT 7
// number of switches
#define N_SWITCHES 4
......@@ -22,16 +25,19 @@ extern int player_y;
// the direction the player is facing
extern int player_direction;
// the player's life
extern int health_points;
const char * str_map;
bool exit_main_loop;
// the order in which the switches are activated and the expected order
// @AGDB see common.c for explanation about this bug
#ifdef string_comparison
extern char played_order[N_SWITCHES];
const char correct_order[N_SWITCHES];
#else
extern char played_order[N_SWITCHES+1];
const char correct_order[N_SWITCHES+1];
#endif
void level_failed(void);
......
......@@ -20,8 +20,16 @@ int verify_switch_order() {
return 0;
}
// wrong order
/* @AGDB use strcmp instead of strncmp
and set the size of these strings to N_SWITCHES instead of N_SWITCHES+1
see common.c for more explanations
in the end the comparaison will never work even if the order is correct
*/
#ifdef string_comparison
if (strcmp(played_order, correct_order)) {
#else
if (strncmp(played_order, correct_order, 4)) {
#endif
return -1;
} else {
return 1;
......
......@@ -2,16 +2,16 @@ TURN_RIGHT
FORWARD
TOUCH
TURN_LEFT
FORWARD_N 12
FORWARD_N 4
TURN_LEFT
FORWARD_N 2
TOUCH
TURN_LEFT
FORWARD_N 12
FORWARD_N 4
TURN_RIGHT
TOUCH
TURN_RIGHT
FORWARD_N 12
FORWARD_N 4
TURN_RIGHT
FORWARD_N 2
TOUCH
......@@ -19,4 +19,4 @@ TURN_LEFT
TURN_LEFT
FORWARD
TURN_RIGHT
FORWARD_N 6
FORWARD_N 4
......@@ -2,6 +2,24 @@
static map_stack mstack;
#ifdef stack_alloc
interactable template_swi;
#endif
interactable * create_switch(int x, int y, int n) {
// @AGDB always return the same switch, so they all light up at once
#ifdef stack_alloc
interactable * swi = &template_swi;
#else
interactable * swi = malloc(sizeof(swi));
#endif
swi->x = x;
swi->y = y;
swi->state = DEACTIVATED;
swi->number = n;
return swi;
}
void init_map_stack(void)
{
mstack.length = 0;
......@@ -51,16 +69,18 @@ void load_map(const char * str_map, const char * new_name) {
m->name = (char*) malloc(sizeof(char) * strlen(new_name));
strcpy(m->name, new_name);
m->floor = (floortype*)malloc(m->width * m->height * sizeof(floortype));
m->inter = (interactable**) malloc(m->width * m->height * sizeof(interactable));
// we have 4 switches only
m->switches = malloc(sizeof(swi) * N_SWITCHES);
int nth_swi = 0;
ch = str_map;
floortype f;
interactable * i;
for (int y=0; y<m->height; y++) {
for (int x=0; x<m->width; x++) {
f = EMPTY;
i = NULL;
switch (*ch) {
case '+':
case '|':
......@@ -79,17 +99,16 @@ void load_map(const char * str_map, const char * new_name) {
m->exit_x = x;
m->exit_y = y;
break;
case 'X': f=OFF_SWI;
case 'X': f=WALL;
assert(nth_swi < 4);
m->switches[nth_swi].number = nth_swi;
m->switches[nth_swi].x = x;
m->switches[nth_swi].y = y;
i = create_switch(x, y, nth_swi);
nth_swi++;
break;
default:
fprintf (stderr, "Unknown map character '%c'!\n",*ch);
}
m->floor[y*m->width + x] = f;
m->inter[y*m->width+x] = i;
ch++;
}
assert (*ch == '\n');
......@@ -108,7 +127,7 @@ void unload_map (void)
free(m->floor);
free(m->name);
free(m->switches);
free(m->inter);
mstack.length--;
m = current_map();
......@@ -146,13 +165,23 @@ bool is_water(int y, int x)
return m->floor[coord_idx(y,x)] == WATER;
}
int swi_number(int y, int x) {
void activate_switch(int y, int x) {
map * m = current_map();
for (int i = 0; i < N_SWITCHES; i++) {
if (m->switches[i].x == x && m->switches[i].y == y)
return m->switches[i].number;
}
int i = coord_idx(y, x);
if (m->inter[i] == NULL) {
printf("There is no switch to touch in front of you.\n");
return;
} else if (m->inter[i]->state == ACTIVATED) {
printf("This switch is already activated.\n");
return;
}
m->inter[i]->state = ACTIVATED;
printf("You activate the %dth switch.\n", m->inter[i]->number);
return -1; // error, no switch found at x,y
char buffer[1];
sprintf(buffer, "%d", m->inter[i]->number);
strcat(played_order, buffer);
}
......@@ -3,6 +3,9 @@
#include "agdbentures.h"
#define ACTIVATED 1
#define DEACTIVATED 0
typedef enum {
EMPTY, /* unreachable area */
GRASS,
......@@ -18,11 +21,13 @@ typedef struct {
int x;
int y;
int number;
} swi;
int state;
} interactable;
typedef struct {
char * name; // shop, overworld, dungeon_Nth_level...
floortype *floor; /* matrix of floortype */
interactable ** inter;
int width; /* size of map */
int height;
int player_x; /* position of player */
......@@ -31,7 +36,6 @@ typedef struct {
int exit_y; /* position of the exit if any */
int door_x;
int door_y;
swi * switches;
} map;
#define MAX_MAPS 256
......@@ -55,7 +59,11 @@ bool is_obstacle(int y, int x);
bool is_water(int y, int x);
// returns the unique number associated to the switch in position (x,y)
int swi_number(int y, int x);
/**
* active l'interrupteur donné,
* ou affiche un message s'il n'y en a pas à cette position,
* ou affiche un message s'il est déjà activé.
*/
void activate_switch(int y, int x);
#endif//MAP_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