-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscores.c
179 lines (167 loc) · 4.61 KB
/
scores.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
int connect4Horizontally(configurations config, char board[][config.width], int move, int *score, char symbol, int row)
{
int counter1 = 0;
int counter2 = 0;
int changes = 0;
for (int i = 1; i < 4 && move + i <= config.width - 1; i++)
{
if (board[row][move + i] == symbol)
{
counter1++; // check rightside of the piece
}
else
{
break;
}
}
for (int i = move - 1; i > move - 4 && i >= 0; i--)
{
if (board[row][i] == symbol)
{
counter2++; // check leftside of the piece
}
else
{
break;
}
}
if (counter1 + counter2 >= 3)
{
*score += (counter1 + counter2 - 2);
changes += (counter1 + counter2 - 2);
}
return changes; // changes in score
}
int connect4Vertically(configurations config, char board[][config.width], int move, int *score, char symbol, int row)
{
int changes = 0;
int counter = 0;
for (int i = row + 1; i != config.height; i++)
{
if (board[i][move] == symbol)
{
counter++;
}
}
if (counter >= 3)
{
*score += 1;
changes++;
}
return changes;
}
int connect4RightDiagonal(configurations config, char board[][config.width], int move, int *score, char symbol, int row)
{
int counter1 = 0;
int counter2 = 0;
int changes = 0;
int j = 1;
for (int i = 1; i < 4 && move + i <= config.width - 1 && row - j != -1; i++)
{
if (board[row - j][move + i] == symbol)
{
counter1++; // check rightside of the piece
}
else
{
break;
}
j++;
}
j = 1;
for (int i = move - 1; i > move - 4 && i >= 0 && row + j != config.height; i--)
{
if (board[row + j][i] == symbol)
{
counter2++; // check leftside of the piece
}
else
{
break;
}
j++;
}
if (counter1 + counter2 >= 3)
{
*score += (counter1 + counter2 - 2);
changes += (counter1 + counter2 - 2);
}
return changes; // changes in score
}
int connect4LeftDiagonal(configurations config, char board[][config.width], int move, int *score, char symbol, int row)
{
int counter1 = 0;
int counter2 = 0;
int changes = 0;
int j = 1;
for (int i = 1; i < 4 && move + i <= config.width - 1 && row + j != config.height; i++)
{
if (board[row + j][move + i] == symbol)
{
counter1++; // check rightside of the piece
}
else
{
break;
}
j++;
}
j = 1;
for (int i = move - 1; i > move - 4 && i >= 0 && row - j != -1; i--)
{
if (board[row - j][i] == symbol)
{
counter2++; // check leftside of the piece
}
else
{
break;
}
j++;
}
if (counter1 + counter2 >= 3)
{
*score += (counter1 + counter2 - 2);
changes += (counter1 + counter2 - 2);
}
return changes; // changes in score
}
void isConnect4(configurations config, char board[][config.width], int move, int *score, int moves_stack[], int moves_count, char symbol, int to_undo, int mode)
{
int repeated_moves = 0;
// To get row's index
if (mode == 2 && symbol == 'X' && (to_undo == 1 || to_undo == 3))
{
for (int i = 0; i < moves_count - 2; i++)
{
if (moves_stack[i] == moves_stack[moves_count - 2])
{
repeated_moves++;
}
}
}
else
{
for (int i = 0; i < moves_count - 1; i++)
{
if (moves_stack[i] == moves_stack[moves_count - 1])
{
repeated_moves++;
}
}
}
repeated_moves = config.height - repeated_moves - 1; // row index
int changes = 0;
changes += connect4Horizontally(config, board, move, score, symbol, repeated_moves);
// checks if the move caused 4 to be connected horizontally
changes += connect4Vertically(config, board, move, score, symbol, repeated_moves);
// checks if the move caused 4 to be connected vertically
changes += connect4RightDiagonal(config, board, move, score, symbol, repeated_moves);
// checks if the move caused 4 to be connected in this direction "/"
changes += connect4LeftDiagonal(config, board, move, score, symbol, repeated_moves);
// checks if the move caused 4 to be connected in this direction "\"
if (changes != 0 && to_undo == 1)
{
*score = *score - (2 * changes);
}
}