3
3
#include <limits.h>
4
4
#include <stdio.h>
5
5
#include <stdlib.h>
6
+ #include <strings.h>
6
7
7
8
#include "./sdl/main.h"
8
9
#include "./structs.h"
@@ -13,9 +14,9 @@ int main(int argc, char* argv[]) {
13
14
14
15
TGame game ;
15
16
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 ;
19
20
20
21
char * requestedPattern ;
21
22
char * maxGeneration ;
@@ -29,15 +30,9 @@ int main(int argc, char* argv[]) {
29
30
setDefaultMainArguments (& mainArguments );
30
31
getMainArguments (& mainArguments , argc , argv );
31
32
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 );
41
36
42
37
game .dashboard = dashboard ;
43
38
game .rows = rows ;
@@ -52,67 +47,108 @@ int main(int argc, char* argv[]) {
52
47
53
48
/* ----------------------------- Request Pattern ---------------------------- */
54
49
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 );
58
56
59
- printf ( "> Pattern received: '%s'.\n\n" , requestedPattern );
57
+ drawPattern ( & game , requestedPattern );
60
58
61
- drawPattern (& game , requestedPattern );
59
+ free (requestedPattern );
60
+ } else {
61
+ requestedPattern = mainArguments .pattern ;
62
62
63
- free (requestedPattern );
63
+ printf ("> Pattern received: '%s'.\n\n" , requestedPattern );
64
+
65
+ drawPattern (& game , requestedPattern );
66
+ };
64
67
65
68
/* ----------------------- Request Maximum Generation ----------------------- */
66
69
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 );
70
74
71
- sscanf (maxGeneration , "%d" , & maxGenerationInt );
75
+ sscanf (maxGeneration , "%d" , & maxGenerationInt );
72
76
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 );
78
84
79
- printf ("> Maximum generation received: %s.\n\n" , maxGeneration );
85
+ if (maxGenerationInt != INT_MAX ) free (maxGeneration );
86
+ } else {
87
+ maxGenerationInt = mainArguments .maximumGeneration ;
80
88
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
+ };
82
98
83
99
/* ------------------------------ Request Delay ----------------------------- */
84
100
85
- sprintf (delayBetweenGenerationsMsg ,
101
+ if (mainArguments .delay == 0 ) {
102
+ sprintf (
103
+ delayBetweenGenerationsMsg ,
86
104
"> What should be the milliseconds delay between generations? (must be between %d and "
87
105
"%d, both included): " ,
88
106
MINIMUM_DELAY , MAXIMUM_DELAY );
89
107
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 );
92
112
93
- sscanf ( delayBetweenGenerations , "%d " , & delayBetweenGenerationsInt );
113
+ printf ( "> Delay received: %s milliseconds.\n\n " , delayBetweenGenerations );
94
114
95
- printf ("> Delay received: %s milliseconds.\n\n" , delayBetweenGenerations );
115
+ free (delayBetweenGenerations );
116
+ } else {
117
+ delayBetweenGenerationsInt = mainArguments .delay ;
96
118
97
- free (delayBetweenGenerations );
119
+ printf ("> Delay received: %d milliseconds.\n\n" , delayBetweenGenerationsInt );
120
+ }
98
121
99
122
/* ---------------------------- Request Platform ---------------------------- */
100
123
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 );
105
129
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
+ }
107
138
108
- if (strcmpi (platformSelected , "console" ) == 0 ) {
109
139
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
+ }
113
150
}
114
151
115
- free (platformSelected );
116
152
startGameBySDL (& game , maxGenerationInt , delayBetweenGenerationsInt );
117
153
destroy2DArray (game .dashboard , game .rows , game .cols );
118
154
0 commit comments