forked from ydeinega/corwar_fin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_champ.c
123 lines (112 loc) · 2.55 KB
/
validate_champ.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* validate_champ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ydeineha <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/02 16:30:41 by ydeineha #+# #+# */
/* Updated: 2018/07/02 16:30:43 by ydeineha ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void validate_champ(int argc, char **argv, int *i)
{
int num;
t_lst_champs *new;
num = 0;
new = NULL;
if (ft_strequ(argv[*i], "-n"))
num = validate_champ_num(argc, argv, i);
if (ft_strstr(argv[*i], ".cor"))
{
new = num > 0 ? new_champ(argv[*i], num, true) :
new_champ(argv[*i], num, false);
add_champ(&(g_game.champ), new);
g_game.players += 1;
}
else
error(4);
}
int validate_champ_num(int argc, char **argv, int *i)
{
int num;
num = 0;
if (*i + 1 == argc)
error(2);
else if (*i + 2 >= argc)
{
if (validate_num(argv[*i + 1]))
error(4);
else
error(2);
}
else
{
*i += 2;
if (validate_num(argv[*i - 1]))
num = ft_atoi(argv[*i - 1]);
else
error(2);
}
!num || num > MAX_PLAYERS ? error(3) : 0;
g_game.champ ? check_double_positions(num) : 0;
return (num);
}
void check_positions(void)
{
int i;
t_lst_champs *tmp;
i = 1;
tmp = NULL;
sort_champs();
tmp = g_game.champ;
if (tmp->next == NULL && tmp->num != 1)
error(7);
while (tmp)
{
if (tmp->num != i)
error(7);
tmp = tmp->next;
i++;
}
}
void sort_champs(void)
{
t_lst_champs *tmp;
t_lst_champs *prev;
int check;
tmp = g_game.champ;
prev = NULL;
check = -1;
while (check != 0)
{
check = 0;
while (tmp->next)
{
if (tmp->num > tmp->next->num)
{
tmp = swap_champs(tmp, prev);
check++;
}
prev = tmp;
tmp = tmp->next;
}
tmp = g_game.champ;
prev = NULL;
}
}
t_lst_champs *swap_champs(t_lst_champs *tmp, t_lst_champs *prev)
{
t_lst_champs *swap;
swap = NULL;
swap = tmp->next;
tmp->next = swap->next;
swap->next = tmp;
tmp = swap;
if (prev)
prev->next = swap;
else
g_game.champ = swap;
return (tmp);
}