| 
									
										
										
										
											2025-10-18 21:35:01 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Created by snapshot112 on 6/10/2025 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "maze_runner.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <ncurses.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-22 15:07:52 +02:00
										 |  |  | #include "../../engine/grid_game_engine.h"
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define EMPTY ' '
 | 
					
						
							|  |  |  | #define WALL '#'
 | 
					
						
							|  |  |  | #define LIVING_PLAYER '*'
 | 
					
						
							|  |  |  | #define DEAD_PLAYER '@'
 | 
					
						
							|  |  |  | #define MAZE_EXIT '$'
 | 
					
						
							|  |  |  | #define TRAP 'X'
 | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Reads in the maze from the assets file. | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Side Effects: | 
					
						
							|  |  |  |  * Memory is allocated to store the grid in. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | static grid *get_maze(void) { | 
					
						
							|  |  |  |     // Open het doolhof bestand en lees het rooster.
 | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     FILE *fh = fopen("assets/maze.txt", "r"); | 
					
						
							|  |  |  |     if (fh == NULL) { | 
					
						
							|  |  |  |         perror("loading maze"); | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     grid *gp = grid_create_from_file(fh); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     fclose(fh); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     // Bepaal of het lezen van het rooster is gelukt.
 | 
					
						
							|  |  |  |     if (gp == NULL) { | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |         fprintf(stderr, "Kan rooster niet maken.\n"); | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     return gp; | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Voert de benodigde veranderingen in het rooster door als de speler in een
 | 
					
						
							|  |  |  |  * bepaalde richting probeert te bewegen. | 
					
						
							|  |  |  |  * Input: | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |  * gp   : een pointer naar het rooster | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Side effect: het rooster wordt aangepast op basis van de handeling van | 
					
						
							|  |  |  |  *              de speler. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | static void maze_runner_move(grid *gp, const int dx, const int dy) { | 
					
						
							|  |  |  |     coordinate player_position = {0, 0}; | 
					
						
							|  |  |  |     grid_find(gp, LIVING_PLAYER, &player_position.x, &player_position.y); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     if (player_position.y == -1) { | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |         printf("Player not found!"); | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |         grid_put_state(gp, STATE_BEGIN); | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     if (grid_contains(gp, player_position.x + dx, player_position.y + dy) == 1) { | 
					
						
							|  |  |  |         char new_location = grid_fetch(gp, player_position.x + dx, player_position.y + dy); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |         switch (new_location) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             case WALL: | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             case TRAP: | 
					
						
							|  |  |  |                 grid_put_state(gp, STATE_VERLOREN); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 enable_highlight(RED); | 
					
						
							|  |  |  |                 update_grid(gp, DEAD_PLAYER, player_position.x, player_position.y); | 
					
						
							|  |  |  |                 disable_highlight(RED); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             case EMPTY: | 
					
						
							|  |  |  |                 update_grid(gp, EMPTY, player_position.x, player_position.y); | 
					
						
							|  |  |  |                 update_grid(gp, LIVING_PLAYER, player_position.x + dx, player_position.y + dy); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             case MAZE_EXIT: | 
					
						
							|  |  |  |                 update_grid(gp, EMPTY, player_position.x, player_position.y); | 
					
						
							|  |  |  |                 grid_put_state(gp, STATE_GEWONNEN); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         refresh(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Speelt het spel met een gegeven rooster tot de toestand niet langer | 
					
						
							|  |  |  |  * AAN_HET_SPELEN is. | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Input: | 
					
						
							|  |  |  |  * gp: Een pointer naar het rooster. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Side effects: | 
					
						
							|  |  |  |  * Het rooster wordt ge-updated afhankelijk van de user input. | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | static void speel_maze(grid *gp) { | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  |     switch (getch()) { | 
					
						
							|  |  |  |         case KEY_UP: // fallthrough
 | 
					
						
							|  |  |  |         case 'w': | 
					
						
							|  |  |  |         case 'W': | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             maze_runner_move(gp, 0, -1); | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case KEY_DOWN: // fallthrough
 | 
					
						
							|  |  |  |         case 's': | 
					
						
							|  |  |  |         case 'S': | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             maze_runner_move(gp, 0, 1); | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case KEY_LEFT: // fallthrough
 | 
					
						
							|  |  |  |         case 'a': | 
					
						
							|  |  |  |         case 'A': | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             maze_runner_move(gp, -1, 0); | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case KEY_RIGHT: // fallthrough
 | 
					
						
							|  |  |  |         case 'd': | 
					
						
							|  |  |  |         case 'D': | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             maze_runner_move(gp, 1, 0); | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |         case 'p': | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  |         case KEY_BACKSPACE: | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |         case KEY_ESCAPE: | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             grid_put_state(gp, STATE_QUIT); | 
					
						
							| 
									
										
										
										
											2025-10-17 11:15:53 +02:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void maze_runner(void) { | 
					
						
							|  |  |  |     // Voorbereiding.
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     grid *gp = get_maze(); | 
					
						
							|  |  |  |     if (gp == NULL) { | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     show_grid(gp); | 
					
						
							|  |  |  |     grid_put_state(gp, STATE_AAN_HET_SPELEN); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Game zelf.
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     while (grid_fetch_state(gp) == STATE_AAN_HET_SPELEN) { | 
					
						
							|  |  |  |         speel_maze(gp); | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Exit game.
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     game_exit_message(gp, (coordinate){0, grid_height(gp) + 2}); | 
					
						
							|  |  |  |     grid_cleanup(gp); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } |