1bc32e2fe1969b082424ab00bfa8af89845b10c6
2 * =====================================================================================
6 * Description: Handle the othello board content
9 * Created: 25/04/2017 15:16:08
13 * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org
14 * Organization: Piment Noir
16 * =====================================================================================
26 unsigned int current_player(unsigned int round_count
) {
28 if (round_count
% 2 != 0) {
35 unsigned int current_opponent(unsigned int current_player
) {
37 if (current_player
== player_one
) {
44 /* for consistency with ncurses, the board coordinates are in the following order:
50 * The origin O has (1, 1) coordinates */
52 unsigned int get_box_value(int y
, int x
, unsigned int pawn_array
[board_size
][board_size
]) {
54 return pawn_array
[y
-1][x
-1];
57 bool is_box_type(int y
, int x
, unsigned int pawn_array
[board_size
][board_size
], unsigned int type
) {
62 if (get_box_value(y
, x
, pawn_array
) == type
) {
69 static bool is_valid_coordinates(int y
, int x
) {
71 if ((y
> 0 && y
< board_size
+ 1) && \
72 (x
> 0 && x
< board_size
+ 1)) {
79 /* helper function to set a correct value at the (y, x) coordinates in the pawns array */
80 void set_pawn(int y
, int x
, unsigned int type
, unsigned int pawn_array
[board_size
][board_size
]) {
82 if (type
> 0 && type
< 3 && \
83 is_valid_coordinates(y
, x
)) {
84 pawn_array
[y
-1][x
-1] = type
;
85 } //FIXME: else case should set invalid values to permit to catch errors
88 /* reverse the pawn at (y, x) coordinates if it exists */
89 static void reverse_pawn(int y
, int x
, unsigned int pawn_array
[board_size
][board_size
]) {
91 if (is_box_type(y
, x
, pawn_array
, black
)) {
92 set_pawn(y
, x
, white
, pawn_array
);
93 } else if (is_box_type(y
, x
, pawn_array
, white
)) {
94 set_pawn(y
, x
, black
, pawn_array
);
98 void zero_pawns(unsigned int pawn_array
[board_size
][board_size
]) {
100 for (int i
= 1; i
<= board_size
; i
++) {
101 for (int j
= 1; j
<= board_size
; j
++) {
102 set_pawn(i
, j
, empty
, pawn_array
);
107 /* set the pawns in the start position */
108 void init_pawns(unsigned int pawn_array
[board_size
][board_size
]) {
110 /* the 2D array zeroing is not necessary if it is properly initialized to zero */
111 zero_pawns(pawn_array
);
112 set_pawn(5, 4, black
, pawn_array
);
113 set_pawn(4, 5, black
, pawn_array
);
114 set_pawn(4, 4, white
, pawn_array
);
115 set_pawn(5, 5, white
, pawn_array
);
118 unsigned int count_pawns_type(unsigned int pawn_array
[board_size
][board_size
], unsigned int type
) {
119 unsigned int count
= 0;
124 for (int i
= 1; i
<= board_size
; i
++) {
125 for (int j
= 1; j
<= board_size
; j
++) {
126 if (is_box_type(i
, j
, pawn_array
, type
)) {
134 static void direction_to_coordinates(unsigned int direction
, int* start_y
, int* start_x
) {
136 if (direction
== north
) {
137 *start_y
= *start_y
- 1;
138 } else if (direction
== north_east
) {
139 *start_y
= *start_y
- 1;
140 *start_x
= *start_x
+ 1;
141 } else if (direction
== east
) {
142 *start_x
= *start_x
+ 1;
143 } else if (direction
== south_east
) {
144 *start_y
= *start_y
+ 1;
145 *start_x
= *start_x
+ 1;
146 } else if (direction
== south
) {
147 *start_y
= *start_y
+ 1;
148 } else if (direction
== south_west
) {
149 *start_y
= *start_y
+ 1;
150 *start_x
= *start_x
- 1;
151 } else if (direction
== west
) {
152 *start_x
= *start_x
- 1;
153 } else if (direction
== north_west
) {
154 *start_y
= *start_y
- 1;
155 *start_x
= *start_x
- 1;
159 bool is_board_full(unsigned int pawn_array
[board_size
][board_size
]) {
161 /* an alternate method is to test the round count vs. 60 */
162 for (int i
= 1; i
<= board_size
; i
++) {
163 for (int j
= 1; j
<= board_size
; j
++) {
164 if (is_box_type(i
, j
, pawn_array
, empty
)) {
172 unsigned int eval_winner(unsigned int nb_white
, unsigned int nb_black
) {
174 if (nb_white
> nb_black
) {
176 } else if (nb_white
< nb_black
) {
183 static unsigned int count_pawn_to_reverse_one_direction(int y
, int x
, int direction
, unsigned int current_player
, unsigned int pawn_array
[board_size
][board_size
]) {
184 unsigned int nb_pawns_reversed
= 0;
185 int moving_y
= y
, moving_x
= x
;
187 /* count the pawns to reverse in the chosen direction */
188 direction_to_coordinates(direction
, &moving_y
, &moving_x
);
190 if (!is_valid_coordinates(moving_y
, moving_x
) || is_box_type(moving_y
, moving_x
, pawn_array
, empty
)) {
193 if (is_box_type(moving_y
, moving_x
, pawn_array
, current_player
)) {
197 direction_to_coordinates(direction
, &moving_y
, &moving_x
);
199 return nb_pawns_reversed
;
202 /* revert the pawns if needed in one direction */
203 static unsigned int reverse_one_direction(int y
, int x
, int direction
, unsigned int current_player
, unsigned int pawn_array
[board_size
][board_size
], bool dry_run
) {
204 unsigned int nb_pawns_reversed
= 0;
205 int moving_y
= y
, moving_x
= x
;
207 nb_pawns_reversed
= count_pawn_to_reverse_one_direction(moving_y
, moving_x
, direction
, current_player
, pawn_array
);
209 /* now reverse the needed pawns */
210 if (nb_pawns_reversed
> 0 && !dry_run
) {
211 moving_y
= y
, moving_x
= x
;
212 direction_to_coordinates(direction
, &moving_y
, &moving_x
);
213 while (!is_box_type(moving_y
, moving_x
, pawn_array
, current_player
)) {
214 reverse_pawn(moving_y
, moving_x
, pawn_array
);
215 direction_to_coordinates(direction
, &moving_y
, &moving_x
);
218 return nb_pawns_reversed
;
221 /* loop optimized version of valid_shot changing nothing to the pawns 2D array */
222 static bool is_legal_shot(int y
, int x
, unsigned int current_player
, unsigned int pawn_array
[board_size
][board_size
]) {
223 unsigned int nb_pawns_reversed
= 0;
225 if (!is_valid_coordinates(y
, x
) || !is_box_type(y
, x
, pawn_array
, empty
)) {
229 for (unsigned int direction
= north
; direction
<= north_west
; direction
++) {
230 nb_pawns_reversed
+= reverse_one_direction(y
, x
, direction
, current_player
, pawn_array
, true);
231 if (nb_pawns_reversed
> 0) {
238 /* play the shot if legal and flip or reverse the necessary pawns */
239 unsigned int valid_shot(int y
, int x
, unsigned int current_player
, unsigned int pawn_array
[board_size
][board_size
]) {
240 unsigned int nb_pawns_reversed
= 0;
242 if (!is_valid_coordinates(y
, x
) || !is_box_type(y
, x
, pawn_array
, empty
)) {
246 for (unsigned int direction
= north
; direction
<= north_west
; direction
++) {
247 nb_pawns_reversed
+= reverse_one_direction(y
, x
, direction
, current_player
, pawn_array
, false);
250 if (nb_pawns_reversed
== 0) {
251 return nb_pawns_reversed
;
254 set_pawn(y
, x
, current_player
, pawn_array
);
255 return nb_pawns_reversed
;
258 static void add_shots_list_cell(int y
, int x
, unsigned int type
, struct shots_list_s
* shots_list
) {
259 struct shots_list_s
* list_cell
= malloc(sizeof(struct shots_list_s
));
264 if (type
> 0 && type
< 5 && is_valid_coordinates(y
, x
)) {
267 list_cell
->type
= type
;
268 list_add_tail(&(list_cell
->list
), &(shots_list
->list
));
272 void free_shots_list(struct shots_list_s
* shots_list
) {
273 struct shots_list_s
* list_counter
;
275 while (!list_empty(&shots_list
->list
)) {
276 list_counter
= list_entry(shots_list
->list
.next
, struct shots_list_s
, list
);
277 list_del(&list_counter
->list
);
283 void build_playable_shots_list(unsigned int current_player
, struct shots_list_s
* shots_list
, unsigned int pawn_array
[board_size
][board_size
]) {
285 for (unsigned int i
= 0; i
<= board_size
; i
++) {
286 for (unsigned int j
= 0; j
<= board_size
; j
++) {
287 if (is_legal_shot(i
, j
, current_player
, pawn_array
)) {
288 add_shots_list_cell(i
, j
, hint_allowed
, shots_list
);
289 /* FIXME: a neighbourhood detection is needed
290 } else if (is_box_type(i, j, pawn_array, empty)){
291 add_shots_list_cell(i, j, hint_forbidden, shots_list);