From 21b9239c8a14c5f9b5553d1f6a583307971b26e6 Mon Sep 17 00:00:00 2001 From: Jerome Benoit Date: Tue, 6 Jun 2017 22:09:19 +0200 Subject: [PATCH] White space cleanup and some constification Signed-off-by: Jerome Benoit --- Makefile | 3 +++ lib/othello.c | 16 ++++++++-------- src/main.c | 26 +++++++++++++------------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 78a3345..2a3ae22 100644 --- a/Makefile +++ b/Makefile @@ -191,6 +191,9 @@ else -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX) endif +run: $(BINARY_NAME) + ./$(BINARY_NAME) + disassemble: $(BINARY_NAME)$(BINARY_SUFFIX) objdump -d $< | less diff --git a/lib/othello.c b/lib/othello.c index 4f1b83e..4e03069 100644 --- a/lib/othello.c +++ b/lib/othello.c @@ -24,7 +24,7 @@ #include "debug.h" unsigned int current_player(unsigned int round_count) { - + if (round_count % 2 != 0) { return player_two; } else { @@ -46,11 +46,11 @@ unsigned int current_opponent(unsigned int current_player) { * | * y * | - * v + * 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]) { - + return pawn_array[y-1][x-1]; } @@ -87,7 +87,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]) { - + if (is_box_type(y, x, pawn_array, black)) { set_pawn(y, x, white, pawn_array); } else if (is_box_type(y, x, pawn_array, white)) { @@ -199,7 +199,7 @@ static unsigned int count_pawn_to_reverse_one_direction(int y, int x, int direct return nb_pawns_reversed; } -/* revert the pawns if needed in one direction */ +/* 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; @@ -221,11 +221,11 @@ static unsigned int reverse_one_direction(int y, int x, int direction, unsigned /* 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]) { unsigned int nb_pawns_reversed = 0; - + if (!is_valid_coordinates(y, x) || !is_box_type(y, x, pawn_array, empty)) { return false; } - + for (unsigned int direction = north; direction <= north_west; direction++) { nb_pawns_reversed += reverse_one_direction(y, x, direction, current_player, pawn_array, true); if (nb_pawns_reversed > 0) { @@ -245,7 +245,7 @@ unsigned int valid_shot(int y, int x, unsigned int current_player, unsigned int for (unsigned int direction = north; direction <= north_west; direction++) { nb_pawns_reversed += reverse_one_direction(y, x, direction, current_player, pawn_array, false); } - + if (nb_pawns_reversed == 0) { return 0; } diff --git a/src/main.c b/src/main.c index b0bf5b9..224e35c 100644 --- a/src/main.c +++ b/src/main.c @@ -15,14 +15,14 @@ int main() { bool exit_condition = false; unsigned int nb_white = 0, nb_black = 0; - char* title_msg = "Jeu Othello"; - char* score_msg = "Pions %s: %d"; - char* invalid_move_msg = "Coup invalide"; - char* player_msg = "Joueur %d (%s) joue !"; - char* winner_msg = "Joueur %d (%s) gagne !"; - char* draw_msg = "Egalite !"; - char* exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; - + const char* title_msg = "Jeu Othello"; + const char* score_msg = "Pions %s: %d"; + const char* invalid_move_msg = "Coup invalide"; + const char* player_msg = "Joueur %d (%s) joue !"; + const char* winner_msg = "Joueur %d (%s) gagne !"; + const char* draw_msg = "Egalite !"; + const char* exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; + /* linked list of playable shots */ struct shots_list_s playable_shots; INIT_LIST_HEAD(&playable_shots.list); @@ -73,10 +73,10 @@ int main() { nb_white = count_pawns_type(pawns, white); nb_black = count_pawns_type(pawns, black); - + mvprintw(center_y, center_x - 42/2 - snprintf(NULL, 0, score_msg, "noirs", nb_black) - 2, score_msg, "noirs", nb_black); mvprintw(center_y, center_x + 42/2 + 2, score_msg, "blancs", nb_white); - + //display_array(1, 1, pawns); int y; @@ -86,7 +86,7 @@ int main() { do { y = 0; x_char = ""; - char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):"; + 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); @@ -129,11 +129,11 @@ int main() { clear(); } } - + refresh(); } while (!exit_condition); - + endwin(); exit(EXIT_SUCCESS); -- 2.34.1