diff --git a/Makefile b/Makefile index 74ee4296372bd21424ee3201ad2e4eab84a0df67..a6096e2bd022f1957ca2ec85c5c76b221d81166a 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,11 @@ CFLAGS = -g -Wall -Wextra -Wpedantic LDLIBS = -lm LDFLAGS = -fsanitize=address -fsanitize=leak -puissance4: puissance4.c +puissance4: puissance4.c src/board.o $(CC) $(CFLAGS) $^ -o $@ $(LDLIBS) $(LDFLAGS) +board.o: src/board.h + clean: @echo "this rule must clean everything up (including candidate files in testbed)" $(MAKE) -C testbed clean diff --git a/puissance4.c b/puissance4.c index 8e2b82eab5662308b128877f27f96953e461bc6c..2bb6759a43661d00730618c0d3a34cf3fcd5f81b 100644 --- a/puissance4.c +++ b/puissance4.c @@ -2,31 +2,20 @@ #include <stdlib.h> #include <stdbool.h> +#include "src/board.h" + #define AI_SEED 0 #define MIN_COLUMN 4 #define MIN_ROW 4 -void init_board(int rows, int cols, int** board); -void print_board(int rows, int cols, int** board); -// struct coordinate add_token(int col, int player, int rows, int** board); bool does_player_win(int col, int row, int rows, int cols, int** board); void flush_input(); -int** create_board(int rows, int cols); -int destroy_board(int rows, int** board); bool is_valid_game_mode(int game_mode); bool is_valid_board_size(int rows, int cols); bool is_column_full(int col, int rows, int** board); 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); - - -enum STATE { - EMPTY = 0, - PLAYER1 = 1, - PLAYER2 = -1 -}; - struct coordinate { int x; int y; @@ -39,7 +28,6 @@ enum GAME_MODE { }; -char transform_state_to_char(enum STATE state); void place_token(int player, struct coordinate coordinate, int** board); struct coordinate find_empty_slot(int col, int rows, int** board); @@ -87,7 +75,6 @@ int main(int argc, char const* argv[]) { continue; } - // coordinate = add_token(player_selected_column, player % 2, rows, board); coordinate = find_empty_slot(player_selected_column, rows, board); if (coordinate.x != -1 && coordinate.y != -1) { place_token(player % 2, coordinate, board); @@ -108,7 +95,6 @@ int main(int argc, char const* argv[]) { } player++; - } if (destroy_board(rows, board) == EXIT_FAILURE) { fprintf(stderr, "Error: Unable to free memory for the board.\n"); @@ -208,41 +194,6 @@ void place_token(int player, struct coordinate coordinate, int** board) { } } -int** create_board(int rows, int cols) { - int** board = malloc(rows * sizeof(int*)); - if (board == NULL) { - return NULL; - } - for (int i = 0; i < rows; i++) { - board[i] = malloc(cols * sizeof(int)); - if (board[i] == NULL) { - for (int j = 0; j < i; j++) { - free(board[j]); - } - free(board); - return NULL; - } - } - return board; -} - -int destroy_board(int rows, int** board) { - for (int i = 0; i < rows; i++) { - free(board[i]); - } - free(board); - return EXIT_SUCCESS; -} - - -void init_board(int rows, int cols, int** board) { - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - board[i][j] = EMPTY; - } - } -} - void flush_input() { int c; while ((c = getchar()) != '\n' && c != EOF); @@ -296,68 +247,68 @@ bool does_player_win(int row, int col, int rows, int cols, int** board) { return false; } -char transform_state_to_char(enum STATE state) { - switch (state) { - case PLAYER1: - return 'X'; - break; - case PLAYER2: - return 'O'; - break; - default: - return ' '; - break; - } -} - -void print_board(int rows, int cols, int** board) { - // Top border of the board - printf("\n┌"); - for (int col = 0; col < cols; col++) { - printf("─"); - if (col < cols - 1) { - printf("┬"); - } - } - printf("┐\n"); - - // Print the board rows - for (int row = 0; row < rows; row++) { - // Print left vertical border - printf("│"); - // Print data and vertical separators - for (int col = 0; col < cols; col++) { - printf("%c", transform_state_to_char(board[row][col])); - if (col < cols - 1) { - printf("│"); - } - } - // Print right vertical border - printf("│\n"); - - // Print row separator - if (row < rows - 1) { - printf("├"); - for (int col = 0; col < cols; col++) { - printf("─"); - if (col < cols - 1) { - printf("┼"); - } - } - printf("┤\n"); - } - } - - // Print bottom border of the board - printf("└"); - for (int col = 0; col < cols; col++) { - printf("─"); - if (col < cols - 1) { - printf("┴"); - } - } - printf("┘\n"); - for (int i = 1; i < cols + 1; i++) { - printf(" %d", i); - } -} +// char transform_state_to_char(enum BOARD_STATE state) { +// switch (state) { +// case PLAYER1: +// return 'X'; +// break; +// case PLAYER2: +// return 'O'; +// break; +// default: +// return ' '; +// break; +// } +// } + +// void print_board(int rows, int cols, int** board) { +// // Top border of the board +// printf("\n┌"); +// for (int col = 0; col < cols; col++) { +// printf("─"); +// if (col < cols - 1) { +// printf("┬"); +// } +// } +// printf("┐\n"); + +// // Print the board rows +// for (int row = 0; row < rows; row++) { +// // Print left vertical border +// printf("│"); +// // Print data and vertical separators +// for (int col = 0; col < cols; col++) { +// printf("%c", transform_state_to_char(board[row][col])); +// if (col < cols - 1) { +// printf("│"); +// } +// } +// // Print right vertical border +// printf("│\n"); + +// // Print row separator +// if (row < rows - 1) { +// printf("├"); +// for (int col = 0; col < cols; col++) { +// printf("─"); +// if (col < cols - 1) { +// printf("┼"); +// } +// } +// printf("┤\n"); +// } +// } + +// // Print bottom border of the board +// printf("└"); +// for (int col = 0; col < cols; col++) { +// printf("─"); +// if (col < cols - 1) { +// printf("┴"); +// } +// } +// printf("┘\n"); +// for (int i = 1; i < cols + 1; i++) { +// printf(" %d", i); +// } +// } diff --git a/src/board.c b/src/board.c new file mode 100644 index 0000000000000000000000000000000000000000..432a70a1ddfb0ced52270db3abed450cb19efc4d --- /dev/null +++ b/src/board.c @@ -0,0 +1,103 @@ +#include "board.h" +#include <stdlib.h> +#include <stdio.h> + +void init_board(int rows, int cols, int** board) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + board[i][j] = EMPTY; + } + } +} + +int destroy_board(int rows, int** board) { + for (int i = 0; i < rows; i++) { + free(board[i]); + } + free(board); + return EXIT_SUCCESS; +} + +void print_board(int rows, int cols, int** board) { + // Top border of the board + printf("\n┌"); + for (int col = 0; col < cols; col++) { + printf("─"); + if (col < cols - 1) { + printf("┬"); + } + } + printf("┐\n"); + + // Print the board rows + for (int row = 0; row < rows; row++) { + // Print left vertical border + printf("│"); + // Print data and vertical separators + for (int col = 0; col < cols; col++) { + printf("%c", transform_state_to_char(board[row][col])); + if (col < cols - 1) { + printf("│"); + } + } + // Print right vertical border + printf("│\n"); + + // Print row separator + if (row < rows - 1) { + printf("├"); + for (int col = 0; col < cols; col++) { + printf("─"); + if (col < cols - 1) { + printf("┼"); + } + } + printf("┤\n"); + } + } + + // Print bottom border of the board + printf("└"); + for (int col = 0; col < cols; col++) { + printf("─"); + if (col < cols - 1) { + printf("┴"); + } + } + printf("┘\n"); + for (int i = 1; i < cols + 1; i++) { + printf(" %d", i); + } +} + +int** create_board(int rows, int cols) { + int** board = malloc(rows * sizeof(int*)); + if (board == NULL) { + return NULL; + } + for (int i = 0; i < rows; i++) { + board[i] = malloc(cols * sizeof(int)); + if (board[i] == NULL) { + for (int j = 0; j < i; j++) { + free(board[j]); + } + free(board); + return NULL; + } + } + return board; +} + +char transform_state_to_char(enum BOARD_STATE state) { + switch (state) { + case PLAYER1: + return 'X'; + break; + case PLAYER2: + return 'O'; + break; + default: + return ' '; + break; + } +} \ No newline at end of file diff --git a/src/board.h b/src/board.h new file mode 100644 index 0000000000000000000000000000000000000000..d40388d7712881b75478a0ca69b629239d0b2f8a --- /dev/null +++ b/src/board.h @@ -0,0 +1,15 @@ +#ifndef _BOARD_H_ + +enum BOARD_STATE { + EMPTY = 0, + PLAYER1 = 1, + PLAYER2 = -1 +}; + +void init_board(int rows, int cols, int** board); +void print_board(int rows, int cols, int** board); +int** create_board(int rows, int cols); +int destroy_board(int rows, int** board); +char transform_state_to_char(enum BOARD_STATE state); + +#endif \ No newline at end of file