From 93a2c37a6c27a5fdd489da3cc494a5a4e69be104 Mon Sep 17 00:00:00 2001 From: Jeroen Boxhoorn Date: Thu, 30 Oct 2025 12:27:22 +0100 Subject: [PATCH] Fixed maze runner --- .../maze-runner/assets => assets}/maze.txt | 0 src/games/maze-runner/maze_runner.c | 29 +++++++++------- src/games/minigame-menu/minigame_menu.c | 34 +++++++++---------- 3 files changed, 32 insertions(+), 31 deletions(-) rename {src/games/maze-runner/assets => assets}/maze.txt (100%) diff --git a/src/games/maze-runner/assets/maze.txt b/assets/maze.txt similarity index 100% rename from src/games/maze-runner/assets/maze.txt rename to assets/maze.txt diff --git a/src/games/maze-runner/maze_runner.c b/src/games/maze-runner/maze_runner.c index 9927025..a72c513 100644 --- a/src/games/maze-runner/maze_runner.c +++ b/src/games/maze-runner/maze_runner.c @@ -44,26 +44,27 @@ static grid *get_maze(void) { /* Voert de benodigde veranderingen in het rooster door als de speler in een * bepaalde richting probeert te bewegen. * Input: - * gp : een pointer naar het rooster - * dx,dy: de richting waarin de speler probeert te bewegen. De vier mogelijk- - * heden voor (dx,dy) zijn (-1,0), (1,0), (0,-1), (0,1) voor resp. - * links, rechts, omhoog en omlaag. + * gp: een pointer naar het rooster + * offset_vector: de richting waarin de speler probeert te bewegen. De vier mogelijk- + * heden voor (dx,dy) zijn (-1,0), (1,0), (0,-1), (0,1) voor resp. + * links, rechts, omhoog en omlaag. * * Side effect: het rooster wordt aangepast op basis van de handeling van * de speler. */ -static void maze_runner_move(grid *gp, const int dx, const int dy) { +static void maze_runner_move(grid *gp, const coordinate offset_vector) { const coordinate player_position = grid_find(gp, LIVING_PLAYER); if (player_position.y == -1) { printf("Player not found!"); + // State begin for the hackerman ending screen. grid_put_state(gp, STATE_BEGIN); return; } coordinate new_player_position = player_position; - new_player_position.x += dx; - new_player_position.y += dy; + new_player_position.x += offset_vector.x; + new_player_position.y += offset_vector.y; if (grid_contains(gp, new_player_position) == 1) { const char new_location = grid_fetch(gp, new_player_position); @@ -79,7 +80,7 @@ static void maze_runner_move(grid *gp, const int dx, const int dy) { break; case EMPTY: update_grid(gp, EMPTY, player_position); - update_grid(gp, LIVING_PLAYER, player_position); + update_grid(gp, LIVING_PLAYER, new_player_position); break; case MAZE_EXIT: update_grid(gp, EMPTY, player_position); @@ -101,33 +102,35 @@ static void maze_runner_move(grid *gp, const int dx, const int dy) { * Het rooster wordt ge-updated afhankelijk van de user input. */ static void speel_maze(grid *gp) { + coordinate offset_vector = {.x = 0, .y = 0}; switch (getch()) { case KEY_UP: // fallthrough case 'w': case 'W': - maze_runner_move(gp, 0, -1); + offset_vector.y--; break; case KEY_DOWN: // fallthrough case 's': case 'S': - maze_runner_move(gp, 0, 1); + offset_vector.y++; break; case KEY_LEFT: // fallthrough case 'a': case 'A': - maze_runner_move(gp, -1, 0); + offset_vector.x--; break; case KEY_RIGHT: // fallthrough case 'd': case 'D': - maze_runner_move(gp, 1, 0); + offset_vector.x++; break; case 'p': case KEY_BACKSPACE: case KEY_ESCAPE: grid_put_state(gp, STATE_QUIT); - break; + return; } + maze_runner_move(gp, offset_vector); } void maze_runner(void) { diff --git a/src/games/minigame-menu/minigame_menu.c b/src/games/minigame-menu/minigame_menu.c index abb3e62..79850b7 100644 --- a/src/games/minigame-menu/minigame_menu.c +++ b/src/games/minigame-menu/minigame_menu.c @@ -23,6 +23,7 @@ typedef enum { GAME_QUIT = 4, } game; +static grid *MENU; static game SELECTED_GAME = GAME_MANUAL; static coordinate OFFSET = {.x = 5, .y = 5}; @@ -34,6 +35,7 @@ static coordinate OFFSET = {.x = 5, .y = 5}; * game: The game you want to launch. */ static void launch_game(const game game) { + clear(); switch (game) { case GAME_MANUAL: manual((coordinate){0,0}); @@ -88,10 +90,10 @@ static void menu_highlight(const grid *menu) { * Side Effects: * Displays the menu */ -static void show_menu(const grid *menu) { +static void show_menu() { erase(); - show_grid_on_offset(menu, OFFSET); - menu_highlight(menu); + show_grid_on_offset(MENU, OFFSET); + menu_highlight(MENU); refresh(); } @@ -121,7 +123,6 @@ static void menu_move(const int offset) { * 0: Continue running. * 1: Exit the menu. * - * * Side Effect: * Changes the SELECTED_GAME as needed and launches selected games on select. */ @@ -156,28 +157,25 @@ static int navigate_menu(void) { /* * Create the menu grid. - * - * Output: - * A pointer to the menu grid. */ -static grid *initialize_menu(void) { - char menu[] = "How to play\n" - "Maze Runner\n" - " Snake \n" - "Minesweeper\n" - " Leave \n"; - grid *rp = grid_create_from_string(menu); - return rp; +static void initialize_menu(void) { + const char menu[] = "How to play\n" + "Maze Runner\n" + " Snake \n" + "Minesweeper\n" + " Leave \n"; + MENU = grid_create_from_string(menu); } void minigame_menu(void) { - grid *menu = initialize_menu(); + initialize_menu(); + while (true) { + show_menu(); if (navigate_menu() == 1) { - show_menu(menu); break; } } - grid_cleanup(menu); + grid_cleanup(MENU); }