aboutsummaryrefslogtreecommitdiff
path: root/games/gamemaze.c
diff options
context:
space:
mode:
authordavid <[email protected]>2026-07-23 22:12:42 -0300
committerdavid <[email protected]>2026-07-23 22:12:42 -0300
commit17881c653f333baa15f9e7b735466a0333a6cee6 (patch)
tree851c25695ca2433714383ecf697e2049e2a6c65e /games/gamemaze.c
parent9765de97f1df3bed997eb341172ba4575f5376f3 (diff)
downloadpetratc-17881c653f333baa15f9e7b735466a0333a6cee6.tar.gz
petratc-17881c653f333baa15f9e7b735466a0333a6cee6.zip
fixed a couple bugs
Diffstat (limited to 'games/gamemaze.c')
-rw-r--r--games/gamemaze.c41
1 files changed, 36 insertions, 5 deletions
diff --git a/games/gamemaze.c b/games/gamemaze.c
index 1468475..3ce8737 100644
--- a/games/gamemaze.c
+++ b/games/gamemaze.c
@@ -64,7 +64,15 @@ void lerw() {
char *vis = calloc(total, 1); //visited
int *px = malloc(total * sizeof(int)); //path x
int *py = malloc(total * sizeof(int)); //path y
- int *ip = malloc(total * sizeof(int)); // index of path
+ int *ip = malloc(total * sizeof(int)); // index of path
+
+ if(vis == NULL || px == NULL || py == NULL || ip == NULL) {
+ free(vis);
+ free(px);
+ free(py);
+ free(ip);
+ return;
+ }
vis[0] = 1;
grid[1][1] = grid[1][2] = grid[2][1] = grid[2][2] = ' '; //these should be open always
int vc = 1;
@@ -203,8 +211,20 @@ static void properrestart(cursor *curs, int *W, int *H) {
*H = height;
grid = malloc(*H * sizeof(char *));
- for (int y = 0; y < *H; y++)
+ if(grid == NULL) {
+ running = 0;
+ return;
+ }
+ for (int y = 0; y < *H; y++) {
grid[y] = malloc(*W * sizeof(char));
+ if(grid[y] == NULL) {
+ // Free already allocated rows
+ for(int i = 0; i < y; i++) free(grid[i]);
+ free(grid);
+ running = 0;
+ return;
+ }
+ }
lerw();
spawncheeses();
@@ -225,9 +245,20 @@ void gamemaze(){
int W = width;
int H = height;
- grid = malloc(H * sizeof(char *)); //those who allocate memory
- for (int y = 0; y < H; y++)
- grid[y] = malloc(W * sizeof(char));
+ grid = malloc(H * sizeof(char *)); //those who allocate memory
+ if(grid == NULL) {
+ stop();
+ return;
+ }
+ for (int y = 0; y < H; y++) {
+ grid[y] = malloc(W * sizeof(char));
+ if(grid[y] == NULL) {
+ for(int i = 0; i < y; i++) free(grid[i]);
+ free(grid);
+ stop();
+ return;
+ }
+ }
lerw();
spawncheeses();