diff --git a/.gitignore b/.gitignore index 0fbd721..bb8944e 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,9 @@ *.hex Startcode/wordsalad_initial Startcode/wordsalad +Startcode/wordsalad_mymain +Startcode/wordsalad_myinput +Startcode/runTests test # Debug files diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..f5510a6 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -2,11 +2,49 @@ #include #include -// TODO: -// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. +/* +Read a single word from file and store it in buffer. +Returns the length of the word, or 0 if no more words are available (EOF). +*/ +static int readSingleWord(FILE *file, char buffer[MAX_WORD_LEN]) +{ + int len = 0; + int c; + + //clear leading seperators + while((c = fgetc(file)) != EOF && (c == ',' || c == ';' || c == ' ' || c == '\n')) {} + + //check for empty file + if(c == EOF) + return 0; + + + //read word + do + { + if(len < MAX_WORD_LEN - 1) // -1 to leave space for '\0' + { + buffer[len] = toupper((unsigned char)c); + len++; + } + c = fgetc(file); + } + while(c != EOF && !(c == ',' || c == ';' || c == ' ' || c == '\n')); + + //append string end-delimiter + buffer[len] = '\0'; // '\0' marks the end of a string, we need to apend it for strcpy + return len; +} // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + char buffer[MAX_WORD_LEN]; + unsigned int wordCount = 0; + int len; + while(wordCount < maxWordCount && (len = readSingleWord(file, buffer)) > 0) + strcpy(words[wordCount++], buffer); + + return wordCount; } \ No newline at end of file diff --git a/Startcode/main.c b/Startcode/main.c index 03da755..3cfef84 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -35,11 +35,16 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - - // TODO: - // Check if all words were successfully placed - // Start the game if successful - // error message if some words couldn't be placed + + if (wordCount == placedWords) + { + startGame(wordSalad, SALAD_SIZE, words, wordCount, 1000); + } + else + { + fprintf(stderr, "Not all words could be placed in the grid.\nOnly %d of %d could be placed", placedWords, wordCount); + exitCode = EXIT_FAILURE; + } } else diff --git a/Startcode/makefile b/Startcode/makefile index 7cf1d4c..e9fc22c 100644 --- a/Startcode/makefile +++ b/Startcode/makefile @@ -22,6 +22,16 @@ codefolder = . wordsalad_initial: $(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) +# -------------------------- +# Zwischenstände: +# -------------------------- +# Only main +wordsalad_mymain: main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad_mymain main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS) +# Main + Input +wordsalad_myinput: main.o input.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad_myinput main.o input.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS) + # -------------------------- # Normales Spiel bauen # -------------------------- @@ -55,5 +65,5 @@ clean: ifeq ($(OS),Windows_NT) del /f *.o *.exe else - rm -f *.o wordsalad $(TEST_BIN) + rm -f *.o wordsalad wordsalad_mymain wordsalad_myinput $(TEST_BIN) endif \ No newline at end of file