diff --git a/main.c b/main.c index ae79ccc08d937e50875d935d51d077c3aa8601ee..85f11cec0f5d77a91f21db5299b946e4e4e2e6bb 100644 --- a/main.c +++ b/main.c @@ -12,6 +12,25 @@ int main() { game_init(&game, RAND_AI, DEFAULT_ROW, DEFAULT_COL); print_game(game); + print_game(game); + manual_play(&game, 0); + print_game(game); + manual_play(&game, 4); + print_game(game); + manual_play(&game, 0); + print_game(game); + manual_play(&game, -1); + print_game(game); + manual_play(&game, 9); + print_game(game); + manual_play(&game, 0); + manual_play(&game, 0); + manual_play(&game, 0); + manual_play(&game, 0); + print_game(game); + manual_play(&game, 0); + manual_play(&game, 1); + print_game(game); game_destroy(&game); diff --git a/puissance.c b/puissance.c index fd0eb3ed835c996ee6e78ac53c77c163ed005d4f..18f02858f415e2a4f1bdecd333e49ea52d2da7d0 100644 --- a/puissance.c +++ b/puissance.c @@ -21,14 +21,14 @@ void game_init(puissance *p, GameMode mode, int row, int col) { // Set a default value for the data for (int r = 0; r < row; r++) { for (int c = 0; c < col; c++) { - p->data[r][c] = -1; + p->data[r][c] = EMPTY_CELL_VALUE; } } } void print_top(char *display, int col) { // Print top - strcat(display, "ā"); + strcat(display, "\nā"); for (int i = 1; i <= col * 2 - 1; i++) { if (i % 2 == 1) { strcat(display, "ā"); @@ -58,6 +58,7 @@ void print_bot(char *display, int col) { strcat(display, " "); strcat(display, i_has_string); } + strcat(display, "\n"); } void print_row_separator(char *display, int col) { @@ -107,17 +108,48 @@ void print_game(puissance p) { // Print the bottom then display the game print_bot(display, p.col); - printf("%s\n", display); + printf("%s", display); + // Clear the string to avoid SIGABRT + display[0] = '\0'; } void verify_game(puissance p) { } +int get_available_row_index(puissance *p, int selected_col_index) { + // Verify the index selected + if (selected_col_index >= p->col || selected_col_index < 0) { + return EMPTY_CELL_VALUE; + } + + for (int i = p->row - 1; i >= 0; i--) { + if (p->data[i][selected_col_index] == EMPTY_CELL_VALUE) { + return i; + } + } + return EMPTY_CELL_VALUE; +} + bool manual_play(puissance *p, int selected_col_index) { - bool valid_action = true; + // Get the available row index and verify that the column is not full. + int row_index = get_available_row_index(p, selected_col_index); + if (row_index == EMPTY_CELL_VALUE) { + return false; + } - return valid_action; + // Play + p->data[row_index][selected_col_index] = p->current_player; + + // Switch the current player + if (p->current_player == PLAYER_ONE) { + p->current_player = PLAYER_TWO; + } else { + p->current_player = PLAYER_ONE; + } + + // Valid the action + return true; } bool random_play(puissance *p) { diff --git a/puissance.h b/puissance.h index af9ea04b50cdc18036c18afc50d212511023084a..4a56adedcd757bf399dd315e0d739ec1ac18c640 100644 --- a/puissance.h +++ b/puissance.h @@ -13,8 +13,9 @@ #define COL_MIN 4 #define DEFAULT_ROW 6 #define DEFAULT_COL 7 -#define PLAYER_ONE_STRING "O" -#define PLAYER_TWO_STRING "X" +#define PLAYER_ONE_STRING "X" +#define PLAYER_TWO_STRING "O" +#define EMPTY_CELL_VALUE -1 typedef enum { RAND_AI, @@ -45,6 +46,7 @@ typedef struct _puissance { void game_init(puissance *p, GameMode mode, int row, int col); void print_game(puissance p); void verify_game(puissance p); +int get_available_row_index(puissance *p, int selected_col_index); bool manual_play(puissance *p, int selected_col_index); bool random_play(puissance *p); bool smart_play(puissance *p);