fix more signed integer issues.
[Project_algorithmic_C.git] / lib / othello.c
index 14c1a9b8c883305a943fbf0f7ee3048f09c0c7d7..6b049b2c68ed06d70ca5fd2c29481ebd77ade9d3 100644 (file)
 #include "othello.h"
 #include "debug.h"
 
+/**
+ * Get current round player integer
+ * @param  round_count current round integer
+ * @return             current round player
+ */
 unsigned int current_player(unsigned int round_count) {
 
     if (round_count % 2 != 0) {
@@ -32,7 +37,12 @@ unsigned int current_player(unsigned int round_count) {
     }
 }
 
-unsigned int current_opponent(unsigned int current_player) {
+/**
+ * Get current round opponent integer
+ * @param  current_player current round player
+ * @return                current round opponent integer
+ */
+static unsigned int current_opponent(unsigned int current_player) {
 
     if (current_player == player_one) {
         return player_two;
@@ -49,12 +59,19 @@ unsigned int current_opponent(unsigned int current_player) {
  * v
  * The origin O has (1, 1) coordinates */
 
-unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]) {
+/**
+ * Get pawn value at coordinates (y,x)
+ * @param  y          y coordinate
+ * @param  x          x coordinate
+ * @param  pawn_array array of played pawns
+ * @return            pawn integer type
+ */
+unsigned int get_box_value(unsigned int y, unsigned int x, unsigned int pawn_array[board_size][board_size]) {
 
     return pawn_array[y-1][x-1];
 }
 
-bool is_box_type(int y, int x, unsigned int pawn_array[board_size][board_size], unsigned int type) {
+bool is_box_type(unsigned int y, unsigned int x, unsigned int pawn_array[board_size][board_size], unsigned int type) {
 
     if (type > 2) {
         return NULL;
@@ -66,7 +83,7 @@ bool is_box_type(int y, int x, unsigned int pawn_array[board_size][board_size],
     }
 }
 
-static bool is_valid_coordinates(int y, int x) {
+static bool is_valid_coordinates(unsigned int y, unsigned int x) {
 
     if ((y > 0 && y < board_size + 1) && \
             (x > 0 && x < board_size + 1)) {
@@ -76,8 +93,14 @@ static bool is_valid_coordinates(int y, int x) {
     }
 }
 
-/* helper function to set a correct value at the (y, x) coordinates in the pawns array */
-void set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]) {
+/**
+ * Helper function to set a correct value at the (y,x) coordinates in the pawns array
+ * @param y      [description]
+ * @param x      [description]
+ * @param type   [description]
+ * @param pawn_array [description]
+ */
+void set_pawn(unsigned int y, unsigned int x, unsigned int type, unsigned int pawn_array[board_size][board_size]) {
 
     if (type > 0 && type < 3 && \
             is_valid_coordinates(y, x)) {
@@ -86,7 +109,7 @@ void set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_siz
 }
 
 /* reverse the pawn at (y, x) coordinates if it exists */
-static void reverse_pawn(int y, int x, unsigned int pawn_array[board_size][board_size]) {
+static void reverse_pawn(unsigned int y, unsigned int x, unsigned int pawn_array[board_size][board_size]) {
 
     if (is_box_type(y, x, pawn_array, black)) {
         set_pawn(y, x, white, pawn_array);
@@ -97,14 +120,17 @@ static void reverse_pawn(int y, int x, unsigned int pawn_array[board_size][board
 
 void zero_pawns(unsigned int pawn_array[board_size][board_size]) {
 
-    for (int i = 1; i <= board_size; i++) {
-        for (int j = 1; j <= board_size; j++) {
+    for (unsigned int i = 1; i <= board_size; i++) {
+        for (unsigned int j = 1; j <= board_size; j++) {
             set_pawn(i, j, empty, pawn_array);
          }
     }
 }
 
-/* set the pawns in the start position */
+/**
+ * Set the pawns in the start position
+ * @param pawn_array array of played pawns
+ */
 void init_pawns(unsigned int pawn_array[board_size][board_size]) {
 
     /* the 2D array zeroing is not necessary if it is properly initialized to zero */
@@ -121,8 +147,8 @@ unsigned int count_pawns_type(unsigned int pawn_array[board_size][board_size], u
     if (type > 2) {
         return 0;
     }
-    for (int i = 1; i <= board_size; i++) {
-        for (int j = 1; j <= board_size; j++) {
+    for (unsigned int i = 1; i <= board_size; i++) {
+        for (unsigned int j = 1; j <= board_size; j++) {
             if (is_box_type(i, j, pawn_array, type)) {
                 count++;
             }
@@ -131,7 +157,7 @@ unsigned int count_pawns_type(unsigned int pawn_array[board_size][board_size], u
     return count;
 }
 
-static void direction_to_coordinates(unsigned int direction, int* start_y, int* start_x) {
+static void direction_to_coordinates(unsigned int direction, unsigned int* start_y, unsigned int* start_x) {
 
     if (direction == north) {
         *start_y = *start_y - 1;
@@ -159,8 +185,8 @@ static void direction_to_coordinates(unsigned int direction, int* start_y, int*
 bool is_board_full(unsigned int pawn_array[board_size][board_size]) {
 
     /* an alternate method is to test the round count vs. 60 */
-    for (int i = 1; i <= board_size; i++) {
-        for (int j = 1; j <= board_size; j++) {
+    for (unsigned int i = 1; i <= board_size; i++) {
+        for (unsigned int j = 1; j <= board_size; j++) {
             if (is_box_type(i, j, pawn_array , empty)) {
                 return false;
             }
@@ -180,9 +206,9 @@ unsigned int eval_winner(unsigned int nb_white, unsigned int nb_black) {
     }
 }
 
-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]) {
+static unsigned int count_pawn_to_reverse_one_direction(unsigned int y, unsigned int x, unsigned int direction, unsigned int current_player, unsigned int pawn_array[board_size][board_size]) {
     unsigned int nb_pawns_reversed = 0;
-    int moving_y = y, moving_x = x;
+    unsigned int moving_y = y, moving_x = x;
 
     /* count the pawns to reverse in the chosen direction */
     direction_to_coordinates(direction, &moving_y, &moving_x);
@@ -200,9 +226,9 @@ static unsigned int count_pawn_to_reverse_one_direction(int y, int x, int direct
 }
 
 /* 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) {
+static unsigned int reverse_one_direction(unsigned int y, unsigned int x, unsigned 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;
+    unsigned 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);
 
@@ -219,7 +245,7 @@ static unsigned int reverse_one_direction(int y, int x, int direction, unsigned
 }
 
 /* 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]) {
+bool is_legal_shot(unsigned int y, unsigned 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)) {
@@ -236,7 +262,7 @@ bool is_legal_shot(int y, int x, unsigned int current_player, unsigned int pawn_
 }
 
 /* play the shot if legal and flip or reverse the necessary pawns */
-unsigned int valid_shot(int y, int x, unsigned int current_player, unsigned int pawn_array[board_size][board_size]) {
+unsigned int valid_shot(unsigned int y, unsigned 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)) {
@@ -255,7 +281,7 @@ unsigned int valid_shot(int y, int x, unsigned int current_player, unsigned int
     return nb_pawns_reversed;
 }
 
-static void add_shots_list_cell(int y, int x, unsigned int type, struct shots_list_s* shots_list) {
+static void add_shots_list_cell(unsigned int y, unsigned int x, unsigned int type, struct shots_list_s* shots_list) {
     struct shots_list_s* list_cell = (struct shots_list_s*)malloc(sizeof(struct shots_list_s));
     if (!list_cell) {
         exit(EXIT_FAILURE);
@@ -277,7 +303,6 @@ void free_shots_list(struct shots_list_s* shots_list) {
         list_del(&(list_counter->list));
         free(list_counter);
     }
-
 }
 
 void build_playable_shots_list(unsigned int current_player, struct shots_list_s* shots_list, unsigned int pawn_array[board_size][board_size]) {