Skip to content

Commit

Permalink
Merge develop branch into Master branch - Release v1.2.0 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hozlucas28 authored Sep 21, 2024
2 parents b9b7d36 + 591f1da commit f8bd1e6
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 15 deletions.
5 changes: 1 addition & 4 deletions libs/game/methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ int countAliveNeighbors(TGame* pGame, int cellRow, int cellCol, int radius) {
void drawPattern(TGame* pGame, char* pattern) {
TPattern SPattern;

char arr[PATTERN_ROWS][PATTERN_COLS];

SPattern.arr = arr;

fillDashboard(pGame, DEAD_CELL);

if (strcmpi(pattern, "glider") == 0) {
Expand All @@ -67,6 +63,7 @@ void drawPattern(TGame* pGame, char* pattern) {
pGame->generation = 0;

drawPatternInDashboard(pGame, &SPattern);
destroy2DArray(SPattern.arr, SPattern.rows, SPattern.cols);
}

void drawPatternInDashboard(TGame* pGame, TPattern* pPattern) {
Expand Down
14 changes: 7 additions & 7 deletions libs/game/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* columns in the dashboard, and the values to represent alive and dead cells.
*/
typedef struct {
char (*dashboard)[DASHBOARD_COLS]; /** Board (2D array) in which the cells moves. */
int rows; /** Number of rows in `dashboard`. */
int cols; /** Number of columns in `dashboard`. */
int center[2]; /** Array (row, and column) representing the center of the `dashboard`. */
int cellsAlive; /** Number of alive cells. */
int cellsDead; /** Number of dead cells. */
int generation; /** Represents the generation number. */
char **dashboard; /** Board (2D array) in which the cells moves. */
int rows; /** Number of rows in `dashboard`. */
int cols; /** Number of columns in `dashboard`. */
int center[2]; /** Array (row, and column) representing the center of the `dashboard`. */
int cellsAlive; /** Number of alive cells. */
int cellsDead; /** Number of dead cells. */
int generation; /** Represents the generation number. */
int maximumGeneration; /** Maximum number of generations to be processed. */
int delayBetweenGenerations; /** Delay time in milliseconds for processed the next generation.*/
} TGame;
Expand Down
5 changes: 5 additions & 0 deletions libs/patterns/constructors.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ void newGliderPattern(TPattern* pattern) {
pattern->rows = 3;
pattern->cols = 3;

pattern->arr = new2DArray(pattern->rows, pattern->cols);

setPatternCenter(pattern);

fillPattern(pattern, DEAD_CELL);
Expand All @@ -23,6 +25,7 @@ void newGliderPattern(TPattern* pattern) {
void newGliderCannonPattern(TPattern* pattern) {
pattern->rows = 9;
pattern->cols = 36;
pattern->arr = new2DArray(pattern->rows, pattern->cols);

setPatternCenter(pattern);

Expand Down Expand Up @@ -71,6 +74,7 @@ void newGliderCannonPattern(TPattern* pattern) {
void newPressPattern(TPattern* pattern) {
pattern->rows = 13;
pattern->cols = 13;
pattern->arr = new2DArray(pattern->rows, pattern->cols);

setPatternCenter(pattern);

Expand Down Expand Up @@ -138,6 +142,7 @@ void newPressPattern(TPattern* pattern) {
void newToadPattern(TPattern* pattern) {
pattern->rows = 2;
pattern->cols = 4;
pattern->arr = new2DArray(pattern->rows, pattern->cols);

setPatternCenter(pattern);

Expand Down
6 changes: 3 additions & 3 deletions libs/patterns/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* the pattern, as well as other properties such as the number of rows and columns of the pattern.
*/
typedef struct {
char (*arr)[PATTERN_COLS]; /** 2D array representing the pattern. */
int rows; /** Number of rows of the pattern. */
int cols; /** Number of columns of the pattern. */
char** arr; /** 2D array representing the pattern. */
int rows; /** Number of rows of the pattern. */
int cols; /** Number of columns of the pattern. */
int center[2]; /** Array (row, and column) representing the center of the pattern. */
} TPattern;

Expand Down
31 changes: 31 additions & 0 deletions libs/utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@

#include "./patterns/main.h"

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

for (i = 0; i < rows; i++) {
free(*(arr + i));
}

free(arr);
}

char* getUserInputStr(char* message, char* onInvalidMessage, int strLength,
int (*validator)(char* userInput)) {
char* userInput = malloc(strLength * sizeof(char));
Expand Down Expand Up @@ -43,6 +53,27 @@ int isStrIn(char* str, char* arr[], int size) {
return 0;
}

char** new2DArray(int rows, int cols) {
char** bidimensionalArr;
int i;

bidimensionalArr = malloc(rows * sizeof(char*));
if (bidimensionalArr == NULL) {
printf("Memory allocation failed!\n");
exit(EXIT_FAILURE);
}

for (i = 0; i < rows; i++) {
*(bidimensionalArr + i) = malloc(cols * sizeof(char));
if (*(bidimensionalArr + i) == NULL) {
printf("Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
}

return bidimensionalArr;
}

void sleep(int miliseconds) {
clock_t startTime = clock();
while (clock() < (startTime + miliseconds))
Expand Down
6 changes: 6 additions & 0 deletions libs/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "./macros.h"
#include "./patterns/main.h"

// TODO: Documentation
void destroy2DArray(char** arr, int rows, int cols);

/**
* @brief Gets user input as a string.
*
Expand Down Expand Up @@ -39,6 +42,9 @@ char* getUserInputStr(char* message, char* onInvalidMessage, int strLength,
*/
int isStrIn(char* str, char* arr[], int arrLength);

// TODO: Documentation
char** new2DArray(int rows, int cols);

/**
* @brief Pauses the execution of the program.
*
Expand Down
14 changes: 13 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

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

#include "./sdl/main.h"

int main() {
TGame game;

char dashboard[DASHBOARD_ROWS][DASHBOARD_COLS];
int rows = DASHBOARD_ROWS;
int cols = DASHBOARD_COLS;
char** dashboard = new2DArray(rows, cols);

char* requestedPattern;
char* maxGeneration;
Expand Down Expand Up @@ -42,6 +43,8 @@ int main() {

drawPattern(&game, requestedPattern);

free(requestedPattern);

/* ----------------------- Request Maximum Generation ----------------------- */

maxGeneration = getUserInputStr(
Expand All @@ -51,12 +54,15 @@ int main() {
sscanf(maxGeneration, "%d", &maxGenerationInt);

if (maxGenerationInt < 0) {
free(maxGeneration);
maxGeneration = "infinity";
maxGenerationInt = INT_MAX;
};

printf("> Maximum generation received: %s.\n\n", maxGeneration);

if (maxGenerationInt != INT_MAX) free(maxGeneration);

/* ------------------------------ Request Delay ----------------------------- */

sprintf(delayBetweenGenerationsMsg,
Expand All @@ -71,6 +77,8 @@ int main() {

printf("> Delay received: %s milliseconds.\n\n", delayBetweenGenerations);

free(delayBetweenGenerations);

/* ---------------------------- Request Platform ---------------------------- */

platformSelected = getUserInputStr(
Expand All @@ -81,11 +89,15 @@ int main() {
printf("> Platform selected: '%s'.\n", platformSelected);

if (strcmpi(platformSelected, "console") == 0) {
free(platformSelected);
startGameByConsole(&game, maxGenerationInt, delayBetweenGenerationsInt);
destroy2DArray(game.dashboard, game.rows, game.cols);
return 0;
}

free(platformSelected);
startGameBySDL(&game, maxGenerationInt, delayBetweenGenerationsInt);
destroy2DArray(game.dashboard, game.rows, game.cols);

return 0;
}
4 changes: 4 additions & 0 deletions todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Falta

- Documentar necesidad de hacer free() luego de utilizar el retorno de la función `getUserInputStr`.
- Documentar nuevas funciones (marcadas como `TODO: Documentation`).

0 comments on commit f8bd1e6

Please sign in to comment.