Skip to content

Commit 084950e

Browse files
hozlucas28TiagoGiannottiGuidolinares
committed
feature: add new function and complete todos
Co-authored-by: Tiago Giannotti <[email protected]> Co-authored-by: Guidolinares <[email protected]>
1 parent 5774885 commit 084950e

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

libs/utilities.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,35 @@ void drawPattern(TGame* pGame, char* pattern) {
3636
pGame->cellsDead = (pGame->cols * pGame->rows) - pGame->cellsAlive;
3737
pGame->generation = 0;
3838

39-
printf("\n\n"); // TODO: Remove line.
40-
printPatternByConsole(&SPattern); // TODO: Remove line.
41-
42-
// TODO
4339
drawPatternInDashboard(pGame, &SPattern);
44-
45-
printf("\n* The '%s' pattern was injected! *", pattern); // TODO: Remove line.
46-
printf("\n* The '%s' pattern was drawn! *", pattern); // TODO: Remove line.
4740
}
4841

49-
// TODO
50-
void drawPatternInDashboard(TGame* pGame, TPattern* pattern) {}
42+
void drawPatternInDashboard(TGame* pGame, TPattern* pPattern) {
43+
int i;
44+
int j;
45+
46+
int pI = 0;
47+
int pJ = 0;
48+
49+
int startRow = pGame->center[0] - pPattern->center[0];
50+
int startCol = pGame->center[1] - pPattern->center[1];
51+
52+
for (i = startRow; pI < pPattern->rows; i++) {
53+
if (i < 0) continue;
54+
if (i > pGame->rows - 1) break;
55+
56+
for (j = startCol; pJ < pPattern->cols; j++) {
57+
if (j < 0) continue;
58+
if (j > pGame->cols - 1) break;
59+
60+
pGame->dashboard[i][j] = pPattern->arr[pI][pJ];
61+
pJ++;
62+
};
63+
64+
pJ = 0;
65+
pI++;
66+
}
67+
}
5168

5269
void fillDashboard(TGame* pGame, char with) {
5370
int i;
@@ -107,6 +124,14 @@ void printDashboardByConsole(TGame* pGame) {
107124
}
108125
}
109126

127+
void setDashboardCenter(TGame* pGame) {
128+
int row = pGame->rows / 2;
129+
int col = pGame->cols / 2;
130+
131+
pGame->center[0] = row;
132+
pGame->center[1] = col;
133+
}
134+
110135
int strcmpi(const char* str01, const char* str02) {
111136
int i;
112137

libs/utilities.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ typedef struct {
2020
char (*dashboard)[DASHBOARD_COLS]; /** Board (2D array) in which the cells moves. */
2121
int rows; /** Number of rows in `dashboard`. */
2222
int cols; /** Number of columns in `dashboard`. */
23-
int cellsAlive; /** Number of alive cells. */
24-
int cellsDead; /** Number of dead cells. */
25-
int generation; /** Represents the generation number. */
23+
int center[2]; /** Array (row, and column) representing the center of the `dashboard`. */
24+
int cellsAlive; /** Number of alive cells. */
25+
int cellsDead; /** Number of dead cells. */
26+
int generation; /** Represents the generation number. */
2627
} TGame;
2728

2829
/**
@@ -39,12 +40,12 @@ void drawPattern(TGame* pGame, char* pattern);
3940
* @brief Draws a specified pattern on a Conway's Game of Life board.
4041
*
4142
* @param pGame Pointer to the Conway's Game of Life structure where the pattern will be drawn.
42-
* @param pattern Pointer to pattern structure to be drawn.
43+
* @param pPattern Pointer to pattern structure to be drawn.
4344
*
4445
* @warning This functions is intended for internal use only and should not be used outside of this
4546
* library.
4647
*/
47-
void drawPatternInDashboard(TGame* pGame, TPattern* pattern);
48+
void drawPatternInDashboard(TGame* pGame, TPattern* pPattern);
4849

4950
/**
5051
* @brief Fills the dashboard of a Conway's Game of Life structure with a
@@ -99,6 +100,16 @@ int isStrIn(char* str, char* arr[], int arrLength);
99100
*/
100101
void printDashboardByConsole(TGame* pGame);
101102

103+
/**
104+
* @brief Sets the center of a Conway's Game of Life structure.
105+
*
106+
* @param pGame A pointer to the Conway's Game of Life structure.
107+
*
108+
* @warning This function assumes that `pGame` has been properly initialized with valid `rows` and
109+
* `cols` field values.
110+
*/
111+
void setDashboardCenter(TGame* pGame);
112+
102113
/**
103114
* @brief Compares two strings case-insensitively.
104115
*

0 commit comments

Comments
 (0)