Skip to content

Commit

Permalink
feature: apply main arguments
Browse files Browse the repository at this point in the history
Co-authored-by: Ferney Santiago Quiroga <[email protected]>
Co-authored-by: Guidolinares <[email protected]>
Co-authored-by: Tiago Giannotti <[email protected]>
  • Loading branch information
4 people committed Oct 11, 2024
1 parent 02beb16 commit fbb22bf
Showing 1 changed file with 81 additions and 45 deletions.
126 changes: 81 additions & 45 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#include "./sdl/main.h"
#include "./structs.h"
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit fbb22bf

Please sign in to comment.