182 lines
3.9 KiB
C
182 lines
3.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <MLV/MLV_all.h>
|
|
#include "grid.h"
|
|
#include "snake.h"
|
|
|
|
Grid *allocate_grid(int n, int m)
|
|
{
|
|
Grid *g = malloc(sizeof(Grid));
|
|
int i;
|
|
if (!g)
|
|
{
|
|
fprintf(stderr, "Error: could not allocate Grid.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
g->nbl = n;
|
|
g->nbc = m;
|
|
g->grid = malloc(n * sizeof(char *));
|
|
if (!g->grid)
|
|
{
|
|
fprintf(stderr, "Error: could not allocate g->grid.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
for (i = 0; i < n; i++)
|
|
{
|
|
g->grid[i] = calloc(m + 2, sizeof(char));
|
|
if (!g->grid[i])
|
|
{
|
|
fprintf(stderr, "Error: could not allocate row.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
return g;
|
|
}
|
|
|
|
void free_grid(Grid *g)
|
|
{
|
|
int i;
|
|
if (!g)
|
|
return;
|
|
for (i = 0; i < g->nbl; i++)
|
|
{
|
|
free(g->grid[i]);
|
|
}
|
|
free(g->grid);
|
|
free(g);
|
|
}
|
|
|
|
void debug(Grid *g)
|
|
{
|
|
int i;
|
|
for (i = 0; i < g->nbl; i++)
|
|
{
|
|
printf("%s\n", g->grid[i]);
|
|
}
|
|
}
|
|
|
|
int compute_size(Grid *g, int w, int h)
|
|
{
|
|
int size_width = w / g->nbc;
|
|
int size_height = h / g->nbl;
|
|
return (size_width < size_height) ? size_width : size_height;
|
|
}
|
|
|
|
void draw_grid(Grid *g)
|
|
{
|
|
int i, j;
|
|
int window_width = MLV_get_window_width();
|
|
int window_height = MLV_get_window_height();
|
|
int cell_size = compute_size(g, window_width, window_height);
|
|
|
|
MLV_draw_filled_rectangle(0, 0, window_width, window_height, MLV_COLOR_BLACK);
|
|
|
|
for (i = 0; i < g->nbl; i++)
|
|
{
|
|
for (j = 0; j < g->nbc; j++)
|
|
{
|
|
int x = j * cell_size;
|
|
int y = i * cell_size;
|
|
|
|
switch (g->grid[i][j])
|
|
{
|
|
case WALL:
|
|
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_BROWN);
|
|
break;
|
|
case EMPTY:
|
|
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_WHITE);
|
|
break;
|
|
case FRUIT:
|
|
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_RED);
|
|
break;
|
|
case SNAKE:
|
|
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_GREEN);
|
|
break;
|
|
default:
|
|
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_BLACK);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void place_snake(Grid *g, struct SnakeStruct *snake)
|
|
{
|
|
Position *current = snake->segments_list;
|
|
|
|
while (current != NULL)
|
|
{
|
|
g->grid[current->y][current->x] = SNAKE;
|
|
current = current->next;
|
|
}
|
|
}
|
|
|
|
Element move_snake(struct SnakeStruct *snake, Grid *g)
|
|
{
|
|
Position *tail = snake->segments_list;
|
|
Position *head;
|
|
Element element_at_head;
|
|
|
|
while (tail->next != NULL) {
|
|
tail = tail->next;
|
|
}
|
|
|
|
g->grid[tail->y][tail->x] = EMPTY;
|
|
|
|
crawl(snake, g);
|
|
|
|
head = snake->segments_list;
|
|
element_at_head = g->grid[head->y][head->x];
|
|
|
|
g->grid[head->y][head->x] = SNAKE;
|
|
|
|
return element_at_head;
|
|
}
|
|
|
|
int count_nb_lines(FILE *stream)
|
|
{
|
|
int count = 0;
|
|
char buffer[256];
|
|
while (fgets(buffer, sizeof(buffer), stream))
|
|
{
|
|
count++;
|
|
}
|
|
rewind(stream);
|
|
return count;
|
|
}
|
|
|
|
int count_fruits(FILE *stream, Grid *g)
|
|
{
|
|
int i, j, nb_fruit = 0;
|
|
|
|
for (i = 0; i < g->nbl; i++)
|
|
{
|
|
if (!fgets(g->grid[i], g->nbc + 2, stream))
|
|
{
|
|
fprintf(stderr, "Error: Stream does not contain enough lines\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
g->grid[i][strcspn(g->grid[i], "\n")] = '\0';
|
|
|
|
for (j = 0; j < g->nbc; j++)
|
|
{
|
|
if (g->grid[i][j] == FRUIT)
|
|
{
|
|
nb_fruit++;
|
|
}
|
|
}
|
|
}
|
|
|
|
return nb_fruit;
|
|
}
|
|
|
|
void copy(const char *src, char *dst)
|
|
{
|
|
int i = 0;
|
|
while (src[i] != '\0' && src[i] != '\n')
|
|
{
|
|
dst[i] = src[i];
|
|
i++;
|
|
}
|
|
dst[i] = '\0';
|
|
} |