-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_op.c
119 lines (105 loc) · 2.35 KB
/
map_op.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_op.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpetruse <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/06/29 14:20:39 by mpetruse #+# #+# */
/* Updated: 2018/07/02 17:40:34 by mpetruse ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "libft.h"
void print_map(t_map *map)
{
int i;
i = 0;
while (i < map->size)
{
ft_putstr(map->array[i]);
ft_putchar('\n');
i++;
}
}
void del_map(t_map *map)
{
int i;
i = 0;
while (i < map->size)
{
ft_memdel((void **)&(map->array[i]));
i++;
}
ft_memdel((void **)&(map->array));
ft_memdel((void **)&map);
}
/*
** Allocates a new map structure with specified size.
*/
t_map *newmap(int size)
{
t_map *map;
int i;
int j;
map = (t_map *)ft_memalloc(sizeof(t_map));
map->size = size;
map->array = (char **)ft_memalloc(sizeof(char *) * size);
i = 0;
while (i < size)
{
map->array[i] = ft_strnew(size);
j = 0;
while (j < size)
{
map->array[i][j] = '.';
j++;
}
i++;
}
return (map);
}
/*
**Places a tetrimino on map at specified position,
**checking if the placement is possible.
*/
int place(t_block *tetri, t_map *map, int w, int h)
{
int i;
int j;
i = 0;
while (i < tetri->x)
{
j = 0;
while (j < tetri->y)
{
if (tetri->shape[j][i] == '#' && map->array[h + j][w + i] != '.')
return (0);
j++;
}
i++;
}
set_piece(tetri, map, new_size(w, h), tetri->val);
return (1);
}
/*
** when sees # sign changes it to the letter later
*/
void set_piece(t_block *tetri, t_map *map, t_dot *dot, char c)
{
int i;
int j;
i = 0;
while (i < tetri->x)
{
j = 0;
while (j < tetri->y)
{
if (tetri->shape[j][i] == '#')
map->array[dot->y + j][dot->x + i] = c;
j++;
}
i++;
}
ft_memdel((void **)&dot);
}