-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_util.c
146 lines (140 loc) · 3.8 KB
/
easy_util.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* easy_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abassibe <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/04 13:02:35 by abassibe #+# #+# */
/* Updated: 2017/03/04 15:16:06 by abassibe ### ########.fr */
/* */
/* ************************************************************************** */
#include "connect_four.h"
int easy_count_connect_v(t_easy *compar, int p, int j, int i)
{
compar->count2 = 0;
compar->count = 0;
compar->pos_i = 0;
compar->pos_j = 0;
while (++j < g_connect.input_h - 2)
{
if (g_connect.grid[i][j] == p)
compar->count2++;
if (g_connect.grid[i][j + 1] == p)
compar->count2++;
if (g_connect.grid[i][j + 2] == p)
compar->count2++;
if (compar->count < compar->count2)
{
compar->count = compar->count2;
compar->pos_j = j;
compar->pos_i = i;
}
if (compar->count2 == 3)
return (easy_counter_win_v(compar));
compar->count2 = 0;
}
return (0);
}
int easy_count_connect_h(t_easy *compar, int p, int j, int i)
{
compar->count2 = 0;
compar->count = 0;
compar->pos_i = 0;
compar->pos_j = 0;
while (++i < g_connect.input_w - 2)
{
if (g_connect.grid[i][j] == p)
compar->count2++;
if (g_connect.grid[i + 1][j] == p)
compar->count2++;
if (g_connect.grid[i + 2][j] == p)
compar->count2++;
if (compar->count < compar->count2)
{
compar->count = compar->count2;
compar->pos_i = i;
compar->pos_j = j;
}
if (compar->count2 == 3)
return (easy_counter_win_h(compar));
compar->count2 = 0;
}
return (0);
}
int easy_count_connect_diag_g(t_easy *compar, int p, int j, int i)
{
compar->count2 = 0;
compar->count = 0;
compar->pos_i = 0;
compar->pos_j = 0;
while (++j < g_connect.input_h - 2)
{
if (g_connect.grid[i][j] == p)
compar->count2++;
if (g_connect.grid[i + 1][j + 1] == p)
compar->count2++;
if (g_connect.grid[i + 2][j + 2] == p)
compar->count2++;
if (compar->count < compar->count2)
{
compar->count = compar->count2;
compar->pos_i = j;
compar->pos_j = i;
}
if (compar->count2 == 3)
return (easy_counter_win_g(compar));
compar->count2 = 0;
}
return (0);
}
int easy_count_connect_diag_d(t_easy *compar, int p, int j, int i)
{
compar->count2 = 0;
compar->count = 0;
compar->pos_i = 0;
compar->pos_j = 0;
while (++j < g_connect.input_h)
{
if (g_connect.grid[i][j] == p)
compar->count2++;
if (g_connect.grid[i + 1][j - 1] == p)
compar->count2++;
if (g_connect.grid[i + 2][j - 2] == p)
compar->count2++;
if (compar->count < compar->count2)
{
compar->count = compar->count2;
compar->pos_i = j;
compar->pos_j = i;
}
if (compar->count2 == 3)
return (easy_counter_win_d(compar));
compar->count2 = 0;
}
return (0);
}
t_easy easy_compar(t_easy *compar, int p)
{
J = -1;
I = -1;
while (++J < g_connect.input_h)
if ((P = easy_count_connect_h(compar, p, J, I)) == 1)
return (*compar);
I = -1;
J = -1;
while (++I < g_connect.input_w)
if ((P = easy_count_connect_v(compar, p, J, I)) == 1)
return (*compar);
I = -1;
J = -1;
while (++I < g_connect.input_w - 3)
if ((P = easy_count_connect_diag_g(compar, p, J, I)) == 1)
return (*compar);
I = -1;
J = 2;
while (++I < g_connect.input_w - 3)
if ((P = easy_count_connect_diag_d(compar, p, J, I)) == 1)
return (*compar);
return (*compar);
}