Skip to content

Commit 2860003

Browse files
authored
Merge develop branch into Master branch - Release v1.4.1 (#10)
- Improve preview image - Prevent dashboard size to be less than pattern size - Resolve Upload demo video #2
2 parents bf40d61 + 43cd59d commit 2860003

File tree

7 files changed

+138
-121
lines changed

7 files changed

+138
-121
lines changed

.github/statics/preview.png

68.1 KB
Loading

.github/translations/es/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</p>
3232

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

3737
## Resumen

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</p>
3232

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

3737
## Summary

libs/game/methods.c

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <limits.h>
55
#include <stdio.h>
6+
#include <string.h>
67

78
#include "../patterns/main.h"
89
#include "../utilities.h"
@@ -74,8 +75,24 @@ void drawPatternInDashboard(TGame* pGame, TPattern* pPattern) {
7475
size_t pI = 0;
7576
size_t pJ = 0;
7677

77-
const int startRow = pGame->center[0] - pPattern->center[0];
78-
const int startCol = pGame->center[1] - pPattern->center[1];
78+
int startRow;
79+
int startCol;
80+
81+
if (pPattern->rows > pGame->rows || pPattern->cols > pGame->cols) {
82+
destroy2DArray(pGame->dashboard, pGame->rows, pGame->cols);
83+
84+
pGame->dashboard = new2DArray(pPattern->rows, pPattern->cols);
85+
pGame->rows = pPattern->rows;
86+
pGame->cols = pPattern->cols;
87+
pGame->cellsDead = (pGame->rows * pGame->cols) - pGame->cellsAlive;
88+
89+
setDashboardCenter(pGame);
90+
91+
fillDashboard(pGame, DEAD_CELL);
92+
}
93+
94+
startRow = pGame->center[0] - pPattern->center[0];
95+
startCol = pGame->center[1] - pPattern->center[1];
7996

8097
for (i = startRow; pI < pPattern->rows; i++) {
8198
if (i < 0) continue;
@@ -203,6 +220,103 @@ void setDashboardCenter(TGame* pGame) {
203220
pGame->center[1] = col;
204221
}
205222

223+
int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols) {
224+
FILE* pf;
225+
TPattern pattern;
226+
227+
char* line;
228+
const size_t lineLength = 100;
229+
230+
char* row;
231+
char* col;
232+
char* sep;
233+
234+
int rowInt;
235+
int colInt;
236+
237+
int rows = minRows;
238+
int cols = minCols;
239+
240+
int patternRows = 0;
241+
int patternCols = 0;
242+
243+
pf = fopen(filePath, "rt");
244+
if (pf == NULL) return 0;
245+
246+
line = malloc(sizeof(char) * (lineLength + 1));
247+
if (line == NULL) {
248+
fclose(pf);
249+
return 0;
250+
};
251+
*(line + lineLength) = '\0';
252+
253+
fgets(line, lineLength, pf);
254+
255+
while (fgets(line, lineLength, pf)) {
256+
row = line;
257+
sep = strrchr(line, ';');
258+
if (sep == NULL) continue;
259+
260+
*sep = '\0';
261+
col = sep + 1;
262+
263+
sscanf(row, "%d", &rowInt);
264+
sscanf(col, "%d", &colInt);
265+
266+
patternRows = MAX(rowInt, patternRows);
267+
patternCols = MAX(colInt, patternCols);
268+
}
269+
270+
rows = MAX(patternRows, rows);
271+
cols = MAX(patternCols, cols);
272+
273+
pGame->dashboard = new2DArray(rows, cols);
274+
pGame->rows = rows;
275+
pGame->cols = cols;
276+
pGame->cellsAlive = 0;
277+
pGame->generation = 0;
278+
279+
setDashboardCenter(pGame);
280+
281+
fillDashboard(pGame, DEAD_CELL);
282+
283+
pattern.arr = new2DArray(patternRows, patternCols);
284+
pattern.rows = patternRows;
285+
pattern.cols = patternCols;
286+
287+
setPatternCenter(&pattern);
288+
289+
fillPattern(&pattern, DEAD_CELL);
290+
291+
rewind(pf);
292+
fgets(line, lineLength, pf);
293+
294+
while (fgets(line, lineLength, pf)) {
295+
row = line;
296+
sep = strrchr(line, ';');
297+
if (sep == NULL) continue;
298+
299+
*sep = '\0';
300+
col = sep + 1;
301+
302+
sscanf(row, "%d", &rowInt);
303+
sscanf(col, "%d", &colInt);
304+
305+
pattern.arr[rowInt - 1][colInt - 1] = ALIVE_CELL;
306+
pGame->cellsAlive++;
307+
}
308+
309+
pGame->cellsDead = (cols * rows) - pGame->cellsAlive;
310+
311+
drawPatternInDashboard(pGame, &pattern);
312+
destroy2DArray(pattern.arr, pattern.rows, pattern.cols);
313+
314+
fclose(pf);
315+
free(line);
316+
317+
return 1;
318+
}
319+
206320
void startGameByConsole(TGame* pGame, const int maxGeneration, const int delayBetweenGenerations) {
207321
size_t generation = 0;
208322
unsigned char isToInfinity = maxGeneration == INT_MAX;

libs/game/methods.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ int countAliveNeighbors(TGame* pGame, const int row, const int col, const int ra
2929
* @param pGame Pointer to the Conway's Game of Life structure where the pattern will be drawn.
3030
* @param pattern Pattern to be drawn.
3131
*
32-
* @warning The pattern must be `glider`, `toad`, `press`, or `glider cannon`.
32+
* @warning The pattern must be one of the following: `glider`, `toad`, `beacon`, or `glider
33+
* cannon`. If the number of rows or columns of the Conway's Game of Life board are less than the
34+
* number of rows or columns of the pattern, the board will be resized to match the pattern's
35+
* dimensions.
3336
*/
3437
void drawPattern(TGame* pGame, const char* pattern);
3538

@@ -99,6 +102,22 @@ void printGameByConsole(TGame* pGame);
99102
*/
100103
void setDashboardCenter(TGame* pGame);
101104

105+
/**
106+
* @brief Sets a Conway's Game of Life dashboard based on a file.
107+
*
108+
* This function reads a file content and updates a Conway's Game of Life structure with the parsed
109+
* content to set the dashboard. Also, it modifies the `rows`, `cols`, `center`, `cellsAlive`, and
110+
* `cellsDead` field of the Conway's Game of Life structure.
111+
*
112+
* @param filePath File path with the content to be parsed.
113+
* @param pGame Pointer to the Conway's Game of Life structure.
114+
* @param minRows Minimum number of rows for the dashboard.
115+
* @param minCols Minimum number of columns for the dashboard.
116+
*
117+
* @return Returns `1` on success, otherwise returns `0`.
118+
*/
119+
int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols);
120+
102121
/**
103122
* @brief Starts a Conway's Game of Life game using the console as the output.
104123
*

libs/utilities.c

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
#include <string.h>
88
#include <time.h>
99

10-
#include "./macros.h"
11-
#include "./patterns/main.h"
12-
1310
void destroy2DArray(char** arr, const int rows, const int cols) {
1411
size_t i;
1512

@@ -20,103 +17,6 @@ void destroy2DArray(char** arr, const int rows, const int cols) {
2017
free(arr);
2118
}
2219

23-
int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols) {
24-
FILE* pf;
25-
TPattern pattern;
26-
27-
char* line;
28-
const size_t lineLength = 100;
29-
30-
char* row;
31-
char* col;
32-
char* sep;
33-
34-
int rowInt;
35-
int colInt;
36-
37-
int rows = minRows;
38-
int cols = minCols;
39-
40-
int patternRows = 0;
41-
int patternCols = 0;
42-
43-
pf = fopen(filePath, "rt");
44-
if (pf == NULL) return 0;
45-
46-
line = malloc(sizeof(char) * (lineLength + 1));
47-
if (line == NULL) {
48-
fclose(pf);
49-
return 0;
50-
};
51-
*(line + lineLength) = '\0';
52-
53-
fgets(line, lineLength, pf);
54-
55-
while (fgets(line, lineLength, pf)) {
56-
row = line;
57-
sep = strrchr(line, ';');
58-
if (sep == NULL) continue;
59-
60-
*sep = '\0';
61-
col = sep + 1;
62-
63-
sscanf(row, "%d", &rowInt);
64-
sscanf(col, "%d", &colInt);
65-
66-
patternRows = MAX(rowInt, patternRows);
67-
patternCols = MAX(colInt, patternCols);
68-
}
69-
70-
rows = MAX(patternRows, rows);
71-
cols = MAX(patternCols, cols);
72-
73-
pGame->dashboard = new2DArray(rows, cols);
74-
pGame->rows = rows;
75-
pGame->cols = cols;
76-
pGame->cellsAlive = 0;
77-
pGame->generation = 0;
78-
79-
setDashboardCenter(pGame);
80-
81-
fillDashboard(pGame, DEAD_CELL);
82-
83-
pattern.arr = new2DArray(patternRows, patternCols);
84-
pattern.rows = patternRows;
85-
pattern.cols = patternCols;
86-
87-
setPatternCenter(&pattern);
88-
89-
fillPattern(&pattern, DEAD_CELL);
90-
91-
rewind(pf);
92-
fgets(line, lineLength, pf);
93-
94-
while (fgets(line, lineLength, pf)) {
95-
row = line;
96-
sep = strrchr(line, ';');
97-
if (sep == NULL) continue;
98-
99-
*sep = '\0';
100-
col = sep + 1;
101-
102-
sscanf(row, "%d", &rowInt);
103-
sscanf(col, "%d", &colInt);
104-
105-
pattern.arr[rowInt - 1][colInt - 1] = ALIVE_CELL;
106-
pGame->cellsAlive++;
107-
}
108-
109-
pGame->cellsDead = (cols * rows) - pGame->cellsAlive;
110-
111-
drawPatternInDashboard(pGame, &pattern);
112-
destroy2DArray(pattern.arr, pattern.rows, pattern.cols);
113-
114-
fclose(pf);
115-
free(line);
116-
117-
return 1;
118-
}
119-
12020
char* getUserInputStr(const char* message, const char* onInvalidMessage, const int strLength,
12121
unsigned char (*validator)(const char* userInput)) {
12222
char* userInput = malloc(strLength * sizeof(char));

libs/utilities.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,6 @@
2424
*/
2525
void destroy2DArray(char** arr, const int rows, const int cols);
2626

27-
/**
28-
* @brief Sets a Conway's Game of Life dashboard based on a file.
29-
*
30-
* This function reads a file content and updates a Conway's Game of Life structure with the parsed
31-
* content to set the dashboard. Also, it modifies the `rows`, `cols`, `center`, `cellsAlive`, and
32-
* `cellsDead` field of the Conway's Game of Life structure.
33-
*
34-
* @param filePath File path with the content to be parsed.
35-
* @param pGame Pointer to the Conway's Game of Life structure.
36-
* @param minRows Minimum number of rows for the dashboard.
37-
* @param minCols Minimum number of columns for the dashboard.
38-
*
39-
* @return Returns `1` on success, otherwise returns `0`.
40-
*/
41-
int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols);
42-
4327
/**
4428
* @brief Gets user input as a string.
4529
*

0 commit comments

Comments
 (0)