-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_valid.c
110 lines (99 loc) · 3.08 KB
/
ft_valid.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_valid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmorrain <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/05/12 13:57:01 by pmorrain #+# #+# */
/* Updated: 2017/05/28 17:08:38 by alalaoui ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
int ft_check_tmino(char *tmino)
{
int i;
int hash;
int dot;
int n;
i = 0;
n = 0;
hash = 0;
dot = 0;
if (ft_strlen(tmino) != 19)
return (0);
while (tmino[i])
{
if (tmino[i] == '.')
dot++;
else if (tmino[i] == '#')
hash++;
else if (tmino[i] == '\n')
n++;
i++;
}
if (hash != 4 || dot != 12 || n != 3)
return (0);
return (1);
}
int ft_check_tab(char **ttristab)
{
int i;
i = 0;
while (ttristab[i])
{
if (!ft_check_tmino(ttristab[i]))
return (0);
ft_tmino_to_upperleft(ttristab[i]);
i++;
}
return (1);
}
int ft_is_valid(char **ttristab, int tablen)
{
int i;
i = 0;
while (ttristab[i] && ft_is_tetrimino(ft_convert_to_nb(ttristab[i])))
i++;
if ((i == tablen) && ft_check_tab(ttristab))
return (1);
return (0);
}
int ft_is_tetrimino(int nb)
{
int s;
s = ft_simplify_tmino(nb);
if (s == N_SQUARE || s == N_HORI_LINE || s == N_VERT_LINE || s == N_T0
|| s == N_T90 || s == N_T180 || s == N_T270 || s == N_L0
|| s == N_L90 || s == N_L180 || s == N_L270 || s == N_SYM_L0
|| s == N_SYM_L90 || s == N_SYM_L180 || s == N_SYM_L270
|| s == N_S0 || s == N_S90 || s == N_Z0 || s == N_Z90)
return (s);
else
return (0);
}
int ft_tmino_to_upperleft(char *tmino)
{
int s;
s = ft_simplify_tmino(ft_convert_to_nb(tmino));
(s == N_SQUARE) ? ft_strcpy(tmino, S_SQUARE) : 0;
(s == N_HORI_LINE) ? ft_strcpy(tmino, S_HORI_LINE) : 0;
(s == N_VERT_LINE) ? ft_strcpy(tmino, S_VERT_LINE) : 0;
(s == N_T0) ? ft_strcpy(tmino, S_T0) : 0;
(s == N_T90) ? ft_strcpy(tmino, S_T90) : 0;
(s == N_T180) ? ft_strcpy(tmino, S_T180) : 0;
(s == N_T270) ? ft_strcpy(tmino, S_T270) : 0;
(s == N_L0) ? ft_strcpy(tmino, S_L0) : 0;
(s == N_L90) ? ft_strcpy(tmino, S_L90) : 0;
(s == N_L180) ? ft_strcpy(tmino, S_L180) : 0;
(s == N_L270) ? ft_strcpy(tmino, S_L270) : 0;
(s == N_SYM_L0) ? ft_strcpy(tmino, S_SYM_L0) : 0;
(s == N_SYM_L90) ? ft_strcpy(tmino, S_SYM_L90) : 0;
(s == N_SYM_L180) ? ft_strcpy(tmino, S_SYM_L180) : 0;
(s == N_SYM_L270) ? ft_strcpy(tmino, S_SYM_L270) : 0;
(s == N_S0) ? ft_strcpy(tmino, S_S0) : 0;
(s == N_S90) ? ft_strcpy(tmino, S_S90) : 0;
(s == N_Z0) ? ft_strcpy(tmino, S_Z0) : 0;
(s == N_Z90) ? ft_strcpy(tmino, S_Z90) : 0;
return (s);
}