Skip to content
Snippets Groups Projects
Commit dc0a241f authored by anthony.bouillan's avatar anthony.bouillan
Browse files

Use memory allocation when creating board

parent 83bf4eb8
No related branches found
No related tags found
No related merge requests found
Pipeline #38995 failed
......@@ -12,7 +12,7 @@ clean:
$(RM) -f puissance4 *.o *.cand
run: puissance4
./$<
./$< 3 5 6
tests: puissance4
$(MAKE) -C testbed
......
......@@ -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++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment