Skip to content

Commit ac94258

Browse files
committed
fix: move setDashboardFromFile function from libs to libs/game
1 parent a9b055a commit ac94258

File tree

4 files changed

+115
-118
lines changed

4 files changed

+115
-118
lines changed

libs/game/methods.c

Lines changed: 98 additions & 0 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"
@@ -203,6 +204,103 @@ void setDashboardCenter(TGame* pGame) {
203204
pGame->center[1] = col;
204205
}
205206

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

libs/game/methods.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ void printGameByConsole(TGame* pGame);
9999
*/
100100
void setDashboardCenter(TGame* pGame);
101101

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

libs/utilities.c

Lines changed: 1 addition & 102 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));
@@ -175,8 +75,7 @@ char** new2DArray(const int rows, const int cols) {
17575

17676
void sleep(int miliseconds) {
17777
const clock_t startTime = clock();
178-
while (clock() < (startTime + miliseconds))
179-
;
78+
while (clock() < (startTime + miliseconds));
18079
}
18180

18281
int strcmpi(const char* str01, const char* str02) {

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)