Skip to content

Commit 45810ab

Browse files
hozlucas28GuidolinarestefhuergoTiagoGiannotti
committed
feature: create functions to start the game by console
Co-authored-by: Guidolinares <[email protected]> Co-authored-by: tefhuergo <[email protected]> Co-authored-by: Tiago Giannotti <[email protected]>
1 parent 73d26f1 commit 45810ab

File tree

3 files changed

+133
-17
lines changed

3 files changed

+133
-17
lines changed

libs/utilities.c

+70-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
#include <stdio.h>
66
#include <stdlib.h>
77
#include <string.h>
8+
#include <time.h>
89

910
#include "./patterns/main.h"
1011

11-
int countNeighbors(TGame* pGame, int cellRow, int cellCol, int radius) {
12+
int countAliveNeighbors(TGame* pGame, int cellRow, int cellCol, int radius) {
1213
int i;
1314
int j;
1415

@@ -18,7 +19,7 @@ int countNeighbors(TGame* pGame, int cellRow, int cellCol, int radius) {
1819
int endRow = cellRow + radius + 1;
1920
int endCol = cellCol + radius + 1;
2021

21-
int neighbors = 0;
22+
int aliveNeighbors = 0;
2223

2324
for (i = startRow; i < endRow; i++) {
2425
if (i > pGame->rows - 1) break;
@@ -29,11 +30,13 @@ int countNeighbors(TGame* pGame, int cellRow, int cellCol, int radius) {
2930
if (j < 0) continue;
3031

3132
if (i == cellRow && j == cellCol) continue;
32-
if (pGame->dashboard[i][j] == ALIVE_CELL) neighbors++;
33+
34+
if (pGame->dashboard[i][j] == ALIVE_CELL || pGame->dashboard[i][j] == DEAD_CELL_NG)
35+
aliveNeighbors++;
3336
}
3437
}
3538

36-
return neighbors;
39+
return aliveNeighbors;
3740
}
3841

3942
void drawPattern(TGame* pGame, char* pattern) {
@@ -160,6 +163,69 @@ void setDashboardCenter(TGame* pGame) {
160163
pGame->center[1] = col;
161164
}
162165

166+
void sleep(int miliseconds) {
167+
clock_t startTime = clock();
168+
while (clock() < (startTime + miliseconds));
169+
}
170+
171+
void startGameByConsole(TGame* pGame, int maxGeneration, int delayBetweenGenerations) {
172+
int i;
173+
int j;
174+
175+
int generation = 0;
176+
int aliveNeighbors;
177+
178+
pGame->generation = 0;
179+
180+
system("cls");
181+
printDashboardByConsole(pGame);
182+
if (generation == maxGeneration) return;
183+
sleep(delayBetweenGenerations);
184+
185+
while (generation < maxGeneration) {
186+
for (i = 0; i < pGame->rows; i++) {
187+
for (j = 0; j < pGame->cols; j++) {
188+
aliveNeighbors = countAliveNeighbors(pGame, i, j, NEIGHBORHOOD_RADIUS);
189+
190+
if (pGame->dashboard[i][j] == DEAD_CELL) {
191+
if (aliveNeighbors == 3) {
192+
pGame->cellsDead--;
193+
pGame->cellsAlive++;
194+
pGame->dashboard[i][j] = ALIVE_CELL_NG;
195+
};
196+
197+
continue;
198+
}
199+
200+
if (aliveNeighbors < 2 || aliveNeighbors > 3) {
201+
pGame->cellsAlive--;
202+
pGame->cellsDead++;
203+
pGame->dashboard[i][j] = DEAD_CELL_NG;
204+
};
205+
}
206+
}
207+
208+
for (i = 0; i < pGame->rows; i++) {
209+
for (j = 0; j < pGame->cols; j++) {
210+
if (pGame->dashboard[i][j] == DEAD_CELL_NG) {
211+
pGame->dashboard[i][j] = DEAD_CELL;
212+
continue;
213+
}
214+
215+
if (pGame->dashboard[i][j] == ALIVE_CELL_NG) pGame->dashboard[i][j] = ALIVE_CELL;
216+
}
217+
}
218+
219+
generation++;
220+
221+
pGame->generation = generation;
222+
223+
system("cls");
224+
printDashboardByConsole(pGame);
225+
if (generation != maxGeneration) sleep(delayBetweenGenerations);
226+
}
227+
}
228+
163229
int strcmpi(const char* str01, const char* str02) {
164230
int i;
165231

libs/utilities.h

+35-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,23 @@ typedef struct {
2626
int generation; /** Represents the generation number. */
2727
} TGame;
2828

29-
// TODO: Documentation
30-
int countNeighbors(TGame* pGame, int row, int col, int radius);
29+
/**
30+
* @brief Counts the number of alive neighbors around a given position inside the `dashboard` field
31+
* of a Conway's Game of Life structure.
32+
*
33+
* This function iterates through the cells within a specified radius around the given cell
34+
* (excluding the cell itself) and counts how many of those cells are alive.
35+
*
36+
* @param pGame Pointer to the Conway's Game of Life structure.
37+
* @param row Row index of the cell.
38+
* @param col Column index of the cell.
39+
* @param radius Radius around the cell to check for alive neighbors.
40+
*
41+
* @return The number of alive neighbors around the specified cell.
42+
*
43+
* @warning This function assumes that `pGame` has been properly initialized.
44+
*/
45+
int countAliveNeighbors(TGame* pGame, int row, int col, int radius);
3146

3247
/**
3348
* @brief Draws a specified pattern on a Conway's Game of Life board.
@@ -113,6 +128,24 @@ void printDashboardByConsole(TGame* pGame);
113128
*/
114129
void setDashboardCenter(TGame* pGame);
115130

131+
/**
132+
* @brief Pauses the execution of the program.
133+
*
134+
* @warning The actual delay may be longer than specified due to system timer.
135+
*/
136+
void sleep(int milliseconds);
137+
138+
/**
139+
* @brief Starts a Conway's Game of Life game using the console as the output.
140+
*
141+
* @param pGame Pointer to a Conway's Game of Life structure.
142+
* @param maxGeneration Maximum number of generations.
143+
* @param delayBetweenGenerations Delay in milliseconds between each generation.
144+
*
145+
* @warning This function assumes that `pGame` has been properly initialized.
146+
*/
147+
void startGameByConsole(TGame* pGame, int maxGeneration, int delayBetweenGenerations);
148+
116149
/**
117150
* @brief Compares two strings case-insensitively.
118151
*

src/main.c

+28-11
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ int main() {
1111
int cols = DASHBOARD_COLS;
1212

1313
char* requestedPattern;
14-
char* maxGenerations;
14+
char* maxGeneration;
1515
char* delayBetweenGenerations;
1616
char delayBetweenGenerationsMsg[120];
17+
char* platformSelected;
1718

18-
int maxGenerationsInt;
19+
int maxGenerationInt;
1920
int delayBetweenGenerationsInt;
2021

2122
game.dashboard = dashboard;
@@ -32,7 +33,7 @@ int main() {
3233
/* ----------------------------- Request Pattern ---------------------------- */
3334

3435
requestedPattern = getUserInputStr(
35-
"> Which pattern do you want ('Glider','Toad', 'Press', or 'Glider cannon')? ",
36+
"> Which pattern do you want? ('Glider','Toad', 'Press', or 'Glider cannon'): ",
3637
"> Invalid pattern! Try again...", 50, &validatePattern);
3738

3839
printf("> Pattern received: '%s'.\n\n", requestedPattern);
@@ -41,23 +42,23 @@ int main() {
4142

4243
/* ----------------------- Request Maximum Generation ----------------------- */
4344

44-
maxGenerations = getUserInputStr(
45+
maxGeneration = getUserInputStr(
4546
"> Which is maximum generation do you want? (a negative number is equal to infinity): ",
4647
"> Invalid generation! Try again...", 10, &validateGeneration);
4748

48-
sscanf(maxGenerations, "%d", &maxGenerationsInt);
49+
sscanf(maxGeneration, "%d", &maxGenerationInt);
4950

50-
if (maxGenerationsInt < 0) {
51-
maxGenerations = "'infinity'";
52-
maxGenerationsInt = INT_MAX;
51+
if (maxGenerationInt < 0) {
52+
maxGeneration = "'infinity'";
53+
maxGenerationInt = INT_MAX;
5354
};
5455

55-
printf("> Maximum generation received: %s.\n\n", maxGenerations);
56+
printf("> Maximum generation received: %s.\n\n", maxGeneration);
5657

5758
/* ------------------------------ Request Delay ----------------------------- */
5859

5960
sprintf(delayBetweenGenerationsMsg,
60-
"> What should be the miliseconds delay between generations? (must be between %d and "
61+
"> What should be the milliseconds delay between generations? (must be between %d and "
6162
"%d, both included): ",
6263
MINIMUM_DELAY, MAXIMUM_DELAY);
6364

@@ -66,7 +67,23 @@ int main() {
6667

6768
sscanf(delayBetweenGenerations, "%d", &delayBetweenGenerationsInt);
6869

69-
printf("> Delay received: %s miliseconds.\n", delayBetweenGenerations);
70+
printf("> Delay received: %s milliseconds.\n\n", delayBetweenGenerations);
71+
72+
/* ---------------------------- Request Platform ---------------------------- */
73+
74+
platformSelected = getUserInputStr(
75+
"> In which platform do you want to start the Conway's Game of Life game? (console, or "
76+
"Simple DirectMedia Layer (SDL)): ",
77+
"> Invalid option! Try again...", 32, &validatePlatform);
78+
79+
printf("> Platform selected: '%s'.\n", platformSelected);
80+
81+
if (strcmpi(platformSelected, "console") == 0) {
82+
startGameByConsole(&game, maxGenerationInt, delayBetweenGenerationsInt);
83+
return 0;
84+
}
85+
86+
// TODO: Start game in SDL.
7087

7188
return 0;
7289
}

0 commit comments

Comments
 (0)