-
Notifications
You must be signed in to change notification settings - Fork 0
/
moves_posi.c
107 lines (97 loc) · 2.52 KB
/
moves_posi.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* moves_posi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fraqioui <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/24 17:01:57 by fraqioui #+# #+# */
/* Updated: 2022/12/24 18:37:35 by fraqioui ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void ft_moves_1(t_node **stack_a, t_node **stack_b, t_node *node)
{
int i;
i = 0;
while (i++ < (-1 * node->pos_a))
{
ft_rev_rotate_a_b(stack_a, stack_b);
write(1, "rrr\n", 4);
}
i = 0;
while (i++ < ((-1 * node->index) + node->pos_a))
{
ft_rev_rotate_b(stack_b);
write(1, "rrb\n", 4);
}
}
static void ft_moves_2(t_node **stack_a, t_node **stack_b, t_node *node)
{
int i;
i = 0;
while (i++ < (-1 * node->index))
{
ft_rev_rotate_a_b(stack_a, stack_b);
write(1, "rrr\n", 4);
}
i = 0;
while (i++ < ((-1 * node->pos_a) + node->index))
{
ft_rev_rotate_a(stack_a);
write(1, "rra\n", 4);
}
}
static void ft_moves_3(t_node **stack_a, t_node **stack_b, t_node *node)
{
int i;
i = 0;
while (i++ < node->pos_a)
{
ft_rotate_a_b(stack_a, stack_b);
write(1, "rr\n", 3);
}
i = 0;
while (i++ < (node->index - node->pos_a))
{
ft_rotate_b(stack_b);
write(1, "rb\n", 3);
}
}
static void ft_moves_4(t_node **stack_a, t_node **stack_b, t_node *node)
{
int i;
i = 0;
while (i++ < node->index)
{
ft_rotate_a_b(stack_a, stack_b);
write(1, "rr\n", 3);
}
i = 0;
while (i++ < (node->pos_a - node->index))
{
ft_rotate_a(stack_a);
write(1, "ra\n", 3);
}
}
void ft_apply_moves_a_same(t_node **stack_a, t_node **stack_b, t_node *node)
{
int i;
i = 0;
if (!stack_a || !*stack_a || !stack_b || !*stack_b)
return ;
if (node->index < 0)
{
if (node->index <= node->pos_a)
ft_moves_1(stack_a, stack_b, node);
else
ft_moves_2(stack_a, stack_b, node);
}
else if (node->index > 0)
{
if (node->index >= node->pos_a)
ft_moves_3(stack_a, stack_b, node);
else
ft_moves_4(stack_a, stack_b, node);
}
}