diff --git a/.github/statics/preview.png b/.github/statics/preview.png index 4c1d97b..d3a0e72 100644 Binary files a/.github/statics/preview.png and b/.github/statics/preview.png differ diff --git a/.github/translations/es/README.md b/.github/translations/es/README.md index df3d398..3748a82 100644 --- a/.github/translations/es/README.md +++ b/.github/translations/es/README.md @@ -31,7 +31,7 @@

- (video demostrativo) + (video demostrativo)

## Resumen diff --git a/README.md b/README.md index 67217c1..66aa498 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@

- (demonstration video) + (demonstration video)

## Summary diff --git a/libs/game/methods.c b/libs/game/methods.c index 5c93738..370d2fe 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" @@ -74,8 +75,24 @@ void drawPatternInDashboard(TGame* pGame, TPattern* pPattern) { size_t pI = 0; size_t pJ = 0; - const int startRow = pGame->center[0] - pPattern->center[0]; - const int startCol = pGame->center[1] - pPattern->center[1]; + int startRow; + int startCol; + + if (pPattern->rows > pGame->rows || pPattern->cols > pGame->cols) { + destroy2DArray(pGame->dashboard, pGame->rows, pGame->cols); + + pGame->dashboard = new2DArray(pPattern->rows, pPattern->cols); + pGame->rows = pPattern->rows; + pGame->cols = pPattern->cols; + pGame->cellsDead = (pGame->rows * pGame->cols) - pGame->cellsAlive; + + setDashboardCenter(pGame); + + fillDashboard(pGame, DEAD_CELL); + } + + startRow = pGame->center[0] - pPattern->center[0]; + startCol = pGame->center[1] - pPattern->center[1]; for (i = startRow; pI < pPattern->rows; i++) { if (i < 0) continue; @@ -203,6 +220,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..acff2ba 100644 --- a/libs/game/methods.h +++ b/libs/game/methods.h @@ -29,7 +29,10 @@ int countAliveNeighbors(TGame* pGame, const int row, const int col, const int ra * @param pGame Pointer to the Conway's Game of Life structure where the pattern will be drawn. * @param pattern Pattern to be drawn. * - * @warning The pattern must be `glider`, `toad`, `press`, or `glider cannon`. + * @warning The pattern must be one of the following: `glider`, `toad`, `beacon`, or `glider + * cannon`. If the number of rows or columns of the Conway's Game of Life board are less than the + * number of rows or columns of the pattern, the board will be resized to match the pattern's + * dimensions. */ void drawPattern(TGame* pGame, const char* pattern); @@ -99,6 +102,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..d662b06 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)); 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. *