Skip to content

Commit

Permalink
fix: move setDashboardFromFile function from libs to libs/game
Browse files Browse the repository at this point in the history
  • Loading branch information
hozlucas28 committed Oct 20, 2024
1 parent a9b055a commit ac94258
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 118 deletions.
98 changes: 98 additions & 0 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 @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions libs/game/methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
103 changes: 1 addition & 102 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 Expand Up @@ -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) {
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

0 comments on commit ac94258

Please sign in to comment.