-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
170 lines (124 loc) · 5.42 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "../libs/main.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "./sdl/main.h"
#include "./structs.h"
#include "./utilities.h"
int main(const int argc, char* argv[]) {
TMainArguments mainArguments;
TGame game;
int rows;
int cols;
char** dashboard;
char* requestedPattern;
char* maxGeneration;
char* delayBetweenGenerations;
char delayBetweenGenerationsMsg[120];
char* platformSelected;
int maxGenerationInt;
int delayBetweenGenerationsInt;
setDefaultMainArguments(&mainArguments);
getMainArguments(&mainArguments, argc, argv);
rows = mainArguments.dashboardRows;
cols = mainArguments.dashboardCols;
/* ----------------------------- Request Pattern ---------------------------- */
if (*mainArguments.initialStateFile == '\0') {
dashboard = new2DArray(rows, cols);
game.dashboard = dashboard;
game.rows = rows;
game.cols = cols;
game.cellsAlive = 0;
game.cellsDead = cols * rows;
game.generation = 0;
setDashboardCenter(&game);
fillDashboard(&game, DEAD_CELL);
if (*mainArguments.pattern == '\0') {
requestedPattern = getUserInputStr(
"> Which pattern do you want? ('Glider','Toad', 'Press', or 'Glider cannon'): ",
"> Invalid pattern! Try again...", 50, &validatePattern);
printf("> Pattern received: '%s'.\n\n", requestedPattern);
drawPattern(&game, requestedPattern);
free(requestedPattern);
} else {
requestedPattern = mainArguments.pattern;
printf("> Pattern received: '%s'.\n\n", requestedPattern);
drawPattern(&game, requestedPattern);
};
} else {
/* --------------------------- Draw Initial State --------------------------- */
if (!setDashboardFromFile(mainArguments.initialStateFile, &game, rows, cols)) {
printf(
"> An error occurred on set the initial state of the dashboard! Please check the "
"file path with the initial state (received %s) and it's content.\n",
mainArguments.initialStateFile);
exit(EXIT_FAILURE);
}
}
/* ----------------------- Request Maximum Generation ----------------------- */
if (mainArguments.maximumGeneration == 0) {
maxGeneration = getUserInputStr(
"> Which is maximum generation do you want? (a negative number is equal to infinity): ",
"> Invalid generation! Try again...", 10, &validateGeneration);
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);
} else {
maxGenerationInt = mainArguments.maximumGeneration;
if (maxGenerationInt < 0) {
maxGeneration = "infinity";
maxGenerationInt = INT_MAX;
printf("> Maximum generation received: %s.\n\n", maxGeneration);
} else {
printf("> Maximum generation received: %d.\n\n", maxGenerationInt);
}
};
/* ------------------------------ Request Delay ----------------------------- */
if (mainArguments.delay == 0) {
sprintf(
delayBetweenGenerationsMsg,
"> What should be the milliseconds delay between generations? (must be between %d and "
"%d, both included): ",
MINIMUM_DELAY, MAXIMUM_DELAY);
delayBetweenGenerations = getUserInputStr(
delayBetweenGenerationsMsg, "> Invalid delay! Try again...", 5, &validateDelay);
sscanf(delayBetweenGenerations, "%d", &delayBetweenGenerationsInt);
printf("> Delay received: %s milliseconds.\n\n", delayBetweenGenerations);
free(delayBetweenGenerations);
} else {
delayBetweenGenerationsInt = mainArguments.delay;
printf("> Delay received: %d milliseconds.\n\n", delayBetweenGenerationsInt);
}
/* ---------------------------- Request Platform ---------------------------- */
if (*mainArguments.platform == '\0') {
platformSelected = getUserInputStr(
"> In which platform do you want to start the Conway's Game of Life game? (console, or "
"Simple DirectMedia Layer (SDL)): ",
"> Invalid option! Try again...", 32, &validatePlatform);
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);
} else {
platformSelected = mainArguments.platform;
printf("> Platform selected: '%s'.\n", platformSelected);
if (strcmpi(platformSelected, "console") == 0) {
startGameByConsole(&game, maxGenerationInt, delayBetweenGenerationsInt);
destroy2DArray(game.dashboard, game.rows, game.cols);
return 0;
}
}
startGameBySDL(&game, maxGenerationInt, delayBetweenGenerationsInt);
destroy2DArray(game.dashboard, game.rows, game.cols);
return 0;
}