Compare commits

..

2 Commits

3 changed files with 9 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
#include "config.h" #include "config.h"
#include "Board.h" #include "Board.h"
#include "Snake.h" #include "Snake.h"
#include "utils.h"
#define MAT_INDEX(mat, w, i, j) (mat)[(j) + (w) * (i)] #define MAT_INDEX(mat, w, i, j) (mat)[(j) + (w) * (i)]
@@ -16,6 +17,9 @@ Board board_alloc(const int width, const int height) {
board.width_with_borders = board.width + 2; board.width_with_borders = board.width + 2;
board.height_with_borders = board.height + 2; board.height_with_borders = board.height + 2;
board.squares = (char*) malloc(sizeof(char) * board.width_with_borders * board.height_with_borders); board.squares = (char*) malloc(sizeof(char) * board.width_with_borders * board.height_with_borders);
if (board.squares == NULL) {
mallocError("board.squares", __FILE__, "board_alloc");
}
for (size_t i = 1; i < board.height_with_borders; i++) { for (size_t i = 1; i < board.height_with_borders; i++) {
for (size_t j = 1; j < board.width_with_borders; j++) { for (size_t j = 1; j < board.width_with_borders; j++) {

View File

@@ -3,6 +3,7 @@
#include <assert.h> #include <assert.h>
#include "Snake.h" #include "Snake.h"
#include "utils.h"
extern const char snake_vis; extern const char snake_vis;
@@ -31,6 +32,9 @@ Snake snake_alloc(
snake.length = 1; snake.length = 1;
snake.dir = init_dir; snake.dir = init_dir;
snake.parts = (BoardPiece*) malloc(sizeof(BoardPiece) * snake.max_length); snake.parts = (BoardPiece*) malloc(sizeof(BoardPiece) * snake.max_length);
if (snake.parts == NULL) {
mallocError("snake.parts", __FILE__, "snake_alloc");
}
snake.parts[0] = (BoardPiece) { .x = init_x, .y = init_y, .vis_char = '&' }; snake.parts[0] = (BoardPiece) { .x = init_x, .y = init_y, .vis_char = '&' };
return snake; return snake;

View File

@@ -18,6 +18,7 @@ int randomInt(const int start, const int end, const unsigned int seed) {
return result; return result;
} }
// TODO: Make a general error logging function and macro
void mallocError(const char* varName, const char* fileName, const char* functionName) { void mallocError(const char* varName, const char* fileName, const char* functionName) {
printf("Ran out of memory to allocate to %s in %s/%s\n", varName, fileName, functionName); printf("Ran out of memory to allocate to %s in %s/%s\n", varName, fileName, functionName);
exit(1); exit(1);