Code cleanup on the shot validation path
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 23 Aug 2017 19:28:50 +0000 (21:28 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 23 Aug 2017 19:28:50 +0000 (21:28 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
lib/othello.c
lib/othello.h
src/main.c

index 1bc32e2fe1969b082424ab00bfa8af89845b10c6..857625a1d08befd3868a17b9b9ef20914141445b 100644 (file)
@@ -218,8 +218,8 @@ static unsigned int reverse_one_direction(int y, int x, int direction, unsigned
     return nb_pawns_reversed;
 }
 
     return nb_pawns_reversed;
 }
 
-/* loop optimized version of valid_shot changing nothing to the pawns 2D array */
-static bool is_legal_shot(int y, int x, unsigned int current_player, unsigned int pawn_array[board_size][board_size]) {
+/* loop optimized version of valid_shot function changing nothing to the pawns 2D array */
+bool is_legal_shot(int y, int x, unsigned int current_player, unsigned int pawn_array[board_size][board_size]) {
     unsigned int nb_pawns_reversed = 0;
 
     if (!is_valid_coordinates(y, x) || !is_box_type(y, x, pawn_array, empty)) {
     unsigned int nb_pawns_reversed = 0;
 
     if (!is_valid_coordinates(y, x) || !is_box_type(y, x, pawn_array, empty)) {
index 7146617a7a4136d3e2684cc0b6ece662597beea4..9eb1c8cd88e3dc1be5b12952f25a314eb610d8e0 100644 (file)
@@ -45,6 +45,7 @@ bool is_board_full(unsigned int pawn_array[board_size][board_size]);
 
 unsigned int count_pawns_type(unsigned int pawn_array[board_size][board_size], unsigned int type);
 
 
 unsigned int count_pawns_type(unsigned int pawn_array[board_size][board_size], unsigned int type);
 
+bool is_legal_shot(int y, int x, unsigned int current_player, unsigned int pawn_array[board_size][board_size]);
 unsigned int valid_shot(int y, int x, unsigned int current_player, unsigned int pawn_array[board_size][board_size]);
 
 void build_playable_shots_list(unsigned int current_player, struct shots_list_s* shots_list, unsigned int pawn_array[board_size][board_size]);
 unsigned int valid_shot(int y, int x, unsigned int current_player, unsigned int pawn_array[board_size][board_size]);
 
 void build_playable_shots_list(unsigned int current_player, struct shots_list_s* shots_list, unsigned int pawn_array[board_size][board_size]);
index a86a7efd94f125123d6aca435bfb62fdde7599b8..3ed7991b2229685cdc40c975a1937b38f3709f32 100644 (file)
@@ -79,7 +79,6 @@ int main() {
 
         build_playable_shots_list(player, &playable_shots, pawns);
         print_shots_list(board_center_y, board_center_x, &playable_shots);
 
         build_playable_shots_list(player, &playable_shots, pawns);
         print_shots_list(board_center_y, board_center_x, &playable_shots);
-        free_shots_list(&playable_shots);
 
         display_array(1, 1, pawns);
 
 
         display_array(1, 1, pawns);
 
@@ -93,8 +92,9 @@ int main() {
             const char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):";
             int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, (center_x - strlen(prompt_msg)/2), prompt_msg, &y, &x_char);
             int x = map_col_letter_to_index(x_char);
             const char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):";
             int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, (center_x - strlen(prompt_msg)/2), prompt_msg, &y, &x_char);
             int x = map_col_letter_to_index(x_char);
-            nb_pawns_reversed = valid_shot(y, x, player, pawns);
-            if (nb_pawns_reversed > 0 && prmt_rt == 1) {
+            /* TODO: a comparaison to the linked list of playable shots is better */
+            if (is_legal_shot(y, x, player, pawns) && prmt_rt == 1) {
+                nb_pawns_reversed = valid_shot(y, x, player, pawns);
                 input_ok = true;
                 clear();
             } else {
                 input_ok = true;
                 clear();
             } else {
@@ -102,6 +102,8 @@ int main() {
             }
         } while (!input_ok);
 
             }
         } while (!input_ok);
 
+        free_shots_list(&playable_shots);
+
         round++; /* increment the round count */
 
         /* here are all the end of the game conditions */
         round++; /* increment the round count */
 
         /* here are all the end of the game conditions */