-
Notifications
You must be signed in to change notification settings - Fork 1
/
tetrimino.c
131 lines (118 loc) · 2.4 KB
/
tetrimino.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
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tetrimino.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sadamant <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/07 12:25:10 by sadamant #+# #+# */
/* Updated: 2017/11/07 12:30:06 by sadamant ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
int count_hash(char *t)
{
int count;
count = 0;
while (*t)
{
if (*t++ == '#')
count++;
}
if (count != 4)
return (0);
return (1);
}
int connections(char *t)
{
int i;
int connect;
i = 0;
connect = 0;
while (t[i])
{
if (t[i] == '#')
{
if ((i + 1 < 20) && t[i + 1] == '#')
connect++;
if ((i - 1 >= 0) && t[i - 1] == '#')
connect++;
if ((i + 5 < 20) && t[i + 5] == '#')
connect++;
if ((i - 5 >= 0) && t[i - 5] == '#')
connect++;
}
i++;
}
return (connect == 6 || connect == 8);
}
/*
** input: unreduced tetrimino string
** checks the coordinate of the # to see if it's a min or a max
** output: width of the tetrimino
*/
int tet_width(char *t)
{
int x;
int l;
int r;
x = 0;
l = 4;
r = 0;
while (*t)
{
if (x % 5 == 0)
x = 0;
if (*t++ == '#')
{
if (x < l)
l = x;
if (x > r)
r = x;
}
x++;
}
return (r - l + 1);
}
/*
** find the leftmost relevant index of the tetrimino
*/
static int leftmost(char *t)
{
int x;
int y;
int l;
x = 0;
y = (ft_strchr(t, '#') - t) / 5;
l = 4;
while (*t)
{
if (x % 5 == 0)
x = 0;
if (*t++ == '#' && x < l)
l = x;
x++;
}
return (l + y * 5);
}
char *reduce_tetrimino(char *t)
{
int i;
int count;
char *reduced;
char *reducedcpy;
i = leftmost(t);
count = 0;
reduced = (char *)malloc(sizeof(char) * 14);
reducedcpy = reduced;
while (t[i] && count < 4)
{
if (t[i] == '\n')
i++;
if (t[i] == '#')
count++;
*reduced++ = t[i++];
}
*reduced = '\0';
return (reducedcpy);
}