-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.c
111 lines (100 loc) · 2.43 KB
/
output.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* output.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kceder <kceder@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/08 14:04:21 by kceder #+# #+# */
/* Updated: 2022/02/08 14:04:25 by kceder ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
void fill_grid(char **grid, unsigned short *tetrimino)
{
unsigned short andgate;
unsigned short z;
unsigned short x;
unsigned short row;
row = 0;
z = 0;
x = 32768;
andgate = 0;
while (z <= 15)
{
if (z % 4 == 0 && z != 0)
row++;
andgate = tetrimino[9] & (x >> z);
if (andgate > 0)
grid[tetrimino[7] + row][tetrimino[8] + z - row * 4] = tetrimino[6];
z++;
}
}
void print_result(char **grid, unsigned short sqrsize)
{
unsigned short i;
i = 0;
while (i < sqrsize)
{
ft_putendl(grid[i]);
i++;
}
}
void make_grid(unsigned short **tetriminos, unsigned short sqrsize,
int tetri_count, unsigned short i)
{
char **grid;
grid = (char **)malloc(sizeof(char *) * (sqrsize + 1));
grid[sqrsize] = NULL;
if (!grid)
return ;
while (i < sqrsize)
{
grid[i] = ft_strnew(sqrsize);
if (!grid[i])
{
ft_free_array(grid);
return ;
}
ft_memset(grid[i], '.', sqrsize);
i++;
}
i = 0;
while (i < tetri_count)
{
fill_grid(grid, tetriminos[i]);
i++;
}
print_result(grid, sqrsize);
ft_free_array(grid);
}
int output(unsigned short sqrsize, unsigned short **tetriminos)
{
int i;
int tetri_count;
i = 0;
tetri_count = 0;
while (tetriminos[i])
{
tetri_count++;
i++;
}
i = 0;
make_grid(tetriminos, sqrsize, tetri_count, 0);
return (1);
}
unsigned short *init_tetrimonos(int count)
{
unsigned short *tetriminos;
int i;
tetriminos = (unsigned short *)malloc(sizeof(unsigned short) * count);
if (!tetriminos)
return (0);
i = 0;
while (i < count)
{
tetriminos[i] = 0;
i++;
}
return (tetriminos);
}