-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_right.c
78 lines (71 loc) · 1.82 KB
/
move_right.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move_right.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/21 19:31:08 by dskrypny #+# #+# */
/* Updated: 2018/07/22 17:28:09 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "header.h"
static short find_pair_right(int mas[4][4], int x, int y)
{
short step;
step = 1;
while ((x - step) >= 0 && mas[x - step][y] == 0)
step++;
if ((x - step >= 0) && mas[x - step][y] == mas[x][y])
{
mas[x][y] *= 2;
mas[x - step][y] = 0;
return (mas[x][y]);
}
return (0);
}
static void push_all_right(int mas[4][4], int *x)
{
short step;
short i;
short j;
i = 4;
while (--i >= 0)
{
j = -1;
while (++j < 4)
{
step = 0;
if (mas[i][j] == 0)
while (i - step >= 0 && !mas[i - step][j])
step++;
if (step && i - step >= 0)
{
mas[i][j] = mas[i - step][j];
mas[i - step][j] = 0;
*x += 1;
}
}
}
}
int move_right(int mas[4][4], t_result *res)
{
short i;
short j;
int x;
x = 0;
i = 4;
while (--i >= 0)
{
j = -1;
while (++j < 4)
{
if (mas[i][j] != 0)
x += find_pair_right(mas, i, j);
}
}
res->result += x;
push_all_right(mas, &x);
print_result(res);
return (x);
}