Compare commits

...

2 Commits

Author SHA1 Message Date
hsv2 a73cbf8773 show op name on monitor 2022-09-27 17:32:42 +02:00
hsv2 2950db9c45 libft update 2022-09-27 16:00:07 +02:00
8 changed files with 180 additions and 43 deletions

View File

@ -20,6 +20,7 @@ STACK_SRC = src/stack_attr.c \
LSTACK = $(addprefix $(DIST_DIR), libftstack.a)
PUSHWAP_SRC = src/push_swap.c \
src/push_swap_ctx.c \
src/push_swap_op.c \
src/push_swap_sort.c \
src/rotate.c \
src/reverse_rotate.c

View File

@ -5,70 +5,73 @@
#include <curses.h>
#include <push_swap.h>
#include "../src/push_swap_ctx.h"
#include "tui.h"
#define STACK_SIZE 20
static WINDOW *wgraph_a;
static WINDOW *wgraph_b;
static WINDOW *wopname;
static WINDOW *graph_a;
static WINDOW *graph_b;
static int max, min;
t_stack *genstack(void)
{
t_stack *s = stack_new(STACK_SIZE, sizeof (int));
for (int i = 0; i < STACK_SIZE; i++)
{
int val = rand() - rand();
stack_push(s, &val);
}
return s;
}
void plot_graph_a(t_stack_view s)
{
plot_graph(graph_a, s, max, min, 2);
wrefresh(graph_a);
plot_graph(wgraph_a, s, max, min, 2);
}
void plot_graph_b(t_stack_view s)
{
plot_graph(graph_b, s, max, min, 2);
wrefresh(graph_b);
plot_graph(wgraph_b, s, max, min, 2);
}
void display_op(t_push_swap_op op)
{
print_op(wopname, op);
}
void refresh_windows()
{
wrefresh(wgraph_a);
wrefresh(wgraph_b);
wrefresh(wopname);
}
int push_swap_cb(t_stack_view a, t_stack_view b, t_push_swap_op op)
{
plot_graph_a(a);
plot_graph_b(b);
display_op(op);
refresh_windows();
usleep(250000);
return 1;
}
int main(void)
int main(int c, char **v)
{
srand(time(NULL));
t_push_swap_ctx *ctx = NULL;
while (!ctx)
ctx = push_swap_ctx_new(genstack());
if (c < 2)
return (-1);
t_stack *stack_a = push_swap_stack_from_args(&v[1], c - 1);
if (!stack_a)
return (-2);
t_push_swap_ctx *ctx = push_swap_ctx_new(stack_a);
if (!ctx)
return (-3);
stack_int_maxmin(ctx->stack_a, &max, &min);
initscr();
cbreak();
noecho();
refresh();
int h, w;
getmaxyx(stdscr, h, w);
graph_a = newwin(LINES, COLS / 2 - 1, 0, 0);
box(graph_a, 0, 0);
graph_b = newwin(LINES, COLS / 2 - 1, 0, COLS / 2);
box(graph_b, 0, 0);
wgraph_a = newwin(LINES - 3, COLS / 2 - 1, 0, 0);
wgraph_b = newwin(LINES - 3, COLS / 2 - 1, 0, COLS / 2);
box(wgraph_b, 0, 0);
wopname = newwin(3, COLS, LINES - 3, 0);
box(wgraph_b, 0, 0);
push_swap_sort_step(ctx, push_swap_cb);
delwin(graph_a);
push_swap_ctx_del(ctx);
delwin(wgraph_a);
endwin();
return (0);
}

View File

@ -1,14 +1,17 @@
#include "tui.h"
#include "libft.h"
#include "push_swap.h"
#include <ncurses.h>
#include <ft_stack.h>
void plot_graph(WINDOW *win, const t_stack_view s, int max, int min, int offset)
void plot_graph(WINDOW *win, t_stack_view s, int max, int min, int offset)
{
if (!s.raw)
return;
werase(win);
box(win, 0, 0);
int h, w;
getmaxyx(win, h, w);
w -= 2 * offset;
@ -19,11 +22,20 @@ void plot_graph(WINDOW *win, const t_stack_view s, int max, int min, int offset)
const size_t item_n = lerp(0, s.maxheight, linear(0, w, x));
const int value = arr_int_get(s.raw, item_n);
const int bar_h = lerp(0, h, linear(min, max, value));
for (int y = 0; y <= h; y++)
for (int y = 0; item_n < s.top && y <= h && bar_h >= y; y++)
{
mvwaddch(win, h - y + offset, x + offset, ' ');
if (item_n < s.top && bar_h >= y)
mvwaddch(win, h - y + offset, x + offset, ACS_BLOCK);
mvwaddch(win, h - y + offset, x + offset, ACS_BLOCK);
}
}
}
void print_op(WINDOW *win, t_push_swap_op op)
{
int h, w;
getmaxyx(win, h, w);
werase(win);
box(win, 0, 0);
const char *const opname = push_swap_name_from_op(op);
mvwaddstr(win, h / 2, (w - ft_strlen(opname)) / 2, opname);
}

View File

@ -1,7 +1,8 @@
#ifndef TUI_H
#define TUI_H
typedef struct s_stack_view t_stack_view;
#include <push_swap.h>
typedef struct _win_st WINDOW;
static inline float lerp(float a, float b, float t)
@ -14,6 +15,7 @@ static inline float linear(float a, float b, float t)
return (t - a) / (b - a);
}
void plot_graph(WINDOW *w, const t_stack_view s, int max, int min, int offset);
void plot_graph(WINDOW *w, t_stack_view s, int max, int min, int offset);
void print_op(WINDOW *w, t_push_swap_op);
#endif

View File

@ -30,10 +30,12 @@ typedef enum e_push_swap_op
ps_op_sa,
ps_op_sb,
ps_op_ss,
ps_op_undefined,
} t_push_swap_op;
t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a);
void *push_swap_ctx_del(t_push_swap_ctx *ctx);
t_stack *push_swap_stack_from_args(char **args, int count);
int push_swap_sort(t_push_swap_ctx *ctx);
int push_swap_sort_step(t_push_swap_ctx *ctx,
@ -52,6 +54,10 @@ void push_swap_sa(t_push_swap_ctx *ctx);
void push_swap_sb(t_push_swap_ctx *ctx);
void push_swap_ss(t_push_swap_ctx *ctx);
void (*push_swap_fn_from_op(t_push_swap_op op))(t_push_swap_ctx*);
const char *push_swap_name_from_op(t_push_swap_op op);
t_push_swap_op push_swap_op_from_name(const char *opname);
#endif
// vi: noet sw=4 ts=4:

2
libft

@ -1 +1 @@
Subproject commit e60990f2d7796adaf37a1870453312b86a0fec03
Subproject commit 6ccf4951e806f84676dcd648c2b6ebb7781500d4

View File

@ -16,6 +16,7 @@
#include <libft.h>
#include "ft_stack.h"
#include "push_swap_ctx.h"
t_push_swap_ctx *push_swap_ctx_new(t_stack *stack_a)
@ -41,4 +42,20 @@ void *push_swap_ctx_del(t_push_swap_ctx *ctx)
free(ctx);
return (NULL);
}
t_stack *push_swap_stack_from_args(char **args, int count)
{
t_stack *s;
int nbr;
s = stack_new(count, sizeof (int));
while (s && count-- > 0)
{
if (ft_atoi_strict(args[count], &nbr) == 0)
stack_push(s, &nbr);
else
return (stack_del(s));
}
return (s);
}
// vi: noet sw=4 ts=4:

96
src/push_swap_op.c Normal file
View File

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* */
/* */
/* */
/* By: ablanken <ablanken at student dot 42barcelona dot com */
/* */
/* Created: foo bar by ablanken */
/* Updated: foo bar by Andrea Blanke */
/* */
/* ************************************************************************** */
#include <push_swap.h>
#include <libft.h>
void (*push_swap_fn_from_op(t_push_swap_op op))(t_push_swap_ctx*)
{
if (op == ps_op_pa)
return (push_swap_pa);
else if (op == ps_op_pb)
return (push_swap_pb);
else if (op == ps_op_ra)
return (push_swap_ra);
else if (op == ps_op_rb)
return (push_swap_rb);
else if (op == ps_op_rr)
return (push_swap_rr);
else if (op == ps_op_rra)
return (push_swap_rra);
else if (op == ps_op_rrb)
return (push_swap_rrb);
else if (op == ps_op_rrr)
return (push_swap_rrr);
else if (op == ps_op_sa)
return (push_swap_sa);
else if (op == ps_op_sb)
return (push_swap_sb);
else if (op == ps_op_ss)
return (push_swap_ss);
return (NULL);
}
const char *push_swap_name_from_op(t_push_swap_op op)
{
if (op == ps_op_pa)
return "push a";
else if (op == ps_op_pb)
return "push b";
else if (op == ps_op_ra)
return "rotate a";
else if (op == ps_op_rb)
return "rotate b";
else if (op == ps_op_rr)
return "rotate both";
else if (op == ps_op_rra)
return "reverse rotate a";
else if (op == ps_op_rrb)
return "reverse rotate b";
else if (op == ps_op_rrr)
return "reverse rotate both";
else if (op == ps_op_sa)
return "swap a";
else if (op == ps_op_sb)
return "swap b";
else if (op == ps_op_ss)
return "swap both";
return ("Unknown");
}
t_push_swap_op push_swap_op_from_name(const char *opname)
{
if (ft_strncmp(opname, "pa", 4))
return (ps_op_pa);
else if (ft_strncmp(opname, "pb", 4))
return (ps_op_pb);
else if (ft_strncmp(opname, "ra", 4))
return (ps_op_ra);
else if (ft_strncmp(opname, "rb", 4))
return (ps_op_rb);
else if (ft_strncmp(opname, "rr", 4))
return (ps_op_rr);
else if (ft_strncmp(opname, "rra", 4))
return (ps_op_rra);
else if (ft_strncmp(opname, "rrb", 4))
return (ps_op_rrb);
else if (ft_strncmp(opname, "rrr", 4))
return (ps_op_rrr);
else if (ft_strncmp(opname, "sa", 4))
return (ps_op_sa);
else if (ft_strncmp(opname, "sb", 4))
return (ps_op_sb);
else if (ft_strncmp(opname, "ss", 4))
return (ps_op_ss);
return (ps_op_undefined);
}
// vi: noet sw=4 ts=4: