Skip to content

Commit fbb22bf

Browse files
hozlucas28Ferny1011GuidolinaresTiagoGiannotti
committed
feature: apply main arguments
Co-authored-by: Ferney Santiago Quiroga <[email protected]> Co-authored-by: Guidolinares <[email protected]> Co-authored-by: Tiago Giannotti <[email protected]>
1 parent 02beb16 commit fbb22bf

File tree

1 file changed

+81
-45
lines changed

1 file changed

+81
-45
lines changed

src/main.c

Lines changed: 81 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <limits.h>
44
#include <stdio.h>
55
#include <stdlib.h>
6+
#include <strings.h>
67

78
#include "./sdl/main.h"
89
#include "./structs.h"
@@ -13,9 +14,9 @@ int main(int argc, char* argv[]) {
1314

1415
TGame game;
1516

16-
int rows = DASHBOARD_ROWS;
17-
int cols = DASHBOARD_COLS;
18-
char** dashboard = new2DArray(rows, cols);
17+
int rows;
18+
int cols;
19+
char** dashboard;
1920

2021
char* requestedPattern;
2122
char* maxGeneration;
@@ -29,15 +30,9 @@ int main(int argc, char* argv[]) {
2930
setDefaultMainArguments(&mainArguments);
3031
getMainArguments(&mainArguments, argc, argv);
3132

32-
printf("> dashboardRows --> %d\n", mainArguments.dashboardRows);
33-
printf("> dashboardCols --> %d\n", mainArguments.dashboardCols);
34-
printf("> pattern --> \"%s\"\n", mainArguments.pattern);
35-
printf("> maximumGeneration --> %d\n", mainArguments.maximumGeneration);
36-
printf("> delay --> %d\n", mainArguments.delay);
37-
printf("> platform --> \"%s\"\n", mainArguments.platform);
38-
39-
// free(mainArguments.pattern);
40-
// free(mainArguments.platform);
33+
rows = mainArguments.dashboardRows;
34+
cols = mainArguments.dashboardCols;
35+
dashboard = new2DArray(rows, cols);
4136

4237
game.dashboard = dashboard;
4338
game.rows = rows;
@@ -52,67 +47,108 @@ int main(int argc, char* argv[]) {
5247

5348
/* ----------------------------- Request Pattern ---------------------------- */
5449

55-
requestedPattern = getUserInputStr(
56-
"> Which pattern do you want? ('Glider','Toad', 'Press', or 'Glider cannon'): ",
57-
"> Invalid pattern! Try again...", 50, &validatePattern);
50+
if (strcmp(mainArguments.pattern, "") == 0) {
51+
requestedPattern = getUserInputStr(
52+
"> Which pattern do you want? ('Glider','Toad', 'Press', or 'Glider cannon'): ",
53+
"> Invalid pattern! Try again...", 50, &validatePattern);
54+
55+
printf("> Pattern received: '%s'.\n\n", requestedPattern);
5856

59-
printf("> Pattern received: '%s'.\n\n", requestedPattern);
57+
drawPattern(&game, requestedPattern);
6058

61-
drawPattern(&game, requestedPattern);
59+
free(requestedPattern);
60+
} else {
61+
requestedPattern = mainArguments.pattern;
6262

63-
free(requestedPattern);
63+
printf("> Pattern received: '%s'.\n\n", requestedPattern);
64+
65+
drawPattern(&game, requestedPattern);
66+
};
6467

6568
/* ----------------------- Request Maximum Generation ----------------------- */
6669

67-
maxGeneration = getUserInputStr(
68-
"> Which is maximum generation do you want? (a negative number is equal to infinity): ",
69-
"> Invalid generation! Try again...", 10, &validateGeneration);
70+
if (mainArguments.maximumGeneration == 0) {
71+
maxGeneration = getUserInputStr(
72+
"> Which is maximum generation do you want? (a negative number is equal to infinity): ",
73+
"> Invalid generation! Try again...", 10, &validateGeneration);
7074

71-
sscanf(maxGeneration, "%d", &maxGenerationInt);
75+
sscanf(maxGeneration, "%d", &maxGenerationInt);
7276

73-
if (maxGenerationInt < 0) {
74-
free(maxGeneration);
75-
maxGeneration = "infinity";
76-
maxGenerationInt = INT_MAX;
77-
};
77+
if (maxGenerationInt < 0) {
78+
free(maxGeneration);
79+
maxGeneration = "infinity";
80+
maxGenerationInt = INT_MAX;
81+
};
82+
83+
printf("> Maximum generation received: %s.\n\n", maxGeneration);
7884

79-
printf("> Maximum generation received: %s.\n\n", maxGeneration);
85+
if (maxGenerationInt != INT_MAX) free(maxGeneration);
86+
} else {
87+
maxGenerationInt = mainArguments.maximumGeneration;
8088

81-
if (maxGenerationInt != INT_MAX) free(maxGeneration);
89+
if (maxGenerationInt < 0) {
90+
maxGeneration = "infinity";
91+
maxGenerationInt = INT_MAX;
92+
93+
printf("> Maximum generation received: %s.\n\n", maxGeneration);
94+
} else {
95+
printf("> Maximum generation received: %d.\n\n", maxGenerationInt);
96+
}
97+
};
8298

8399
/* ------------------------------ Request Delay ----------------------------- */
84100

85-
sprintf(delayBetweenGenerationsMsg,
101+
if (mainArguments.delay == 0) {
102+
sprintf(
103+
delayBetweenGenerationsMsg,
86104
"> What should be the milliseconds delay between generations? (must be between %d and "
87105
"%d, both included): ",
88106
MINIMUM_DELAY, MAXIMUM_DELAY);
89107

90-
delayBetweenGenerations = getUserInputStr(delayBetweenGenerationsMsg,
91-
"> Invalid delay! Try again...", 5, &validateDelay);
108+
delayBetweenGenerations = getUserInputStr(
109+
delayBetweenGenerationsMsg, "> Invalid delay! Try again...", 5, &validateDelay);
110+
111+
sscanf(delayBetweenGenerations, "%d", &delayBetweenGenerationsInt);
92112

93-
sscanf(delayBetweenGenerations, "%d", &delayBetweenGenerationsInt);
113+
printf("> Delay received: %s milliseconds.\n\n", delayBetweenGenerations);
94114

95-
printf("> Delay received: %s milliseconds.\n\n", delayBetweenGenerations);
115+
free(delayBetweenGenerations);
116+
} else {
117+
delayBetweenGenerationsInt = mainArguments.delay;
96118

97-
free(delayBetweenGenerations);
119+
printf("> Delay received: %d milliseconds.\n\n", delayBetweenGenerationsInt);
120+
}
98121

99122
/* ---------------------------- Request Platform ---------------------------- */
100123

101-
platformSelected = getUserInputStr(
102-
"> In which platform do you want to start the Conway's Game of Life game? (console, or "
103-
"Simple DirectMedia Layer (SDL)): ",
104-
"> Invalid option! Try again...", 32, &validatePlatform);
124+
if (strcmp(mainArguments.platform, "") == 0) {
125+
platformSelected = getUserInputStr(
126+
"> In which platform do you want to start the Conway's Game of Life game? (console, or "
127+
"Simple DirectMedia Layer (SDL)): ",
128+
"> Invalid option! Try again...", 32, &validatePlatform);
105129

106-
printf("> Platform selected: '%s'.\n", platformSelected);
130+
printf("> Platform selected: '%s'.\n", platformSelected);
131+
132+
if (strcmpi(platformSelected, "console") == 0) {
133+
free(platformSelected);
134+
startGameByConsole(&game, maxGenerationInt, delayBetweenGenerationsInt);
135+
destroy2DArray(game.dashboard, game.rows, game.cols);
136+
return 0;
137+
}
107138

108-
if (strcmpi(platformSelected, "console") == 0) {
109139
free(platformSelected);
110-
startGameByConsole(&game, maxGenerationInt, delayBetweenGenerationsInt);
111-
destroy2DArray(game.dashboard, game.rows, game.cols);
112-
return 0;
140+
} else {
141+
platformSelected = mainArguments.platform;
142+
143+
printf("> Platform selected: '%s'.\n", platformSelected);
144+
145+
if (strcmpi(platformSelected, "console") == 0) {
146+
startGameByConsole(&game, maxGenerationInt, delayBetweenGenerationsInt);
147+
destroy2DArray(game.dashboard, game.rows, game.cols);
148+
return 0;
149+
}
113150
}
114151

115-
free(platformSelected);
116152
startGameBySDL(&game, maxGenerationInt, delayBetweenGenerationsInt);
117153
destroy2DArray(game.dashboard, game.rows, game.cols);
118154

0 commit comments

Comments
 (0)