From dc0a241fc88af3bc4f62a39f3a0f4ac7211e8d99 Mon Sep 17 00:00:00 2001 From: Anthony <bouillant.anthony@gmail.com> Date: Mon, 31 Mar 2025 18:12:12 +0200 Subject: [PATCH] Use memory allocation when creating board --- Makefile | 2 +- puissance4.c | 56 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 6ef9070..74ee429 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ clean: $(RM) -f puissance4 *.o *.cand run: puissance4 - ./$< + ./$< 3 5 6 tests: puissance4 $(MAKE) -C testbed diff --git a/puissance4.c b/puissance4.c index e9dd578..c9a67f8 100644 --- a/puissance4.c +++ b/puissance4.c @@ -2,11 +2,13 @@ #include <stdlib.h> #include <stdbool.h> -void init_board(int rows, int cols, int board[rows][cols]); -void print_board(int rows, int cols, int board[rows][cols]); -struct coordinate add_token(int col, int player, int rows, int cols, int board[rows][cols]); -bool is_player_winning(int col, int row, int rows, int cols, int board[rows][cols]); +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 is_player_winning(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); enum STATE { @@ -38,7 +40,11 @@ int main(int argc, char const* argv[]) { int rows = atoi(argv[2]); int cols = atoi(argv[3]); - int board[rows][cols]; + int** board = create_board(rows, cols); + if (board == NULL) { + fprintf(stderr, "Error: Unable to allocate memory for the board.\n"); + return EXIT_FAILURE; + } init_board(rows, cols, board); printf("Board size is %dx%d (rows x col)", rows, cols); @@ -54,7 +60,7 @@ int main(int argc, char const* argv[]) { if (col < 1 || col > cols) { continue; } - coordinate = add_token(col, player % 2, rows, cols, board); + coordinate = add_token(col, player % 2, rows, board); print_board(rows, cols, board); if (is_player_winning(coordinate.x, coordinate.y, rows, cols, board)) { printf("Player %s won!\n", player % 2 == 0 ? "one" : "two"); @@ -67,10 +73,14 @@ 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"); + return EXIT_FAILURE; + } return EXIT_SUCCESS; } -struct coordinate add_token(int col, int player, int rows, int cols, int board[rows][cols]) { +struct coordinate add_token(int col, int player, int rows, int** board) { struct coordinate coordinate = { 0,0 }; for (int i = rows - 1; i >= 0; i--) { if (board[i][col - 1] == EMPTY) { @@ -83,8 +93,34 @@ struct coordinate add_token(int col, int player, int rows, int cols, int board[r return coordinate; } +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[rows][cols]) { +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; @@ -97,7 +133,7 @@ void flush_input() { while ((c = getchar()) != '\n' && c != EOF); } -bool is_player_winning(int row, int col, int rows, int cols, int board[rows][cols]) { +bool is_player_winning(int row, int col, int rows, int cols, int** board) { int current_piece = board[row][col]; for (int i = 0; i < rows - 3; i++) { @@ -155,7 +191,7 @@ char transform_state_to_char(enum STATE state) { } } -void print_board(int rows, int cols, int board[rows][cols]) { +void print_board(int rows, int cols, int** board) { // Top border of the board printf("\n┌"); for (int col = 0; col < cols; col++) { -- GitLab