-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize_lst.c
78 lines (69 loc) · 2.16 KB
/
normalize_lst.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* normalize_lst.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nde-maes <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/17 14:33:53 by nde-maes #+# #+# */
/* Updated: 2019/01/07 12:46:08 by nde-maes ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
/*
** Normalization consists of 2 things:
** - moving the tetraminos to the top left corner
** - replacing its '#' with the letter corresponding to the order of which
** the tetraminos appeared
*/
void replace_tet_hash_by_letter(t_tet *node, char c)
{
int line;
int col;
line = -1;
while (node->piece[++line])
{
col = -1;
while (node->piece[line][++col])
if (node->piece[line][col] != '.')
node->piece[line][col] = c;
}
}
/*
** Returns the max number of blocks in a row in a tetriminos.
** Possible values are: 4 (for a line), 3 and 2 (for a square).
*/
int get_max_blocks_aligned(char **tet)
{
int counter;
counter = -1;
while (++counter < 4)
if (tet[counter][3] != '.' || tet[3][counter] != '.')
return (4);
counter = -1;
while (++counter < 4)
if (tet[counter][2] != '.' || tet[2][counter] != '.')
return (3);
return (2);
}
int normalize_lst(t_tet **head)
{
t_tet *node;
int counter;
char letter;
int max;
counter = 0;
node = *head;
max = 2;
while (node)
{
letter = ++counter + 64;
move_tet_top_left(node->piece);
if (get_max_blocks_aligned(node->piece) > max)
max = get_max_blocks_aligned(node->piece);
replace_tet_hash_by_letter(node, letter);
node->letter = letter;
node = node->next;
}
return (max);
}