-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqr_utils.c
86 lines (75 loc) · 2.03 KB
/
sqr_utils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sqr_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nde-maes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/17 12:14:40 by nde-maes #+# #+# */
/* Updated: 2019/01/07 11:28:56 by nde-maes ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
/*
** Create a square of dimension `dim` and fills it with '.'.
*/
char **create_empty_sqr(int dim)
{
char **sqr;
int line;
int col;
if (!(sqr = (char**)malloc(sizeof(char*) * (dim + 1))))
error_message();
sqr[dim] = NULL;
line = 0;
while (line < dim)
{
if (!(sqr[line] = (char*)malloc(sizeof(char) * (dim + 1))))
error_message();
sqr[line][dim] = 0;
col = 0;
while (col < dim)
sqr[line][col++] = '.';
line++;
}
return (sqr);
}
/*
** Allocates a square of dimension `dim`, then copies the provided
** tetriminos in it (the tetriminos blocks can't be further than
** index 3).
*/
char **copy_tet_with_dim(char **tet, int dim)
{
char **tet_copy;
int line;
int col;
tet_copy = create_empty_sqr(dim);
line = -1;
while (tet_copy[++line] && line < 4)
{
col = -1;
while (tet_copy[line][++col] && col < 4)
tet_copy[line][col] = tet[line][col];
}
return (tet_copy);
}
void print_sqr(char **sqr)
{
int line;
line = 0;
while (sqr[line])
{
ft_putstr(sqr[line]);
ft_putchar(10);
line++;
}
}
void free_sqr(char **sqr)
{
int line;
line = 0;
while (sqr[line])
free(sqr[line++]);
free(sqr);
}