From 5836e13977a7bf5ff7e6907be81cda97e8a177e9 Mon Sep 17 00:00:00 2001
From: Anthony <bouillant.anthony@gmail.com>
Date: Fri, 4 Apr 2025 10:52:35 +0200
Subject: [PATCH] refactor setup_board function

---
 src/board/test_board.c       | 4 ++--
 src/computer/test_computer.c | 6 +++---
 src/game/test_game.c         | 6 +++---
 src/test_puissance4.c        | 9 ++-------
 src/test_puissance4.h        | 2 +-
 5 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/src/board/test_board.c b/src/board/test_board.c
index 5ef7722..4d38562 100644
--- a/src/board/test_board.c
+++ b/src/board/test_board.c
@@ -24,8 +24,8 @@ void test_create_board() {
         assert(board[i] != NULL && RED "Row allocation failed\n" RESET);
     }
 
-    printf(GREEN "test_create_board: PASSED\n" RESET);
     destroy_board(rows, board);
+    printf(GREEN "test_create_board: PASSED\n" RESET);
 }
 
 void test_fill_board() {
@@ -42,6 +42,6 @@ void test_fill_board() {
         }
     }
 
-    printf(GREEN "test_fill_board: PASSED\n" RESET);
     destroy_board(rows, board);
+    printf(GREEN "test_fill_board: PASSED\n" RESET);
 }
diff --git a/src/computer/test_computer.c b/src/computer/test_computer.c
index 35e7122..c23e4ce 100644
--- a/src/computer/test_computer.c
+++ b/src/computer/test_computer.c
@@ -2,7 +2,7 @@
 
 void test_is_column_full() {
     int rows = 6, cols = 7;
-    int** board = setup_board(rows, cols, EMPTY);
+    int** board = setup_board(rows, cols);
 
     // An empty column should not be considered full
     assert(is_column_full(1, rows, board) == false && RED "test_is_column_full: FAILED - column should not be full\n" RESET);
@@ -19,7 +19,7 @@ void test_is_column_full() {
 
 void test_select_smart_column() {
     int rows = 6, cols = 7;
-    int** board = setup_board(rows, cols, EMPTY);
+    int** board = setup_board(rows, cols);
 
     // Simulate a situation where the AI can win immediately
     board[5][0] = PLAYER2;
@@ -41,7 +41,7 @@ void test_select_smart_column() {
 
 void test_select_random_column() {
     int rows = 6, cols = 7;
-    int** board = setup_board(rows, cols, EMPTY);
+    int** board = setup_board(rows, cols);
 
     // Select a random column on an empty board
     int col = select_random_column(cols, rows, board);
diff --git a/src/game/test_game.c b/src/game/test_game.c
index 7f2ec44..30d9631 100644
--- a/src/game/test_game.c
+++ b/src/game/test_game.c
@@ -2,7 +2,7 @@
 
 void test_place_token() {
     int rows = 6, cols = 7;
-    int** board = setup_board(rows, cols, EMPTY);
+    int** board = setup_board(rows, cols);
 
     // Player1
     struct coordinate coord = { 2, 3 };
@@ -21,7 +21,7 @@ void test_place_token() {
 
 void test_find_empty_slot() {
     int rows = 6, cols = 7;
-    int** board = setup_board(rows, cols, EMPTY);
+    int** board = setup_board(rows, cols);
 
     // Search for an empty slot in a column
     board[5][2] = PLAYER1;
@@ -58,7 +58,7 @@ void test_is_board_full() {
 
 void test_does_player_win() {
     int rows = 6, cols = 7;
-    int** board = setup_board(rows, cols, EMPTY);
+    int** board = setup_board(rows, cols);
 
     // Test horizontal win
     board[5][0] = PLAYER1;
diff --git a/src/test_puissance4.c b/src/test_puissance4.c
index 74004d5..e3ee464 100644
--- a/src/test_puissance4.c
+++ b/src/test_puissance4.c
@@ -22,18 +22,13 @@ int main() {
     return EXIT_SUCCESS;
 }
 
-int** setup_board(int rows, int cols, int default_value) {
+int** setup_board(int rows, int cols) {
     int** board = create_board(rows, cols);
     if (board == NULL) {
         fprintf(stderr, "Error: Unable to allocate memory for the board.\n");
         exit(EXIT_FAILURE);
     }
-
-    for (int i = 0; i < rows; i++) {
-        for (int j = 0; j < cols; j++) {
-            board[i][j] = default_value;
-        }
-    }
+    fill_board(rows, cols, board);
 
     return board;
 }
diff --git a/src/test_puissance4.h b/src/test_puissance4.h
index 3b4ac31..c44495a 100644
--- a/src/test_puissance4.h
+++ b/src/test_puissance4.h
@@ -15,7 +15,7 @@
 #define RED "\033[1;31m"
 #define RESET "\033[0m"
 
-int** setup_board(int rows, int cols, int default_value);
+int** setup_board(int rows, int cols);
 void reset_board(int rows, int cols, int** board);
 
 #endif
-- 
GitLab