-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.c
67 lines (60 loc) · 1.91 KB
/
grid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* LE - / */
/* / */
/* grid.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: fakl <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2019/03/22 10:48:23 by davfelix #+# ## ## #+# */
/* Updated: 2019/04/25 19:53:18 by davfelix ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "fillit.h"
int only_sqr(void)
{
int j;
j = 0;
while (g_sf->typ_f[j] == 7)
j++;
if (j == g_sf->nb_tetris)
return (1);
return (0);
}
void define_size(int size_plus)
{
g_sf->size = ft_sqrt((g_sf->nb_tetris) * 4) + size_plus;
if (only_sqr() && g_sf->size % 2 == 1)
g_sf->size++;
}
int malloc_grid(int row, int col)
{
int c;
if (!(g_sf->grid = ((char **)malloc(sizeof(char *) * (g_sf->size + 1)))))
return (0);
c = g_sf->size + 1;
while (row < g_sf->size)
{
if (!(g_sf->grid[row] = ((char *)malloc(sizeof(char) * (c + 1)))))
return (0);
while (col < c - 1)
{
g_sf->grid[row][col] = '.';
col++;
}
g_sf->grid[row][col] = '\n';
g_sf->grid[row][col + 1] = '\0';
row++;
col = 0;
}
g_sf->grid[row] = (char *)malloc(c + 1);
ft_memset(g_sf->grid[row], '\0', c + 1);
return (1);
}
int create_grid(int size_plus, int row, int col)
{
if (g_sf->grid != NULL)
free_grid();
define_size(size_plus);
return (malloc_grid(row, col));
}