introduce state var and the moves on the state

This commit is contained in:
whaffman 2024-11-07 14:04:56 +01:00
parent 05daa6c06e
commit 7797fe91c4
6 changed files with 104 additions and 17 deletions

View File

@ -31,4 +31,15 @@ void rotate(t_list **stack);
void reverse_rotate(t_list **stack);
void swap(t_list **stack);
void pa(t_state *state);
void pb(t_state *state);
void sa(t_state *state);
void sb(t_state *state);
void ss(t_state *state);
void ra(t_state *state);
void rb(t_state *state);
void rr(t_state *state);
void rra(t_state *state);
void rrb(t_state *state);
void rrr(t_state *state);
#endif

View File

@ -13,6 +13,18 @@
#include "libft.h"
#include "push_swap.h"
void pa(t_state *state)
{
push(&(state->a), &(state->b));
ft_printf("pa\n");
}
void pb(t_state *state)
{
push(&(state->b), &(state->a));
ft_printf("pb\n");
}
void push(t_list **stack_1, t_list **stack_2)
{
t_list *temp;

View File

@ -11,6 +11,26 @@
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
void rra(t_state *state)
{
reverse_rotate(&(state->a));
ft_printf("rra\n");
}
void rrb(t_state *state)
{
reverse_rotate(&(state->b));
ft_printf("rrb\n");
}
void rrr(t_state *state)
{
reverse_rotate(&(state->a));
reverse_rotate(&(state->b));
ft_printf("rrr\n");
}
void reverse_rotate(t_list **stack)
{

View File

@ -11,6 +11,26 @@
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
void ra(t_state *state)
{
rotate(&(state->a));
ft_printf("ra\n");
}
void rb(t_state *state)
{
rotate(&(state->b));
ft_printf("rb\n");
}
void rr(t_state *state)
{
rotate(&(state->a));
rotate(&(state->b));
ft_printf("rr\n");
}
void rotate(t_list **stack)
{

View File

@ -11,6 +11,26 @@
/* ************************************************************************** */
#include "libft.h"
#include "push_swap.h"
void sa(t_state *state)
{
swap(&(state->a));
ft_printf("sa\n");
}
void sb(t_state *state)
{
swap(&(state->b));
ft_printf("rb\n");
}
void ss(t_state *state)
{
swap(&(state->a));
swap(&(state->b));
ft_printf("ss\n");
}
void swap(t_list **stack)
{

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/04 13:04:11 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/07 13:33:13 by whaffman ### ### ### ### / \ */
/* Updated: 2024/11/07 13:44:28 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
@ -36,36 +36,40 @@ void print_stack(const char *name, t_list *stack)
ft_printf("\n");
}
void print_stacks(t_list *stack_a, t_list *stack_b)
void print_state(t_state *state)
{
ft_printf("\n");
print_stack("A", stack_a);
print_stack("B", stack_b);
print_stack("A", state->a);
print_stack("B", state->b);
ft_printf("===================\n");
}
t_state *initialise_state(t_state **state)
{
*state = malloc(sizeof (t_state));
if (*state)
{
(*state)->a = NULL;
(*state)->b = NULL;
}
return (*state);
}
int main(int argc, char *argv[])
{
t_list *stack_a;
t_list *stack_b;
t_state *state;
stack_a = NULL;
stack_b = NULL;
state = NULL;
if (!initialise_state(&state))
return (1);
while (argc-- > 1)
{
if (!new_element(&stack_a, argv[argc]))
if (!new_element(&(state->a), argv[argc]))
{
ft_putstr_fd("Error\n", 2);
return (1);
}
}
push(&stack_a, &stack_b);
push(&stack_a, &stack_b);
push(&stack_a, &stack_b);
rotate(&stack_a);
reverse_rotate(&stack_b);
print_stacks(stack_a, stack_b);
swap(&stack_a);
print_stacks(stack_a, stack_b);
print_state(state);
return (0);
}