From 54dedac802cea26b76de1e1b8b52fd1f273e2852 Mon Sep 17 00:00:00 2001 From: mszczepa <marin.szczepanski@inria.fr> Date: Wed, 18 May 2022 10:58:28 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9placements=20up/down/left/right=20dans?= =?UTF-8?q?=20fullgame?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fullgame/Makefile | 4 +++- fullgame/action.c | 55 +++++++++++++++++++++++++++++++++++++++------- fullgame/action.h | 19 +++++++++++++--- fullgame/input.txt | 20 +++++------------ fullgame/main.c | 24 +++++++++++++------- 5 files changed, 87 insertions(+), 35 deletions(-) diff --git a/fullgame/Makefile b/fullgame/Makefile index afe6eca1..513969be 100644 --- a/fullgame/Makefile +++ b/fullgame/Makefile @@ -4,10 +4,12 @@ OBJECTS := items.o map.o action.o shop.o monster.o common.o events.o events_map. CC := gcc CFLAGS := -g +DEFINES := -Dshow_solution + all: main main: $(OBJECTS) main.c - $(CC) $(CFLAGS) -o $@ main.c $(OBJECTS) + $(CC) $(CFLAGS) -o $@ main.c $(OBJECTS) $(DEFINES) clean: rm *.o diff --git a/fullgame/action.c b/fullgame/action.c index b8f852da..1e162d54 100644 --- a/fullgame/action.c +++ b/fullgame/action.c @@ -33,10 +33,10 @@ void turn_right (void) { int d; switch (player_direction) { - case 8: d=6; break; - case 4: d=8; break; - case 2: d=4; break; - case 6: d=2; break; + case UP: d=RIGHT; break; + case LEFT: d=UP; break; + case DOWN: d=LEFT; break; + case RIGHT: d=DOWN; break; default: assert(false); } player_direction = d; @@ -46,10 +46,10 @@ void turn_left (void) { int d; switch (player_direction) { - case 8: d=4; break; - case 4: d=2; break; - case 2: d=6; break; - case 6: d=8; break; + case UP: d=LEFT; break; + case LEFT: d=DOWN; break; + case DOWN: d=RIGHT; break; + case RIGHT: d=UP; break; default: assert(false); } player_direction = d; @@ -120,6 +120,45 @@ int player_idx_in_dir_n(int n) return coord_idx (y, x); } +void up() { + player_direction = UP; + forward(); +} + +void up_n(unsigned n) { + player_direction = UP; + forward_n(n); +} + +void down() { + player_direction = DOWN; + forward(); +} + +void down_n(unsigned n) { + player_direction = DOWN; + forward_n(n); +} + +void left() { + player_direction = LEFT; + forward(); +} + +void left_n(unsigned n) { + player_direction = LEFT; + forward_n(n); +} + +void right() { + player_direction = RIGHT; + forward(); +} + +void right_n(unsigned n) { + player_direction = RIGHT; + forward_n(n); +} void forward (void) diff --git a/fullgame/action.h b/fullgame/action.h index dc3cd8c6..c6c63fc0 100644 --- a/fullgame/action.h +++ b/fullgame/action.h @@ -9,6 +9,11 @@ // the height of the screen, in tiles #define SCREEN_HEIGHT 8 +#define UP 8 +#define DOWN 2 +#define LEFT 4 +#define RIGHT 6 + /** * pos_x : current x position of the character * pos_y : current y position of the character @@ -32,8 +37,18 @@ void coord_in_dir (int y, int x, int dir, int *cy, int *cx); */ void coord_in_dir_n (int y, int x, int dir, int *cy, int *cx, int n); +// Functions to move the character +void up(); +void up_n(unsigned n); +void down(); +void down_n(unsigned n); +void left(); +void left_n(unsigned n); +void right(); +void right_n(unsigned n); + void forward (void); -void forward_n (unsigned int); +void forward_n (unsigned num_steps); /** * Verifies if the player can move to tile (x, y). @@ -44,8 +59,6 @@ void forward_n (unsigned int); */ int collision(int x, int y); - - void talk(void); void fight(equipment eq); diff --git a/fullgame/input.txt b/fullgame/input.txt index d7b0fe76..cd8323e1 100644 --- a/fullgame/input.txt +++ b/fullgame/input.txt @@ -1,19 +1,9 @@ -FORWARD_N 18 -TURN_LEFT -FORWARD -FORWARD -FORWARD -FORWARD +RIGHT_N 18 +UP_N 4 TALK BUY_WEAPON BUY_ARMOR -TURN_RIGHT -TURN_RIGHT -FORWARD -FORWARD -FORWARD -FORWARD -TURN_LEFT -FORWARD_N 27 +DOWN_N 4 +RIGHT_N 27 FIGHT -FORWARD_N 10 +RIGHT_N 10 diff --git a/fullgame/main.c b/fullgame/main.c index 1b157849..9015359f 100644 --- a/fullgame/main.c +++ b/fullgame/main.c @@ -68,14 +68,22 @@ void apply_input(Command * c, equipment * eq) { #else void apply_input(Command * c, equipment eq) { #endif - if (!strcmp(c->command_buffer, "FORWARD")) { - forward(); - } else if (!strcmp(c->command_buffer, "FORWARD_N")) { - forward_n(atoi(c->args[0])); - } else if (!strcmp(c->command_buffer, "TURN_LEFT")) { - turn_left(); - } else if (!strcmp(c->command_buffer, "TURN_RIGHT")) { - turn_right(); + if (!strcmp(c->command_buffer, "UP")) { + up(); + } else if (!strcmp(c->command_buffer, "UP_N")) { + up_n(atoi(c->args[0])); + } else if (!strcmp(c->command_buffer, "DOWN")) { + down(); + } else if (!strcmp(c->command_buffer, "DOWN_N")) { + down_n(atoi(c->args[0])); + } else if (!strcmp(c->command_buffer, "LEFT")) { + left(); + } else if (!strcmp(c->command_buffer, "LEFT_N")) { + left_n(atoi(c->args[0])); + } else if (!strcmp(c->command_buffer, "RIGHT")) { + right(); + } else if (!strcmp(c->command_buffer, "RIGHT_N")) { + right_n(atoi(c->args[0])); } else if (!strcmp(c->command_buffer, "TALK")) { talk(); } else if (!strcmp(c->command_buffer, "BUY_WEAPON")) { -- GitLab