From ac942584737e5af5fbac0aa57dbfa8998a2bf4f9 Mon Sep 17 00:00:00 2001 From: hozlucas28 Date: Sun, 20 Oct 2024 20:04:47 -0300 Subject: [PATCH] fix: move `setDashboardFromFile` function from `libs` to `libs/game` --- libs/game/methods.c | 98 +++++++++++++++++++++++++++++++++++++++++ libs/game/methods.h | 16 +++++++ libs/utilities.c | 103 +------------------------------------------- libs/utilities.h | 16 ------- 4 files changed, 115 insertions(+), 118 deletions(-) diff --git a/libs/game/methods.c b/libs/game/methods.c index 5c93738..dd5bcf8 100644 --- a/libs/game/methods.c +++ b/libs/game/methods.c @@ -3,6 +3,7 @@ #include #include +#include #include "../patterns/main.h" #include "../utilities.h" @@ -203,6 +204,103 @@ void setDashboardCenter(TGame* pGame) { pGame->center[1] = col; } +int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols) { + FILE* pf; + TPattern pattern; + + char* line; + const size_t lineLength = 100; + + char* row; + char* col; + char* sep; + + int rowInt; + int colInt; + + int rows = minRows; + int cols = minCols; + + int patternRows = 0; + int patternCols = 0; + + pf = fopen(filePath, "rt"); + if (pf == NULL) return 0; + + line = malloc(sizeof(char) * (lineLength + 1)); + if (line == NULL) { + fclose(pf); + return 0; + }; + *(line + lineLength) = '\0'; + + fgets(line, lineLength, pf); + + while (fgets(line, lineLength, pf)) { + row = line; + sep = strrchr(line, ';'); + if (sep == NULL) continue; + + *sep = '\0'; + col = sep + 1; + + sscanf(row, "%d", &rowInt); + sscanf(col, "%d", &colInt); + + patternRows = MAX(rowInt, patternRows); + patternCols = MAX(colInt, patternCols); + } + + rows = MAX(patternRows, rows); + cols = MAX(patternCols, cols); + + pGame->dashboard = new2DArray(rows, cols); + pGame->rows = rows; + pGame->cols = cols; + pGame->cellsAlive = 0; + pGame->generation = 0; + + setDashboardCenter(pGame); + + fillDashboard(pGame, DEAD_CELL); + + pattern.arr = new2DArray(patternRows, patternCols); + pattern.rows = patternRows; + pattern.cols = patternCols; + + setPatternCenter(&pattern); + + fillPattern(&pattern, DEAD_CELL); + + rewind(pf); + fgets(line, lineLength, pf); + + while (fgets(line, lineLength, pf)) { + row = line; + sep = strrchr(line, ';'); + if (sep == NULL) continue; + + *sep = '\0'; + col = sep + 1; + + sscanf(row, "%d", &rowInt); + sscanf(col, "%d", &colInt); + + pattern.arr[rowInt - 1][colInt - 1] = ALIVE_CELL; + pGame->cellsAlive++; + } + + pGame->cellsDead = (cols * rows) - pGame->cellsAlive; + + drawPatternInDashboard(pGame, &pattern); + destroy2DArray(pattern.arr, pattern.rows, pattern.cols); + + fclose(pf); + free(line); + + return 1; +} + void startGameByConsole(TGame* pGame, const int maxGeneration, const int delayBetweenGenerations) { size_t generation = 0; unsigned char isToInfinity = maxGeneration == INT_MAX; diff --git a/libs/game/methods.h b/libs/game/methods.h index 8734d56..2d7be80 100644 --- a/libs/game/methods.h +++ b/libs/game/methods.h @@ -99,6 +99,22 @@ void printGameByConsole(TGame* pGame); */ void setDashboardCenter(TGame* pGame); +/** + * @brief Sets a Conway's Game of Life dashboard based on a file. + * + * This function reads a file content and updates a Conway's Game of Life structure with the parsed + * content to set the dashboard. Also, it modifies the `rows`, `cols`, `center`, `cellsAlive`, and + * `cellsDead` field of the Conway's Game of Life structure. + * + * @param filePath File path with the content to be parsed. + * @param pGame Pointer to the Conway's Game of Life structure. + * @param minRows Minimum number of rows for the dashboard. + * @param minCols Minimum number of columns for the dashboard. + * + * @return Returns `1` on success, otherwise returns `0`. + */ +int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols); + /** * @brief Starts a Conway's Game of Life game using the console as the output. * diff --git a/libs/utilities.c b/libs/utilities.c index 8919bda..1942f00 100644 --- a/libs/utilities.c +++ b/libs/utilities.c @@ -7,9 +7,6 @@ #include #include -#include "./macros.h" -#include "./patterns/main.h" - void destroy2DArray(char** arr, const int rows, const int cols) { size_t i; @@ -20,103 +17,6 @@ void destroy2DArray(char** arr, const int rows, const int cols) { free(arr); } -int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols) { - FILE* pf; - TPattern pattern; - - char* line; - const size_t lineLength = 100; - - char* row; - char* col; - char* sep; - - int rowInt; - int colInt; - - int rows = minRows; - int cols = minCols; - - int patternRows = 0; - int patternCols = 0; - - pf = fopen(filePath, "rt"); - if (pf == NULL) return 0; - - line = malloc(sizeof(char) * (lineLength + 1)); - if (line == NULL) { - fclose(pf); - return 0; - }; - *(line + lineLength) = '\0'; - - fgets(line, lineLength, pf); - - while (fgets(line, lineLength, pf)) { - row = line; - sep = strrchr(line, ';'); - if (sep == NULL) continue; - - *sep = '\0'; - col = sep + 1; - - sscanf(row, "%d", &rowInt); - sscanf(col, "%d", &colInt); - - patternRows = MAX(rowInt, patternRows); - patternCols = MAX(colInt, patternCols); - } - - rows = MAX(patternRows, rows); - cols = MAX(patternCols, cols); - - pGame->dashboard = new2DArray(rows, cols); - pGame->rows = rows; - pGame->cols = cols; - pGame->cellsAlive = 0; - pGame->generation = 0; - - setDashboardCenter(pGame); - - fillDashboard(pGame, DEAD_CELL); - - pattern.arr = new2DArray(patternRows, patternCols); - pattern.rows = patternRows; - pattern.cols = patternCols; - - setPatternCenter(&pattern); - - fillPattern(&pattern, DEAD_CELL); - - rewind(pf); - fgets(line, lineLength, pf); - - while (fgets(line, lineLength, pf)) { - row = line; - sep = strrchr(line, ';'); - if (sep == NULL) continue; - - *sep = '\0'; - col = sep + 1; - - sscanf(row, "%d", &rowInt); - sscanf(col, "%d", &colInt); - - pattern.arr[rowInt - 1][colInt - 1] = ALIVE_CELL; - pGame->cellsAlive++; - } - - pGame->cellsDead = (cols * rows) - pGame->cellsAlive; - - drawPatternInDashboard(pGame, &pattern); - destroy2DArray(pattern.arr, pattern.rows, pattern.cols); - - fclose(pf); - free(line); - - return 1; -} - char* getUserInputStr(const char* message, const char* onInvalidMessage, const int strLength, unsigned char (*validator)(const char* userInput)) { char* userInput = malloc(strLength * sizeof(char)); @@ -175,8 +75,7 @@ char** new2DArray(const int rows, const int cols) { void sleep(int miliseconds) { const clock_t startTime = clock(); - while (clock() < (startTime + miliseconds)) - ; + while (clock() < (startTime + miliseconds)); } int strcmpi(const char* str01, const char* str02) { diff --git a/libs/utilities.h b/libs/utilities.h index ae8ed73..aae62bc 100644 --- a/libs/utilities.h +++ b/libs/utilities.h @@ -24,22 +24,6 @@ */ void destroy2DArray(char** arr, const int rows, const int cols); -/** - * @brief Sets a Conway's Game of Life dashboard based on a file. - * - * This function reads a file content and updates a Conway's Game of Life structure with the parsed - * content to set the dashboard. Also, it modifies the `rows`, `cols`, `center`, `cellsAlive`, and - * `cellsDead` field of the Conway's Game of Life structure. - * - * @param filePath File path with the content to be parsed. - * @param pGame Pointer to the Conway's Game of Life structure. - * @param minRows Minimum number of rows for the dashboard. - * @param minCols Minimum number of columns for the dashboard. - * - * @return Returns `1` on success, otherwise returns `0`. - */ -int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols); - /** * @brief Gets user input as a string. *