diff --git a/src/main.c b/src/main.c index 28afcc3..d4a0f13 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "./sdl/main.h" #include "./structs.h" @@ -13,9 +14,9 @@ int main(int argc, char* argv[]) { TGame game; - int rows = DASHBOARD_ROWS; - int cols = DASHBOARD_COLS; - char** dashboard = new2DArray(rows, cols); + int rows; + int cols; + char** dashboard; char* requestedPattern; char* maxGeneration; @@ -29,15 +30,9 @@ int main(int argc, char* argv[]) { setDefaultMainArguments(&mainArguments); getMainArguments(&mainArguments, argc, argv); - printf("> dashboardRows --> %d\n", mainArguments.dashboardRows); - printf("> dashboardCols --> %d\n", mainArguments.dashboardCols); - printf("> pattern --> \"%s\"\n", mainArguments.pattern); - printf("> maximumGeneration --> %d\n", mainArguments.maximumGeneration); - printf("> delay --> %d\n", mainArguments.delay); - printf("> platform --> \"%s\"\n", mainArguments.platform); - - // free(mainArguments.pattern); - // free(mainArguments.platform); + rows = mainArguments.dashboardRows; + cols = mainArguments.dashboardCols; + dashboard = new2DArray(rows, cols); game.dashboard = dashboard; game.rows = rows; @@ -52,67 +47,108 @@ int main(int argc, char* argv[]) { /* ----------------------------- Request Pattern ---------------------------- */ - requestedPattern = getUserInputStr( - "> Which pattern do you want? ('Glider','Toad', 'Press', or 'Glider cannon'): ", - "> Invalid pattern! Try again...", 50, &validatePattern); + if (strcmp(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); - printf("> Pattern received: '%s'.\n\n", requestedPattern); + drawPattern(&game, requestedPattern); - drawPattern(&game, requestedPattern); + free(requestedPattern); + } else { + requestedPattern = mainArguments.pattern; - free(requestedPattern); + printf("> Pattern received: '%s'.\n\n", requestedPattern); + + drawPattern(&game, requestedPattern); + }; /* ----------------------- Request Maximum Generation ----------------------- */ - maxGeneration = getUserInputStr( - "> Which is maximum generation do you want? (a negative number is equal to infinity): ", - "> Invalid generation! Try again...", 10, &validateGeneration); + 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); + sscanf(maxGeneration, "%d", &maxGenerationInt); - if (maxGenerationInt < 0) { - free(maxGeneration); - maxGeneration = "infinity"; - maxGenerationInt = INT_MAX; - }; + if (maxGenerationInt < 0) { + free(maxGeneration); + maxGeneration = "infinity"; + maxGenerationInt = INT_MAX; + }; + + printf("> Maximum generation received: %s.\n\n", maxGeneration); - printf("> Maximum generation received: %s.\n\n", maxGeneration); + if (maxGenerationInt != INT_MAX) free(maxGeneration); + } else { + maxGenerationInt = mainArguments.maximumGeneration; - if (maxGenerationInt != INT_MAX) free(maxGeneration); + 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 ----------------------------- */ - sprintf(delayBetweenGenerationsMsg, + 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); + delayBetweenGenerations = getUserInputStr( + delayBetweenGenerationsMsg, "> Invalid delay! Try again...", 5, &validateDelay); + + sscanf(delayBetweenGenerations, "%d", &delayBetweenGenerationsInt); - sscanf(delayBetweenGenerations, "%d", &delayBetweenGenerationsInt); + printf("> Delay received: %s milliseconds.\n\n", delayBetweenGenerations); - printf("> Delay received: %s milliseconds.\n\n", delayBetweenGenerations); + free(delayBetweenGenerations); + } else { + delayBetweenGenerationsInt = mainArguments.delay; - free(delayBetweenGenerations); + printf("> Delay received: %d milliseconds.\n\n", delayBetweenGenerationsInt); + } /* ---------------------------- Request Platform ---------------------------- */ - 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); + if (strcmp(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); + 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; + } - if (strcmpi(platformSelected, "console") == 0) { free(platformSelected); - startGameByConsole(&game, maxGenerationInt, delayBetweenGenerationsInt); - destroy2DArray(game.dashboard, game.rows, game.cols); - return 0; + } 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; + } } - free(platformSelected); startGameBySDL(&game, maxGenerationInt, delayBetweenGenerationsInt); destroy2DArray(game.dashboard, game.rows, game.cols);