From: Jérôme Benoit Date: Sun, 7 Oct 2018 17:36:48 +0000 (+0200) Subject: fix more signed integer issues. X-Git-Url: https://git.piment-noir.org/?p=Project_algorithmic_C.git;a=commitdiff_plain;h=3de18b0799930ec82892a0ce65895716e1e8acbc fix more signed integer issues. Signed-off-by: Jérôme Benoit --- diff --git a/lib/othello.c b/lib/othello.c index 72066a0..6b049b2 100644 --- a/lib/othello.c +++ b/lib/othello.c @@ -66,12 +66,12 @@ static unsigned int current_opponent(unsigned int current_player) { * @param pawn_array array of played pawns * @return pawn integer type */ -unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]) { +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; @@ -83,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)) { @@ -100,7 +100,7 @@ static bool is_valid_coordinates(int y, int x) { * @param type [description] * @param pawn_array [description] */ -void set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]) { +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)) { @@ -109,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); @@ -120,8 +120,8 @@ 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); } } @@ -147,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++; } @@ -157,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; @@ -185,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; } @@ -206,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, unsigned 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); @@ -226,9 +226,9 @@ static unsigned int count_pawn_to_reverse_one_direction(int y, int x, unsigned i } /* revert the pawns if needed in one direction */ -static unsigned int reverse_one_direction(int y, int x, unsigned 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); @@ -245,7 +245,7 @@ static unsigned int reverse_one_direction(int y, int x, unsigned int direction, } /* 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)) { @@ -262,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)) { diff --git a/lib/othello.h b/lib/othello.h index 9eb1c8c..02a7117 100644 --- a/lib/othello.h +++ b/lib/othello.h @@ -27,8 +27,8 @@ /* linked list of can play shots */ struct shots_list_s { struct list_head list; - int y; - int x; + unsigned int y; + unsigned int x; unsigned int type; /* can be white or black or hint allowed or hint_fordidden */ }; @@ -37,16 +37,16 @@ unsigned int eval_winner(unsigned int nb_white, unsigned int nb_black); void zero_pawns(unsigned int pawn_array[board_size][board_size]); void init_pawns(unsigned int pawn_array[board_size][board_size]); -unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]); -void set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]); +unsigned int get_box_value(unsigned int y, unsigned int x, unsigned int pawn_array[board_size][board_size]); +void set_pawn(unsigned int y, unsigned int x, unsigned int type, unsigned int pawn_array[board_size][board_size]); -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); 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); -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]); +bool is_legal_shot(unsigned int y, unsigned 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]); void build_playable_shots_list(unsigned int current_player, struct shots_list_s* shots_list, unsigned int pawn_array[board_size][board_size]); void free_shots_list(struct shots_list_s* shots_list); diff --git a/lib/ui.c b/lib/ui.c index 02eecce..da79cb3 100644 --- a/lib/ui.c +++ b/lib/ui.c @@ -120,8 +120,8 @@ static int remap_x(int x) { void print_pawns(int base_y, int base_x, 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++) { if (!is_box_type(i, j, pawn_array, empty)) { print_o(base_y + remap_y(i), base_x + remap_x(j), get_box_value(i, j, pawn_array)); }