From a803b2d87911a9168f4d420ee2ef41beb0f34fae Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Sun, 5 Feb 2023 03:13:57 +0100 Subject: [PATCH] added function for user to accept instructions --- src/c/main.c | 90 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/src/c/main.c b/src/c/main.c index 9448ed1..b0df59e 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -1,27 +1,89 @@ -//BIBs +// BIBs #include #include #include +#include bool gameRunning; +bool acceptedRules; + +// content +char *gameInstructionsFile = "../../src/content/game_instructions.txt"; + +// function declarations +void printInit(); int main() { - //define variables - char userInput[20]; //maximum defined user input length - gameRunning = 1; + // define variables + char userInput[20]; // maximum defined user input length + gameRunning = 1; + + // init and instructions + printInit(); - while (gameRunning == 1) //while running + if (acceptedRules == 1) { - // User Input - printf("User Input:"); - scanf(" %s", userInput); - printf("\n"); - - //NEXT STEP: - //Processing - //processInput(userInput); - } + while (gameRunning == 1) // while running + { + // User Input + printf("User Input:"); + scanf(" %s", userInput); + printf("\n"); + // NEXT STEP: + // Processing + // processInput(userInput); + } + } return 0; +}; + +// init dialogue +void printInit() +{ + FILE *stream; + char *line = NULL; + size_t len = 0; + ssize_t read; + + stream = fopen(gameInstructionsFile, "r"); + if (stream == NULL) + { + printf("ERROR: couldn't open or find file: INSTRUCTIONS !\n"); + exit(EXIT_FAILURE); // exit + } + + /* print line by line from file */ + while ((read = getline(&line, &len, stream)) != -1) + { + // printf("Retrieved line of length %u :\n", read); + printf("%s", line); + } + + free(line); /* Deallocate allocated memory */ + fclose(stream); /* closing file */ + + char userInput[1]; + bool inputState = 0; + int acceptCounter = 0; + while (inputState == 0) + { + scanf(" %c", userInput); + if (strcasecmp(userInput, "y") == 0) + { + acceptedRules = 1; + inputState = 1; //break while + } + else if (strcasecmp(userInput, "n") == 0) + { + printf("You didn't read our rules & instructions. The game will close now. \n\n"); + acceptedRules = 0; + inputState = 1; //break while + } + else + { + printf("Invalid Input!\n"); + } + } }; \ No newline at end of file