snake wip
This commit is contained in:
		
							
								
								
									
										65
									
								
								snake.c
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								snake.c
									
									
									
									
									
								
							| @@ -5,12 +5,67 @@ | ||||
| #include "snake.h" | ||||
|  | ||||
| #include <ncurses.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #include "grid_game_engine.h" | ||||
|  | ||||
| void snake(void) { | ||||
|     clear(); | ||||
|     mvprintw(0,0, "Snake has not yet been created"); | ||||
|     graceful_exit(); | ||||
|     refresh(); | ||||
| /* | ||||
|  * Create a grid for snake with a given height and width. | ||||
|  * | ||||
|  * Input: | ||||
|  * height: The height of the map. | ||||
|  * width:  The width of the map. | ||||
|  * | ||||
|  * Returns: | ||||
|  * A pointer to the grid. | ||||
|  */ | ||||
| static rooster *initialize_snake(void) { | ||||
|     int width = 10; | ||||
|     int height = 10; | ||||
|     const int grid_size = (width + 1) * height + 1; | ||||
|     char *map = malloc(grid_size * sizeof(char)); | ||||
|     if (map == NULL) { | ||||
|         return NULL; | ||||
|     } | ||||
|  | ||||
|     for (int i = 0; i < (width + 1) * height; i++) { | ||||
|         int top_line = i < width; | ||||
|         int bottom_line = i > grid_size - (width + 2); | ||||
|  | ||||
|         int line_position = modulo(i, width + 1); | ||||
|  | ||||
|         int line_start = line_position == 1; | ||||
|         int line_end = line_position == width; | ||||
|  | ||||
|         int newline = line_position == 0; | ||||
|  | ||||
|         if (newline) { | ||||
|             map[i] = '\n'; | ||||
|         } else if (top_line || bottom_line || line_start || line_end) { | ||||
|             map[i] = '#'; | ||||
|         } | ||||
|         else { | ||||
|             map[i] = ' '; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     map[grid_size - 1] = '\0'; | ||||
|  | ||||
|     printf("%s", map); | ||||
|  | ||||
|     rooster *grid = rooster_maak(map); | ||||
|     free(map); | ||||
|  | ||||
|     return grid; | ||||
| } | ||||
|  | ||||
| void snake(void) { | ||||
|     // erase(); | ||||
|     rooster* const gp = initialize_snake(); | ||||
|     if (gp == NULL) { | ||||
|         printf("Help, initializing the grid failed\n"); | ||||
|     } | ||||
|     // show_grid(gp); | ||||
|     // graceful_exit(); | ||||
|     // refresh(); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user