Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .github/statics/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .github/translations/es/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</p>

<p align="center">
<strong><a href="#" target="_blank">(video demostrativo)</a></strong>
<strong><a href="https://youtu.be/o5M8t04p9Es?si=9KL47cKXzm7n2_Mw" target="_blank">(video demostrativo)</a></strong>
</p>

## Resumen
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</p>

<p align="center">
<strong><a href="#" target="_blank">(demonstration video)</a></strong>
<strong><a href="https://youtu.be/o5M8t04p9Es?si=9KL47cKXzm7n2_Mw" target="_blank">(demonstration video)</a></strong>
</p>

## Summary
Expand Down
118 changes: 116 additions & 2 deletions libs/game/methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <limits.h>
#include <stdio.h>
#include <string.h>

#include "../patterns/main.h"
#include "../utilities.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 20 additions & 1 deletion libs/game/methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*
Expand Down
100 changes: 0 additions & 100 deletions libs/utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include <string.h>
#include <time.h>

#include "./macros.h"
#include "./patterns/main.h"

void destroy2DArray(char** arr, const int rows, const int cols) {
size_t i;

Expand All @@ -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));
Expand Down
16 changes: 0 additions & 16 deletions libs/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down