Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
check memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
sneikki committed Sep 7, 2021
1 parent f64ae75 commit b6d200e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
18 changes: 12 additions & 6 deletions game.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ int run(void)
timeout(1000.0 / INPUT_RATE);

generate_apple(ARENA_WIDTH, ARENA_HEIGHT);
init_snake();
if (!init_snake()) {
return QUIT;
}

while (true) {
if (process() == QUIT) {
Expand Down Expand Up @@ -56,7 +58,9 @@ int process(void)
break;
}

update();
if (!update()) {
return QUIT;
}

return RUNNING;
}
Expand Down Expand Up @@ -89,7 +93,7 @@ int input(void)
return RUNNING;
}

void update(void)
int update(void)
{
if (!check_win_size()) {

Expand All @@ -112,7 +116,9 @@ void update(void)
if (!(state == PAUSE || state == GAME_OVER || state == INTERRUPT)) {
if (grow_snake_next) {
grow_snake_next = 0;
grow_snake();
if (!grow_snake()) {
return 0;
}
} else {
update_snake();
}
Expand All @@ -137,17 +143,17 @@ void update(void)
draw();
refresh();
}

return 1;
}

void draw(void)
{
draw_arena(ARENA_WIDTH, ARENA_HEIGHT);

/* print score */
sprintf(text_buffer, "\n\n Score: %d", score);
draw_text(text_buffer);

// sprintf(text_buffer, "\nr - restart\nq - quit");
draw_text("\n\n r - restart\n q - quit");

if (state == GAME_OVER) {
Expand Down
2 changes: 1 addition & 1 deletion game.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum {
int run(void);
int process(void);
int input(void);
void update(void);
int update(void);
void draw(void);

#endif
13 changes: 11 additions & 2 deletions snake.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
queue snake;
int snake_dir;

void init_snake(void)
int init_snake(void)
{
struct node *nodes[SNAKE_MIN_LENGTH];
int i, x, y;
Expand All @@ -15,6 +15,9 @@ void init_snake(void)

for (i = 0; i < SNAKE_MIN_LENGTH; i++) {
nodes[i] = (struct node *) malloc(sizeof(struct node));
if (!nodes[i]) {
return 0;
}

nodes[i]->x = x + i;
nodes[i]->y = y;
Expand All @@ -23,6 +26,7 @@ void init_snake(void)
}

snake_dir = RIGHT;
return 1;
}

void empty_snake(void)
Expand Down Expand Up @@ -90,10 +94,15 @@ void insert_snake_node(struct node *new)
}
}

void grow_snake(void)
int grow_snake(void)
{
struct node *new = (struct node *) malloc(sizeof(struct node));
if (!new) {
return 0;
}

insert_snake_node(new);
return 1;
}

int snake_has_eaten(int apple_x, int apple_y)
Expand Down
4 changes: 2 additions & 2 deletions snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ enum direction {
extern queue snake;
extern int snake_dir;

void init_snake(void);
int init_snake(void);
void empty_snake(void);
void update_snake(void);
void turn_snake(int direction, int opposite, int *dir_changed);
void grow_snake(void);
int grow_snake(void);
void insert_snake_node(struct node *new);
int snake_has_eaten(int apple_x, int apple_y);
int check_snake_location(int arena_Width, int arena_height);
Expand Down

0 comments on commit b6d200e

Please sign in to comment.