aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-01-07 17:18:30 +0100
committerHampusM <hampus@hampusmat.com>2022-01-07 17:18:30 +0100
commit02dee3370f1420b208c546194ac67322fe563a24 (patch)
tree183df0b5267c233d658f0fd3cb5bfd87eb11b402 /src
parent813c98b028c9ba484fb5b21a60067ae35cc925ff (diff)
refactor: add explicit int types
Diffstat (limited to 'src')
-rw-r--r--src/grid.c10
-rw-r--r--src/maze.c50
-rw-r--r--src/mazerator.c1
-rw-r--r--src/position_stack.c2
-rw-r--r--src/utils.c2
5 files changed, 38 insertions, 27 deletions
diff --git a/src/grid.c b/src/grid.c
index 3554779..1ad01cd 100644
--- a/src/grid.c
+++ b/src/grid.c
@@ -13,11 +13,11 @@ Grid grid_create(unsigned int width, unsigned int height, char *fill)
Grid grid = {.grid = malloc_s(mem_height), .dimens = dimens};
// Fill the grid
- for (unsigned int y = 0; y < height; y++)
+ for (unsigned int y = 0U; y < height; y++)
{
grid.grid[y] = malloc_s(mem_width);
- for (unsigned int x = 0; x < width; x++)
+ for (unsigned int x = 0U; x < width; x++)
grid.grid[y][x] = fill;
}
@@ -36,9 +36,9 @@ void grid_set(Grid grid, Position pos, char *value)
void grid_print(Grid grid)
{
- for (unsigned int y = 0; y < grid.dimens.height; y++)
+ for (unsigned int y = 0U; y < grid.dimens.height; y++)
{
- for (unsigned int x = 0; x < grid.dimens.width; x++)
+ for (unsigned int x = 0U; x < grid.dimens.width; x++)
printf("%s", grid.grid[y][x]);
printf("\n");
@@ -48,7 +48,7 @@ void grid_print(Grid grid)
void grid_destroy(Grid grid)
{
// Deallocate the memory of the grid
- for (unsigned int y = 0; y < grid.dimens.height; y++)
+ for (unsigned int y = 0U; y < grid.dimens.height; y++)
free(grid.grid[y]);
free(grid.grid);
diff --git a/src/maze.c b/src/maze.c
index 5a59c76..76775b2 100644
--- a/src/maze.c
+++ b/src/maze.c
@@ -1,7 +1,6 @@
#include "maze.h"
#include "grid.h"
#include "position.h"
-#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -11,42 +10,43 @@ int is_pos_empty(Grid grid, Position pos)
return strcmp(grid_get(grid, pos), " ") != 0;
}
-void add_neighbour(Position neighbours[], uint8_t *neighbour_cnt, Position neighbour_pos)
+void add_neighbour(Position neighbours[3], unsigned int *neighbour_cnt,
+ Position neighbour_pos)
{
neighbours[*neighbour_cnt] = neighbour_pos;
(*neighbour_cnt)++;
}
-void get_neighbours(Grid grid, Position pos, Position neighbours[],
- uint8_t *neighbour_cnt)
+void get_neighbours(Grid grid, Position pos, Position neighbours[3],
+ unsigned int *neighbour_cnt)
{
- if (pos.y != 1)
+ if (pos.y != 1U)
{
- Position pos_down = position_create(pos.x, pos.y - 2);
+ Position pos_down = position_create(pos.x, pos.y - 2U);
if (is_pos_empty(grid, pos_down))
add_neighbour(neighbours, neighbour_cnt, pos_down);
}
- if (pos.y != grid.dimens.height - 2)
+ if (pos.y != grid.dimens.height - 2U)
{
- Position pos_up = position_create(pos.x, pos.y + 2);
+ Position pos_up = position_create(pos.x, pos.y + 2U);
if (is_pos_empty(grid, pos_up))
add_neighbour(neighbours, neighbour_cnt, pos_up);
}
- if (pos.x != 1)
+ if (pos.x != 1U)
{
- Position pos_left = position_create(pos.x - 2, pos.y);
+ Position pos_left = position_create(pos.x - 2U, pos.y);
if (is_pos_empty(grid, pos_left))
add_neighbour(neighbours, neighbour_cnt, pos_left);
}
- if (pos.x != grid.dimens.width - 2)
+ if (pos.x != grid.dimens.width - 2U)
{
- Position pos_right = position_create(pos.x + 2, pos.y);
+ Position pos_right = position_create(pos.x + 2U, pos.y);
if (is_pos_empty(grid, pos_right))
add_neighbour(neighbours, neighbour_cnt, pos_right);
@@ -55,12 +55,12 @@ void get_neighbours(Grid grid, Position pos, Position neighbours[],
unsigned int get_maze_size(Grid grid)
{
- return ((grid.dimens.width - 1) / 2) * ((grid.dimens.height - 1) / 2);
+ return ((grid.dimens.width - 1U) / 2U) * ((grid.dimens.height - 1U) / 2U);
}
int is_whole_maze_visited(Grid grid, unsigned int visited_pos_cnt)
{
- return visited_pos_cnt == get_maze_size(grid) - 1;
+ return visited_pos_cnt == get_maze_size(grid) - 1U;
}
void grid_to_maze(Grid grid, Position start_pos)
@@ -69,7 +69,7 @@ void grid_to_maze(Grid grid, Position start_pos)
pos_stack_push(path, start_pos);
- unsigned int visited_pos_cnt = 0;
+ unsigned int visited_pos_cnt = 0U;
while (1)
{
Position pos = pos_stack_peek(path);
@@ -77,11 +77,11 @@ void grid_to_maze(Grid grid, Position start_pos)
grid_set(grid, pos, " ");
Position neighbours[3];
- uint8_t neighbour_cnt = 0;
+ unsigned int neighbour_cnt = 0U;
get_neighbours(grid, pos, neighbours, &neighbour_cnt);
- if (neighbour_cnt == 0)
+ if (neighbour_cnt == 0U)
{
if (is_whole_maze_visited(grid, visited_pos_cnt))
break;
@@ -98,14 +98,26 @@ void grid_to_maze(Grid grid, Position start_pos)
Position between_pos = position_create(pos.x, pos.y);
if (next_pos.y != pos.y)
- between_pos.y += next_pos.y > pos.y ? 1 : -1;
+ {
+ if (next_pos.y > pos.y)
+ between_pos.y += 1U;
+ else
+ between_pos.y -= 1U;
+ }
if (next_pos.x != pos.x)
- between_pos.x += next_pos.x > pos.x ? 1 : -1;
+ {
+ if (next_pos.x > pos.x)
+ between_pos.x += 1U;
+ else
+ between_pos.x -= 1U;
+ }
grid_set(grid, between_pos, " ");
pos_stack_push(path, next_pos);
+
+ // grid_print(grid);
}
pos_stack_destroy(path);
diff --git a/src/mazerator.c b/src/mazerator.c
index c624dc3..9926c1e 100644
--- a/src/mazerator.c
+++ b/src/mazerator.c
@@ -145,7 +145,6 @@ int main(int argc, char *argv[])
}
srand(*seed);
-
free(seed);
if (start_x == NULL)
diff --git a/src/position_stack.c b/src/position_stack.c
index 597ed5a..0b4895a 100644
--- a/src/position_stack.c
+++ b/src/position_stack.c
@@ -41,7 +41,7 @@ void pos_stack_destroy(PositionStack *pos_stack)
void pos_stack_push(PositionStack *pos_stack, Position pos)
{
// Avoid a overflow by checking if the stack is full
- if (pos_stack->top == pos_stack->capacity - 1)
+ if (pos_stack->top == pos_stack->capacity - 1U)
{
stack_error(STACK_ERR_OVERFLOW);
}
diff --git a/src/utils.c b/src/utils.c
index 5fc27d3..fa33cb0 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -29,7 +29,7 @@ unsigned int str_to_uint(char *str, char **err)
char *str_waste;
unsigned long num = strtoul(str, &str_waste, 10);
- if (strlen(str_waste) != 0)
+ if (strlen(str_waste) != 0UL)
{
*err = "Not a number";
return 0;