Code cleanup and documentation
This commit is contained in:
BIN
deel2.tar.gz
Normal file
BIN
deel2.tar.gz
Normal file
Binary file not shown.
92
grid.c
92
grid.c
@@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Created by snapshot112 on 2/10/2025
|
||||
*/
|
||||
|
||||
|
||||
#include "grid.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
@@ -8,12 +13,15 @@
|
||||
* The grid type this program is build around.
|
||||
*/
|
||||
typedef struct grid_data {
|
||||
char *rost;
|
||||
char *locations;
|
||||
int height;
|
||||
int width;
|
||||
state state;
|
||||
} grid;
|
||||
|
||||
/*
|
||||
* Translate x and y coordinates to a location index on the grid.
|
||||
*/
|
||||
static int internal_location(const grid *gp, const int x, const int y) {
|
||||
return y * (gp->width + 1) + x;
|
||||
}
|
||||
@@ -48,12 +56,12 @@ grid *grid_create_from_string(const char* input) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gp->rost = malloc(grid_size * sizeof(char));
|
||||
gp->locations = malloc(grid_size * sizeof(char));
|
||||
gp->height = height;
|
||||
gp->width = width;
|
||||
gp->state = STATE_BEGIN;
|
||||
|
||||
strcpy(gp->rost, input);
|
||||
strcpy(gp->locations, input);
|
||||
|
||||
return gp;
|
||||
}
|
||||
@@ -62,8 +70,8 @@ grid *grid_create_from_string(const char* input) {
|
||||
* Sets a grids width and height
|
||||
*
|
||||
* Input:
|
||||
* fh: the stream to read the grid from.
|
||||
* rost: a grid file to store the width and height in.
|
||||
* fh: The stream to read the grid from.
|
||||
* gp: A pointer to the grid whose width and height you want to set.
|
||||
*
|
||||
* Side effects:
|
||||
* the grid gets its width and height set
|
||||
@@ -72,27 +80,27 @@ grid *grid_create_from_string(const char* input) {
|
||||
* 1 if the file width and height seem to match with its size
|
||||
* 0 otherwise
|
||||
*/
|
||||
static int get_grid_sizes(FILE *fh, grid *rost) {
|
||||
static int get_grid_sizes(FILE *fh, grid *gp) {
|
||||
while (getc(fh) != '\n') {
|
||||
if (feof(fh)) {
|
||||
return 0;
|
||||
}
|
||||
rost->width++;
|
||||
gp->width++;
|
||||
}
|
||||
|
||||
if (rost->width == 0) {
|
||||
if (gp->width == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
fseek(fh, 0, SEEK_END);
|
||||
|
||||
// Get file size (- 1 for the blank newline and EOF at the end)
|
||||
if (ftell(fh) % (rost->width + 1) != 0) {
|
||||
if (ftell(fh) % (gp->width + 1) != 0) {
|
||||
// Not all lines are the same width
|
||||
return 0;
|
||||
}
|
||||
|
||||
rost->height = (int)ftell(fh) / (int)sizeof(char) / (rost->width + 1);
|
||||
gp->height = (int)ftell(fh) / (int)sizeof(char) / (gp->width + 1);
|
||||
fseek(fh, 0, SEEK_SET);
|
||||
|
||||
return 1;
|
||||
@@ -103,7 +111,7 @@ grid *grid_create_from_file(FILE *fh) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
grid rost = {
|
||||
grid temp_grid = {
|
||||
NULL,
|
||||
0,
|
||||
0,
|
||||
@@ -111,52 +119,52 @@ grid *grid_create_from_file(FILE *fh) {
|
||||
};
|
||||
|
||||
// Sets the width and height of the grid.
|
||||
if (get_grid_sizes(fh, &rost) != 1) {
|
||||
if (get_grid_sizes(fh, &temp_grid) != 1) {
|
||||
// Unlogical file structure.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const int grid_size = (rost.width + 1) * rost.height + 1;
|
||||
const int grid_size = (temp_grid.width + 1) * temp_grid.height + 1;
|
||||
|
||||
rost.rost = malloc(grid_size * sizeof(char));
|
||||
if (rost.rost == NULL) {
|
||||
temp_grid.locations = malloc(grid_size * sizeof(char));
|
||||
if (temp_grid.locations == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// This makes the strncat() work.
|
||||
rost.rost[0] = '\0';
|
||||
temp_grid.locations[0] = '\0';
|
||||
|
||||
char *line = malloc((rost.width + 2) * sizeof(char));
|
||||
char *line = malloc((temp_grid.width + 2) * sizeof(char));
|
||||
if (line == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rost.height; i++) {
|
||||
if (fgets(line, rost.width + 2, fh) == NULL) {
|
||||
free(rost.rost);
|
||||
for (int i = 0; i < temp_grid.height; i++) {
|
||||
if (fgets(line, temp_grid.width + 2, fh) == NULL) {
|
||||
free(temp_grid.locations);
|
||||
free(line);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Validate that the line length is correct
|
||||
if ((int)strcspn(line, "\n") != rost.width) {
|
||||
free(rost.rost);
|
||||
if ((int)strcspn(line, "\n") != temp_grid.width) {
|
||||
free(temp_grid.locations);
|
||||
free(line);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Width is without the newline at the end.
|
||||
strncat(rost.rost, line, rost.width + 1);
|
||||
strncat(temp_grid.locations, line, temp_grid.width + 1);
|
||||
}
|
||||
|
||||
free(line);
|
||||
|
||||
grid *return_grid = malloc(sizeof(rost));
|
||||
grid *return_grid = malloc(sizeof(temp_grid));
|
||||
if (return_grid == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(return_grid, &rost, sizeof(rost));
|
||||
memcpy(return_grid, &temp_grid, sizeof(temp_grid));
|
||||
|
||||
return return_grid;
|
||||
}
|
||||
@@ -192,9 +200,9 @@ void grid_put_state(grid *gp, const state t) {
|
||||
|
||||
void grid_cleanup(grid *gp) {
|
||||
if (gp != NULL) {
|
||||
if (gp->rost != NULL)
|
||||
if (gp->locations != NULL)
|
||||
{
|
||||
free(gp->rost);
|
||||
free(gp->locations);
|
||||
}
|
||||
free(gp);
|
||||
}
|
||||
@@ -215,7 +223,7 @@ int grid_height(const grid *gp) {
|
||||
}
|
||||
|
||||
int grid_contains(const grid *gp, const int x, const int y) {
|
||||
if (gp != NULL && gp->rost != NULL) {
|
||||
if (gp != NULL && gp->locations != NULL) {
|
||||
if (x >= 0 && y >= 0 && x < gp->width && y < gp->height)
|
||||
{
|
||||
return 1;
|
||||
@@ -225,25 +233,25 @@ int grid_contains(const grid *gp, const int x, const int y) {
|
||||
}
|
||||
|
||||
char grid_fetch(const grid *gp, const int x, const int y) {
|
||||
if (gp != NULL && gp->rost != NULL && grid_contains(gp, x, y) == 1) {
|
||||
return gp->rost[internal_location(gp, x, y)];
|
||||
if (gp != NULL && gp->locations != NULL && grid_contains(gp, x, y) == 1) {
|
||||
return gp->locations[internal_location(gp, x, y)];
|
||||
}
|
||||
return '\0';
|
||||
}
|
||||
|
||||
int grid_put(grid *gp, const int x, const int y, const char c) {
|
||||
if (gp != NULL && gp->rost != NULL && grid_contains(gp, x, y) == 1) {
|
||||
gp->rost[internal_location(gp, x, y)] = c;
|
||||
if (gp != NULL && gp->locations != NULL && grid_contains(gp, x, y) == 1) {
|
||||
gp->locations[internal_location(gp, x, y)] = c;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *grid_fetch_row(const grid *gp, const int y) {
|
||||
if (gp != NULL && gp->rost != NULL && grid_contains(gp, 0, y) == 1) {
|
||||
if (gp != NULL && gp->locations != NULL && grid_contains(gp, 0, y) == 1) {
|
||||
// we're going to remove the newline so this is long enough
|
||||
char *row = malloc((gp->width + 1) * sizeof(char));
|
||||
memcpy(row, &gp->rost[internal_location(gp, 0, y)], gp->width * sizeof(char));
|
||||
memcpy(row, &gp->locations[internal_location(gp, 0, y)], gp->width * sizeof(char));
|
||||
row[gp->width] = '\0';
|
||||
return row;
|
||||
}
|
||||
@@ -251,11 +259,11 @@ char *grid_fetch_row(const grid *gp, const int y) {
|
||||
}
|
||||
|
||||
grid *grid_copy(const grid *gp) {
|
||||
if (gp != NULL && gp->rost != NULL) {
|
||||
if (gp != NULL && gp->locations != NULL) {
|
||||
const size_t grid_memory = ((gp->width + 1) * gp->height + 1) * sizeof(char);
|
||||
|
||||
char *rost = malloc(grid_memory);
|
||||
if (rost == NULL) {
|
||||
char *locations = malloc(grid_memory);
|
||||
if (locations == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -264,18 +272,18 @@ grid *grid_copy(const grid *gp) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(rost, gp->rost, grid_memory);
|
||||
memcpy(locations, gp->locations, grid_memory);
|
||||
|
||||
memcpy(new_grid, gp, sizeof(*gp));
|
||||
|
||||
new_grid->rost = rost;
|
||||
new_grid->locations = locations;
|
||||
return new_grid;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void grid_find(const grid *gp, const char c, int *x, int *y) {
|
||||
if (gp == NULL || gp->rost == NULL) {
|
||||
if (gp == NULL || gp->locations == NULL) {
|
||||
*x = -1;
|
||||
*y = -1;
|
||||
return;
|
||||
@@ -283,9 +291,9 @@ void grid_find(const grid *gp, const char c, int *x, int *y) {
|
||||
|
||||
const char search[2] = {c};
|
||||
|
||||
const int char_index = (int)strcspn(gp->rost, search);
|
||||
const int char_index = (int)strcspn(gp->locations, search);
|
||||
|
||||
if (gp->rost[char_index] == '\0')
|
||||
if (gp->locations[char_index] == '\0')
|
||||
{
|
||||
*x = -1;
|
||||
*y = -1;
|
||||
|
||||
2
grid.h
2
grid.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* grid.h
|
||||
* Created by snapshot112 on 2/10/2025
|
||||
*
|
||||
* This module provides a grid api.
|
||||
*
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/*
|
||||
* Created by snapshot112 on 10/15/25.
|
||||
*
|
||||
* A game engine that can run and display games in square grids.
|
||||
* Created by snapshot112 on 8/10/2025
|
||||
*/
|
||||
|
||||
#include "grid_game_engine.h"
|
||||
@@ -84,14 +82,13 @@ static void display_hackerman(const coordinate location) {
|
||||
}
|
||||
|
||||
void graceful_exit(const coordinate message_location) {
|
||||
mvaddstr(message_location.y, message_location.x, "Press ENTER or SPACE to exit.");
|
||||
mvaddstr(message_location.y, message_location.x, "Press ENTER to exit.");
|
||||
while (1) {
|
||||
switch (getch()) {
|
||||
case KEY_BACKSPACE:
|
||||
case KEY_ESCAPE:
|
||||
case KEY_ENTER:
|
||||
case '\n':
|
||||
case ' ':
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
/*
|
||||
* Created by snapshot112 on 10/15/25.
|
||||
* Created by snapshot112 on 15/10/2025.
|
||||
*
|
||||
* A game engine that uses the can run and display games in rectangular grids.
|
||||
* The graphics are made using ncurses.
|
||||
* A game engine build on top of a grid api to run and display games using ncurses.
|
||||
*
|
||||
* Please make sure to initialize the game engine before running any games.
|
||||
*/
|
||||
@@ -133,12 +132,12 @@ void update_grid(grid *gp, char c, int x, int y);
|
||||
void game_exit_message(const grid *gp, coordinate location);
|
||||
|
||||
/*
|
||||
* Waits for you to press ENTER or SPACE before exiting the game.
|
||||
* Waits for you to press ENTER before exiting the game.
|
||||
*
|
||||
* Input:
|
||||
* coordinate: The location to show the message.
|
||||
*
|
||||
* Side effect: Prints "Press ENTER or SPACE to exit." game to the console.
|
||||
* Side effect: Prints "Press ENTER to exit." game to the console.
|
||||
*/
|
||||
void graceful_exit(coordinate message_location);
|
||||
|
||||
|
||||
9
manual.c
9
manual.c
@@ -1,6 +1,7 @@
|
||||
//
|
||||
// Created by snapshot112 on 10/17/25.
|
||||
//
|
||||
/*
|
||||
* Created by snapshot112 on 10/17/2025
|
||||
*/
|
||||
|
||||
|
||||
#include "manual.h"
|
||||
|
||||
@@ -26,7 +27,7 @@ void manual(const coordinate display_location) {
|
||||
|
||||
// Wait until ESCAPE or BACKSPACE is pressed.
|
||||
timeout(200);
|
||||
for (int ch = getch(); ch != KEY_ESCAPE && ch != KEY_BACKSPACE && ch != ' '; ch = getch()) {
|
||||
for (int ch = getch(); ch != KEY_ESCAPE && ch != KEY_BACKSPACE; ch = getch()) {
|
||||
// Update the screen in the meantime to accommodate windows resizes.
|
||||
show_grid_on_offset(grid, display_location.x, display_location.y);
|
||||
}
|
||||
|
||||
8
manual.h
8
manual.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Created by snapshot112 on 10/15/25.
|
||||
* Created by snapshot112 on 10/17/2025
|
||||
*
|
||||
* Display the manual for the minigames
|
||||
* Provides a way to display the minigame manual in the assets in game.
|
||||
*/
|
||||
|
||||
#ifndef MINIGAME_MENU_MANUAL_H
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "grid_game_engine.h"
|
||||
|
||||
/*
|
||||
* A game manual for the minigames menu.
|
||||
* An in game manual for the minigames menu.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before opening the manual
|
||||
*
|
||||
@@ -20,7 +20,7 @@
|
||||
* Clears the console and uses it to display the manual.
|
||||
*
|
||||
* Controls:
|
||||
* Press ESCAPE or ENTER to exit the manual.
|
||||
* Press ESCAPE or BACKSPACE to exit the manual.
|
||||
*/
|
||||
void manual(coordinate display_location);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// Created by snapshot112 on 10/15/25.
|
||||
//
|
||||
/*
|
||||
* Created by snapshot112 on 6/10/2025
|
||||
*/
|
||||
|
||||
#include "maze_runner.h"
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
/*
|
||||
* Created by snapshot112 on 6/10/2025
|
||||
*
|
||||
* Naam: Jeroen Boxhoorn
|
||||
* UvAnetID: 16333969
|
||||
* Studie: BSc Informatica
|
||||
* Studie: BSC Informatica
|
||||
*
|
||||
* A game of maze runner configured to run on the grid game engine.
|
||||
* A game of maze runner build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling maze_runner();
|
||||
*
|
||||
@@ -23,7 +25,7 @@
|
||||
#define MINIGAME_MENU_MAZE_RUNNER_H
|
||||
|
||||
/*
|
||||
* A game of maze runner configured to run on the grid game engine.
|
||||
* A game of maze runner build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling maze_runner();
|
||||
*
|
||||
@@ -32,7 +34,7 @@
|
||||
*
|
||||
* Controls:
|
||||
* use WSAD or arrow keys to move through the maze.
|
||||
* Use BACKSPACE to exit the game early.
|
||||
* Use BACKSPACE or ESCAPE to exit the game early.
|
||||
*/
|
||||
void maze_runner(void);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// Created by snapshot112 on 10/15/25.
|
||||
//
|
||||
/*
|
||||
* Created by snapshot112 on 15/10/2025
|
||||
*/
|
||||
|
||||
#include "minesweeper.h"
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
/*
|
||||
* Created by snapshot112 on 10/15/25.
|
||||
* Created by snapshot112 on 15/10/2025
|
||||
*
|
||||
* A game of minesweeper runner configured to run on the grid game engine.
|
||||
* A game of minesweeper build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling snake();
|
||||
* Please make sure to include and initialize the game engine before calling minesweeper();
|
||||
*/
|
||||
|
||||
#ifndef MINIGAME_MENU_MINESWEEPER_H
|
||||
#define MINIGAME_MENU_MINESWEEPER_H
|
||||
|
||||
/*
|
||||
* A game of minesweeper configured to run on the grid game engine.
|
||||
* A game of minesweeper build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling snake();
|
||||
* Please make sure to include and initialize the game engine before calling minesweeper();
|
||||
*
|
||||
* Side Effects:
|
||||
* Clears the console and uses it to play a game of minesweeper.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// Created by snapshot112 on 10/15/25.
|
||||
//
|
||||
/*
|
||||
* Created by snapshot112 on 15/10/2025
|
||||
*/
|
||||
|
||||
#include "minigame_menu.h"
|
||||
|
||||
@@ -152,7 +152,6 @@ static int navigate_menu(void) {
|
||||
}
|
||||
launch_game(SELECTED_GAME);
|
||||
break;
|
||||
case 'p':
|
||||
case KEY_BACKSPACE:
|
||||
case KEY_ESCAPE:
|
||||
return 1;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Created by snapshot112 on 10/15/25.
|
||||
* Created by snapshot112 on 15/10/2025
|
||||
*
|
||||
* A minigame menu for games configured to run on the grid game engine.
|
||||
* A minigame menu for games build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling menu();
|
||||
*/
|
||||
@@ -10,7 +10,7 @@
|
||||
#define MINIGAME_MENU_MINIGAME_MENU_H
|
||||
|
||||
/*
|
||||
* A minigame menu for games configured to run on the grid game engine.
|
||||
* A minigame menu for games build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling menu();
|
||||
*
|
||||
@@ -18,10 +18,10 @@
|
||||
* Clears the console and uses it to display a minigame menu.
|
||||
*
|
||||
* Controls:
|
||||
* 'w'/'arr_up: Next menu item.
|
||||
* 's'/'arr_down': Previous menu item.
|
||||
* 'f': Select current menu item.
|
||||
* 'BACKSPACE': Exit the menu.
|
||||
* 'w'/'arr_up': Next menu item.
|
||||
* 's'/'arr_down': Previous menu item.
|
||||
* 'f'/'ENTER': Select current menu item.
|
||||
* 'BACKSPACE'/'ESC': Exit the menu.
|
||||
*/
|
||||
void minigame_menu(void);
|
||||
|
||||
|
||||
6
snake.c
6
snake.c
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// Created by snapshot112 on 10/15/25.
|
||||
//
|
||||
/*
|
||||
* Created by snapshot112 on 15/10/2025
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 199309
|
||||
|
||||
|
||||
8
snake.h
8
snake.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Created by snapshot112 on 10/15/25.
|
||||
* Created by snapshot112 on 15/10/2025
|
||||
*
|
||||
* A game of maze runner configured to run on the grid game engine.
|
||||
* A game of maze runner build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling snake();
|
||||
*/
|
||||
@@ -10,7 +10,7 @@
|
||||
#define MINIGAME_MENU_SNAKE_H
|
||||
|
||||
/*
|
||||
* A game of snake configured to run on the grid game engine.
|
||||
* A game of snake build on the grid game engine.
|
||||
*
|
||||
* Please make sure to include and initialize the game engine before calling snake();
|
||||
*
|
||||
@@ -19,7 +19,7 @@
|
||||
*
|
||||
* Controls:
|
||||
* Use WSAD or arrow keys to redirect the snake.
|
||||
* Use BACKSPACE to exit the game early.
|
||||
* Use BACKSPACE or ESCAPE to exit the game early.
|
||||
*/
|
||||
void snake(void);
|
||||
|
||||
|
||||
6
spel.c
6
spel.c
@@ -1,7 +1,9 @@
|
||||
/*
|
||||
* Created by snapshot112 on 15/10/2025
|
||||
*
|
||||
* Naam: Jeroen Boxhoorn
|
||||
* UvAnetID: 16333969
|
||||
* Studie: BSc Informatica
|
||||
* Studie: BSC Informatica
|
||||
*
|
||||
* A minigame menu that lets you play games on the grid game engine.
|
||||
*
|
||||
@@ -9,6 +11,8 @@
|
||||
* - maze runner
|
||||
* - snake
|
||||
* - minesweeper
|
||||
*
|
||||
* A user manual can be found in the assets or by selected it in the menu using ENTER or 'f'.
|
||||
*/
|
||||
|
||||
#include "grid_game_engine.h"
|
||||
|
||||
Reference in New Issue
Block a user