Fixed maze runner
This commit is contained in:
		| @@ -44,26 +44,27 @@ static grid *get_maze(void) { | |||||||
| /* Voert de benodigde veranderingen in het rooster door als de speler in een | /* Voert de benodigde veranderingen in het rooster door als de speler in een | ||||||
|  * bepaalde richting probeert te bewegen. |  * bepaalde richting probeert te bewegen. | ||||||
|  * Input: |  * Input: | ||||||
|  * gp   : een pointer naar het rooster |  * gp: een pointer naar het rooster | ||||||
|  * dx,dy: de richting waarin de speler probeert te bewegen. De vier mogelijk- |  * 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. |  *                heden voor (dx,dy) zijn (-1,0), (1,0), (0,-1), (0,1) voor resp. | ||||||
|  *                links, rechts, omhoog en omlaag. |  *                links, rechts, omhoog en omlaag. | ||||||
|  * |  * | ||||||
|  * Side effect: het rooster wordt aangepast op basis van de handeling van |  * Side effect: het rooster wordt aangepast op basis van de handeling van | ||||||
|  *              de speler. |  *              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); |     const coordinate player_position = grid_find(gp, LIVING_PLAYER); | ||||||
|  |  | ||||||
|     if (player_position.y == -1) { |     if (player_position.y == -1) { | ||||||
|         printf("Player not found!"); |         printf("Player not found!"); | ||||||
|  |         // State begin for the hackerman ending screen. | ||||||
|         grid_put_state(gp, STATE_BEGIN); |         grid_put_state(gp, STATE_BEGIN); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     coordinate new_player_position = player_position; |     coordinate new_player_position = player_position; | ||||||
|     new_player_position.x += dx; |     new_player_position.x += offset_vector.x; | ||||||
|     new_player_position.y += dy; |     new_player_position.y += offset_vector.y; | ||||||
|  |  | ||||||
|     if (grid_contains(gp, new_player_position) == 1) { |     if (grid_contains(gp, new_player_position) == 1) { | ||||||
|         const char new_location = grid_fetch(gp, new_player_position); |         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; |                 break; | ||||||
|             case EMPTY: |             case EMPTY: | ||||||
|                 update_grid(gp, EMPTY, player_position); |                 update_grid(gp, EMPTY, player_position); | ||||||
|                 update_grid(gp, LIVING_PLAYER, player_position); |                 update_grid(gp, LIVING_PLAYER, new_player_position); | ||||||
|                 break; |                 break; | ||||||
|             case MAZE_EXIT: |             case MAZE_EXIT: | ||||||
|                 update_grid(gp, EMPTY, player_position); |                 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. |  * Het rooster wordt ge-updated afhankelijk van de user input. | ||||||
|  */ |  */ | ||||||
| static void speel_maze(grid *gp) { | static void speel_maze(grid *gp) { | ||||||
|  |     coordinate offset_vector = {.x = 0, .y = 0}; | ||||||
|     switch (getch()) { |     switch (getch()) { | ||||||
|         case KEY_UP: // fallthrough |         case KEY_UP: // fallthrough | ||||||
|         case 'w': |         case 'w': | ||||||
|         case 'W': |         case 'W': | ||||||
|             maze_runner_move(gp, 0, -1); |             offset_vector.y--; | ||||||
|             break; |             break; | ||||||
|         case KEY_DOWN: // fallthrough |         case KEY_DOWN: // fallthrough | ||||||
|         case 's': |         case 's': | ||||||
|         case 'S': |         case 'S': | ||||||
|             maze_runner_move(gp, 0, 1); |             offset_vector.y++; | ||||||
|             break; |             break; | ||||||
|         case KEY_LEFT: // fallthrough |         case KEY_LEFT: // fallthrough | ||||||
|         case 'a': |         case 'a': | ||||||
|         case 'A': |         case 'A': | ||||||
|             maze_runner_move(gp, -1, 0); |             offset_vector.x--; | ||||||
|             break; |             break; | ||||||
|         case KEY_RIGHT: // fallthrough |         case KEY_RIGHT: // fallthrough | ||||||
|         case 'd': |         case 'd': | ||||||
|         case 'D': |         case 'D': | ||||||
|             maze_runner_move(gp, 1, 0); |             offset_vector.x++; | ||||||
|             break; |             break; | ||||||
|         case 'p': |         case 'p': | ||||||
|         case KEY_BACKSPACE: |         case KEY_BACKSPACE: | ||||||
|         case KEY_ESCAPE: |         case KEY_ESCAPE: | ||||||
|             grid_put_state(gp, STATE_QUIT); |             grid_put_state(gp, STATE_QUIT); | ||||||
|             break; |             return; | ||||||
|     } |     } | ||||||
|  |     maze_runner_move(gp, offset_vector); | ||||||
| } | } | ||||||
|  |  | ||||||
| void maze_runner(void) { | void maze_runner(void) { | ||||||
|   | |||||||
| @@ -23,6 +23,7 @@ typedef enum { | |||||||
|     GAME_QUIT = 4, |     GAME_QUIT = 4, | ||||||
| } game; | } game; | ||||||
|  |  | ||||||
|  | static grid *MENU; | ||||||
| static game SELECTED_GAME = GAME_MANUAL; | static game SELECTED_GAME = GAME_MANUAL; | ||||||
| static coordinate OFFSET = {.x = 5, .y = 5}; | 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. |  * game: The game you want to launch. | ||||||
|  */ |  */ | ||||||
| static void launch_game(const game game) { | static void launch_game(const game game) { | ||||||
|  |     clear(); | ||||||
|     switch (game) { |     switch (game) { | ||||||
|         case GAME_MANUAL: |         case GAME_MANUAL: | ||||||
|             manual((coordinate){0,0}); |             manual((coordinate){0,0}); | ||||||
| @@ -88,10 +90,10 @@ static void menu_highlight(const grid *menu) { | |||||||
|  * Side Effects: |  * Side Effects: | ||||||
|  * Displays the menu |  * Displays the menu | ||||||
|  */ |  */ | ||||||
| static void show_menu(const grid *menu) { | static void show_menu() { | ||||||
|     erase(); |     erase(); | ||||||
|     show_grid_on_offset(menu, OFFSET); |     show_grid_on_offset(MENU, OFFSET); | ||||||
|     menu_highlight(menu); |     menu_highlight(MENU); | ||||||
|     refresh(); |     refresh(); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -121,7 +123,6 @@ static void menu_move(const int offset) { | |||||||
|  * 0: Continue running. |  * 0: Continue running. | ||||||
|  * 1: Exit the menu. |  * 1: Exit the menu. | ||||||
|  * |  * | ||||||
|  * |  | ||||||
|  * Side Effect: |  * Side Effect: | ||||||
|  * Changes the SELECTED_GAME as needed and launches selected games on select. |  * 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. |  * Create the menu grid. | ||||||
|  * |  | ||||||
|  * Output: |  | ||||||
|  * A pointer to the menu grid. |  | ||||||
|  */ |  */ | ||||||
| static grid *initialize_menu(void) { | static void initialize_menu(void) { | ||||||
|     char menu[] = "How to play\n" |     const char menu[] = "How to play\n" | ||||||
|                         "Maze Runner\n" |                         "Maze Runner\n" | ||||||
|                         "   Snake   \n" |                         "   Snake   \n" | ||||||
|                         "Minesweeper\n" |                         "Minesweeper\n" | ||||||
|                         "   Leave   \n"; |                         "   Leave   \n"; | ||||||
|     grid *rp = grid_create_from_string(menu); |     MENU = grid_create_from_string(menu); | ||||||
|     return rp; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void minigame_menu(void) { | void minigame_menu(void) { | ||||||
|     grid *menu = initialize_menu(); |     initialize_menu(); | ||||||
|  |  | ||||||
|     while (true) { |     while (true) { | ||||||
|  |         show_menu(); | ||||||
|         if (navigate_menu() == 1) { |         if (navigate_menu() == 1) { | ||||||
|             show_menu(menu); |  | ||||||
|             break; |             break; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     grid_cleanup(menu); |     grid_cleanup(MENU); | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user