Skip to content

Commit

Permalink
Handle [-v N ...]; close #16
Browse files Browse the repository at this point in the history
  • Loading branch information
fpetras committed Apr 23, 2018
1 parent 5070975 commit 3145b0d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
10 changes: 8 additions & 2 deletions include/ft_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
# include "libft.h"
# include "op.h"

# define V_LVL_0 0
# define V_LVL_1 1
# define V_LVL_2 2
# define V_LVL_4 3
# define V_LVL_8 4
# define V_LVL_16 5

typedef struct s_vm
{
int dump;
int cycles;
int verbose;
int verbosity_lvl;
int v_lvl[6];
char *players[MAX_PLAYERS + 2];
} t_vm;

Expand Down
4 changes: 2 additions & 2 deletions src/ft_vm/ft_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ int main(int ac, char **av)
ft_bzero(&vm, sizeof(t_vm));
if (ac < 2 || vm_options(av, &vm) == -1)
{
ft_dprintf(2, "usage: %s [-dump nbr_cycles] [-n number] ", av[0]);
ft_dprintf(2, "champion1.cor ...\n");
ft_dprintf(2, "usage: %s [-dump nbr_cycles] ", av[0]);
ft_dprintf(2, "[-v N ...] [-n number] champion1.cor ...\n");
return (-1);
}
else if (vm_get_champions(av, &vm) > MAX_PLAYERS)
Expand Down
51 changes: 40 additions & 11 deletions src/ft_vm/vm_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,59 @@ static int vm_valid_verbosity_lvl(int v)
return (v == 0 || v == 1 || v == 2 || v == 4 || v == 8 || v == 16);
}

static int vm_options_3(int i, char **av, t_vm *vm)
static int vm_lvl_to_index(int lvl)
{
if (lvl == 0 || lvl == 1 || lvl == 2)
return (lvl);
else if (lvl == 4)
return (V_LVL_4);
else if (lvl == 8)
return (V_LVL_8);
else if (lvl == 16)
return (V_LVL_16);
else
return (0);
}

static int vm_option_v(int i, char **av, t_vm *vm)
{
int lvl;

if (!ft_strcmp(av[i], "-v"))
{
if (!vm_isnumber(av[i + 1]))
return (-1);
vm->verbose = 1;
vm->verbosity_lvl = ft_atoi(av[i + 1]);
if (!vm_valid_verbosity_lvl(vm->verbosity_lvl))
return (-1);
while (av[++i] && vm_isnumber(av[i]))
{
lvl = ft_atoi(av[i]);
if (!vm_valid_verbosity_lvl(lvl))
return (-1);
vm->v_lvl[vm_lvl_to_index(lvl)] =
(vm_valid_verbosity_lvl(lvl)) ? 1 : vm->v_lvl[lvl];
}
}
else if (!ft_strncmp(av[i], "-v", 2))
{
if (!vm_isnumber(&av[i][2]))
return (-1);
vm->verbose = 1;
vm->verbosity_lvl = ft_atoi(&av[i][2]);
if (!vm_valid_verbosity_lvl(vm->verbosity_lvl))
lvl = ft_atoi(&av[i][2]);
if (!vm_valid_verbosity_lvl(lvl))
return (-1);
vm->v_lvl[vm_lvl_to_index(lvl)] =
(vm_valid_verbosity_lvl(lvl)) ? 1 : vm->v_lvl[lvl];
while (av[++i] && vm_isnumber(av[i]))
{
lvl = ft_atoi(av[i]);
if (!vm_valid_verbosity_lvl(lvl))
return (-1);
vm->v_lvl[vm_lvl_to_index(lvl)] =
(vm_valid_verbosity_lvl(lvl)) ? 1 : vm->v_lvl[lvl];
}
}
return (0);
}

static int vm_options_2(int i, char **av, t_vm *vm)
static int vm_option_n(int i, char **av, t_vm *vm)
{
int num;

Expand Down Expand Up @@ -85,9 +114,9 @@ int vm_options(char **av, t_vm *vm)
if ((vm->cycles = ft_atoi(av[i + 1])) <= 0)
return (-1);
}
else if (vm_options_2(i, av, vm) == -1)
else if (vm_option_n(i, av, vm) == -1)
return (-1);
else if (vm_options_3(i, av, vm) == -1)
else if (vm_option_v(i, av, vm) == -1)
return (-1);
// if (ft_strncmp(av[i], "-n", 2) && ft_strcmp(av[i], "-dump") &&
// ft_strcmp(&av[i][ft_strlen(av[i]) - 4], ".cor") &&
Expand Down
14 changes: 12 additions & 2 deletions src/ft_vm/vm_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@
void vm_print(t_vm vm)
{
int i;
int j;

i = -1;
ft_printf("dump: %d cycles: %d\n", vm.dump, vm.cycles);
ft_printf("is verbose: %d verbosity level: %d\n", vm.verbose, vm.verbosity_lvl);
i = -1;
j = 0;
while (++i < 6)
{
j = (i == 3) ? 4 : j;
j = (i == 4) ? 8 : j;
j = (i == 5) ? 16 : j;
ft_printf("verbosity lvl %d: %d\n", j, vm.v_lvl[i]);
j++;
}
i = -1;
while (++i < MAX_PLAYERS)
ft_printf("player %d: %s\n", i + 1, vm.players[i]);
ft_printfln("This is Corewar!");
Expand Down

0 comments on commit 3145b0d

Please sign in to comment.