- if (direction == north) {
- *start_y = *start_y - 1;
- } else if (direction == north_east) {
- *start_y = *start_y - 1;
- *start_x = *start_x + 1;
- } else if (direction == east) {
- *start_x = *start_x + 1;
- } else if (direction == south_east) {
- *start_y = *start_y + 1;
- *start_x = *start_x + 1;
- } else if (direction == south) {
- *start_y = *start_y + 1;
- } else if (direction == south_west) {
- *start_y = *start_y + 1;
- *start_x = *start_x - 1;
- } else if (direction == west) {
- *start_x = *start_x - 1;
- } else if (direction == north_west) {
- *start_y = *start_y - 1;
- *start_x = *start_x - 1;
+ /* count the pawns to reverse in the chosen direction */
+ direction_to_coordinates(direction, &moving_y, &moving_x);
+ while (true) {
+ if (!is_valid_coordinates(moving_y, moving_x) || is_box_type(moving_y, moving_x, pawn_array, empty)) {
+ return 0;
+ }
+ if (is_box_type(moving_y, moving_x, pawn_array, current_player)) {
+ break;
+ }
+ nb_pawns_reversed++;
+ direction_to_coordinates(direction, &moving_y, &moving_x);
+ }
+ return nb_pawns_reversed;
+}
+
+/* revert the pawns if needed in one direction */
+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) {
+ unsigned int nb_pawns_reversed = 0;
+ int moving_y = y, moving_x = x;
+
+ nb_pawns_reversed = count_pawn_to_reverse_one_direction(moving_y, moving_x, direction, current_player, pawn_array);
+
+ /* now reverse the needed pawns */
+ if (nb_pawns_reversed > 0 && !dry_run) {
+ moving_y = y, moving_x = x;
+ direction_to_coordinates(direction, &moving_y, &moving_x);
+ while (!is_box_type(moving_y, moving_x, pawn_array, current_player)) {
+ reverse_pawn(moving_y, moving_x, pawn_array);
+ direction_to_coordinates(direction, &moving_y, &moving_x);
+ }