diff --git a/puissance4.c b/puissance4.c index 9f100df8d814161087f7d18539bc571ba7256613..78e3ce1e7fac8db72c2babda8f47201c116cd512 100644 --- a/puissance4.c +++ b/puissance4.c @@ -1,20 +1,38 @@ #include "puissance4.h" +void print_usage(const char* program_name) { + printf("Usage: %s <mode> <row> <col>\n", program_name); + printf("%*smode specifies the mode: 1 = single player game (random),\n", 5, ""); + printf("%*s 2 = single player game (AI), 3 = two players game\n", 5, ""); + printf("%*srow specifies the number of rows (>= %d)\n", 5, "", MIN_ROW); + printf("%*scol specifies the number of columns (>= %d)\n", 5, "", MIN_COLUMN); +} + +bool validate_arguments(char const* argv[], int* game_mode, int* row_count, int* col_count) { + *game_mode = atoi(argv[1]); + *row_count = atoi(argv[2]); + *col_count = atoi(argv[3]); + + if (!is_valid_game_mode(*game_mode) || !is_valid_board_size(*row_count, *col_count)) { + print_usage(argv[0]); + return false; + } + + return true; +} + int main(int argc, char const* argv[]) { - if (argc != 4 || !is_valid_game_mode(atoi(argv[1])) || !is_valid_board_size(atoi(argv[2]), atoi(argv[3]))) { - printf("Usage: %s <mode> <row> <col>\n", argv[0]); - printf("%*smode specifies the mode: 1 = single player game (random),\n", 5, ""); - printf("%*s 2 = single player game (AI), 3 = two players game\n", 5, ""); - printf("%*srow specifies the number of rows (>= 4)\n", 5, ""); - printf("%*scol specifies the number of columns (>= 4)\n", 5, ""); + if (argc != 4) { + print_usage(argv[0]); + return false; + } + + int game_mode, row_count, col_count; + if (!validate_arguments(argv, &game_mode, &row_count, &col_count)) { return EXIT_FAILURE; } - enum GAME_MODE game_mode = atoi(argv[1]); - int row_count = atoi(argv[2]); - int col_count = atoi(argv[3]); srand(AI_SEED); - int** board = create_board(row_count, col_count); if (board == NULL) { fprintf(stderr, "Error: Unable to allocate memory for the board.\n"); @@ -60,11 +78,10 @@ int main(int argc, char const* argv[]) { } void announce_winner(enum GAME_MODE game_mode, int current_turn) { - const char* winner = (current_turn % 2 == 0) ? "Player one" : "Computer"; + char* winner = (current_turn % 2 == 0) ? "Player one" : "Computer"; if (game_mode == PLAYER_VS_PLAYER) { - winner = (current_turn % 2 == 0) ? "one" : "two"; - printf("\nPlayer %s won!\n", winner); + printf("\nPlayer %s won!\n", (current_turn % 2 == 0) ? "one" : "two"); } else { printf("\n%s won!\n", winner); } @@ -77,8 +94,3 @@ bool is_valid_game_mode(int game_mode) { bool is_valid_board_size(int rows, int cols) { return rows >= MIN_ROW && cols >= MIN_COLUMN; } - -void flush_input() { - int c; - while ((c = getchar()) != '\n' && c != EOF); -} diff --git a/puissance4.h b/puissance4.h index 7ebd43755a5b51549332615c6af887d8ba1e5548..4d13fa957b3180bfdd9c112b667a69f5db6dbe93 100644 --- a/puissance4.h +++ b/puissance4.h @@ -13,12 +13,9 @@ #define MIN_COLUMN 4 #define MIN_ROW 4 +bool validate_arguments(char const* argv[], int* game_mode, int* row_count, int* col_count); void announce_winner(enum GAME_MODE game_mode, int current_turn); bool is_valid_game_mode(int game_mode); bool is_valid_board_size(int rows, int cols); -// int select_random_column(int number_of_columns, int number_of_rows, int** board); -// int select_smart_column(int number_of_columns, int number_of_rows, int** board); -// bool is_column_full(int col, int rows, int** board); -void flush_input(); #endif diff --git a/test_puissance4.c b/test_puissance4.c new file mode 100644 index 0000000000000000000000000000000000000000..f63ff0d35fc96b59d6337277ac1545b4fb711c68 --- /dev/null +++ b/test_puissance4.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include "puissance4.h" + +#define GREEN "\033[1;32m" +#define RED "\033[1;31m" +#define RESET "\033[0m" + +void test_is_valid_game_mode(); +void test_is_valid_board_size(); + +int main() { + test_is_valid_game_mode(); + test_is_valid_board_size(); + return EXIT_SUCCESS; +} + +void test_is_valid_game_mode() { + assert(is_valid_game_mode(1) == true); + assert(is_valid_game_mode(2) == true); + assert(is_valid_game_mode(3) == true); + assert(is_valid_game_mode(0) == false); + assert(is_valid_game_mode(4) == false); + assert(is_valid_game_mode(-1) == false); + printf(GREEN "test_is_valid_game_mode: PASSED\n" RESET); +} + +void test_is_valid_board_size() { + assert(is_valid_board_size(4, 4) == true); + assert(is_valid_board_size(6, 7) == true); + assert(is_valid_board_size(3, 4) == false); + assert(is_valid_board_size(4, 3) == false); + assert(is_valid_board_size(2, 2) == false); + printf(GREEN "test_is_valid_board_size: PASSED\n" RESET); +} \ No newline at end of file diff --git a/tests_board b/tests_board deleted file mode 100755 index 4963c645ce1583ee18bdcea9857c70d6a42b9165..0000000000000000000000000000000000000000 Binary files a/tests_board and /dev/null differ diff --git a/tests_computer b/tests_computer deleted file mode 100755 index a1c01814781908237943a909f10aeff832497a61..0000000000000000000000000000000000000000 Binary files a/tests_computer and /dev/null differ diff --git a/tests_game b/tests_game deleted file mode 100755 index f1cfa60e87bb4232589ffc7d78236e1434260775..0000000000000000000000000000000000000000 Binary files a/tests_game and /dev/null differ