| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-10-18 21:35:01 +02:00
										 |  |  |  * Created by snapshot112 on 8/10/2025 | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "grid_game_engine.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <locale.h>
 | 
					
						
							|  |  |  | #include <ncurses.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | int same_coordinate(const coordinate a, const coordinate b) { | 
					
						
							| 
									
										
										
										
											2025-10-17 14:29:22 +02:00
										 |  |  |     return a.x == b.x && a.y == b.y; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | int modulo(const int number, const int mod) { | 
					
						
							|  |  |  |     int result = number % mod; | 
					
						
							|  |  |  |     if (result < 0) { | 
					
						
							|  |  |  |         result += mod;    //This is not how math is supposed to work C, damnit.
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | void show_grid_on_offset(const grid *gp, const int starting_x, const int starting_y) { | 
					
						
							|  |  |  |     const int height = grid_height(gp); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int y = 0; y < height; y++) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |         char *rij = grid_fetch_row(gp, y); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |         mvprintw(starting_y + y, starting_x, "%s", rij); | 
					
						
							|  |  |  |         free(rij); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     refresh(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | void show_grid(const grid *gp) { | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |     show_grid_on_offset(gp, 0, 0); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | void update_grid(grid *gp, const char c, const int x, const int y) { | 
					
						
							|  |  |  |     if (grid_put(gp, x, y, c) == 1) { | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |         mvaddch(y, x, c); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Shows the victory screen. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Side Effect: | 
					
						
							|  |  |  |  * Clears the console and prints the victory message. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | static void display_victory(const coordinate location) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     mvaddstr(location.y, location.x, "YOU WON!!!!!"); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Shows the GAME OVER screen. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Side Effect: | 
					
						
							|  |  |  |  * Clears the console and prints the GAME OVER message. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | static void display_loss(const coordinate location) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     mvaddstr(location.y, location.x, "GAME OVER"); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Shows the quit screen | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Side Effect: | 
					
						
							|  |  |  |  * Clears the console and prints the quit message. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | static void display_quit(const coordinate location) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     mvaddstr(location.y, location.x, "You quit the game"); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Shows the hacker man screen | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Side Effect: | 
					
						
							|  |  |  |  * Clears the console and prints the hacker man message. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | static void display_hackerman(const coordinate location) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     mvaddstr(location.y, location.x, "The hacker man strikes again..."); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | void graceful_exit(const coordinate message_location) { | 
					
						
							| 
									
										
										
										
											2025-10-18 21:35:01 +02:00
										 |  |  |     mvaddstr(message_location.y, message_location.x, "Press ENTER to exit."); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     while (1) { | 
					
						
							|  |  |  |         switch (getch()) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |             case KEY_BACKSPACE: | 
					
						
							|  |  |  |             case KEY_ESCAPE: | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |             case KEY_ENTER: | 
					
						
							|  |  |  |             case '\n': | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | void game_exit_message(const grid *gp, coordinate location) { | 
					
						
							|  |  |  |     const state current_state = grid_fetch_state(gp); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     switch (current_state) { | 
					
						
							|  |  |  |         case STATE_GEWONNEN: | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |             display_victory(location); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case STATE_VERLOREN: | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |             display_loss(location); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case STATE_QUIT: | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |             display_quit(location); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |             display_hackerman(location); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     location.y += 2; | 
					
						
							|  |  |  |     graceful_exit(location); | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     refresh(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void enable_highlight(const game_colors color) { | 
					
						
							|  |  |  |     attron(COLOR_PAIR(color)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void disable_highlight(const game_colors color) { | 
					
						
							|  |  |  |     attroff(COLOR_PAIR(color)); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Initialize ncurses. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This enables cbreak, noecho, hides the cursor and enables the extra keys. | 
					
						
							|  |  |  |  * This also creates the color pairs needed for game_colors and sets ESCDELAY to 0. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void init_ncurses(void) { | 
					
						
							| 
									
										
										
										
											2025-10-17 21:08:03 +02:00
										 |  |  |     ESCDELAY = 0; | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  |     setlocale(LC_ALL, ""); | 
					
						
							|  |  |  |     initscr(); | 
					
						
							|  |  |  |     cbreak();             // So you can cancel the game with ctrl + c.
 | 
					
						
							|  |  |  |     keypad(stdscr, TRUE); // Enable extra keys like the arrow keys.
 | 
					
						
							|  |  |  |     noecho();             // Don't write the keyboard input to the console.
 | 
					
						
							|  |  |  |     curs_set(0);          // Hides the cursor.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     start_color(); | 
					
						
							|  |  |  |     init_pair(BLACK, COLOR_BLACK, COLOR_BLACK); | 
					
						
							|  |  |  |     init_pair(WHITE, COLOR_BLACK, COLOR_WHITE); | 
					
						
							|  |  |  |     init_pair(BLUE, COLOR_BLACK, COLOR_BLUE); | 
					
						
							|  |  |  |     init_pair(GREEN, COLOR_BLACK, COLOR_GREEN); | 
					
						
							|  |  |  |     init_pair(CYAN, COLOR_BLACK, COLOR_CYAN); | 
					
						
							|  |  |  |     init_pair(MAGENTA, COLOR_BLACK, COLOR_MAGENTA); | 
					
						
							|  |  |  |     init_pair(YELLOW, COLOR_BLACK, COLOR_YELLOW); | 
					
						
							|  |  |  |     init_pair(RED, COLOR_BLACK, COLOR_RED); | 
					
						
							| 
									
										
										
										
											2025-10-16 12:05:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     clear(); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void init_engine(void) { | 
					
						
							|  |  |  |     init_ncurses(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void cleanup_engine(void) { | 
					
						
							| 
									
										
										
										
											2025-10-18 18:56:28 +02:00
										 |  |  |     endwin(); | 
					
						
							| 
									
										
										
										
											2025-10-16 01:10:09 +02:00
										 |  |  | } |