diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..65541fd 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -1,12 +1,53 @@ #include "input.h" #include #include +#include // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // Read words from file and store in 'words' array -int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) -{ +static int createSingleWords(char text[MAX_LINE_LEN], char words[][MAX_WORD_LEN], unsigned int maxWordCount); +unsigned int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) +{ + int wordCount = 0; + char lineRead[MAX_LINE_LEN]; + + while(fgets(lineRead, MAX_LINE_LEN, file) != NULL && wordCount < (int)maxWordCount) + { + wordCount += createSingleWords(lineRead, words + wordCount, maxWordCount - wordCount); + } + + return wordCount; //wordCount +} + +static int createSingleWords(char text[MAX_LINE_LEN], char words[][MAX_WORD_LEN], unsigned int maxWordCount) +{ + char *teiler = " ;,.\n"; + char *token; + + int i = 0; + + token = strtok(text, teiler); + + while(token != NULL && i < maxWordCount) + { + strncpy(words[i], token, MAX_WORD_LEN - 1); + //kleinbuchstaben --> GROßBUCHSTABEN + int length = strlen(words[i]); + for(int j = 0; j < length; j++) + { + if(islower(words[i][j])) + { + words[i][j] = toupper(words[i][j]); + } + } + words[i][MAX_WORD_LEN - 1] = '\0'; + + token = strtok(NULL, teiler); + i++; + } + + return i; } \ No newline at end of file diff --git a/Startcode/main.c b/Startcode/main.c index 03da755..fbf9ad6 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -41,6 +41,23 @@ int main(int argc, char *argv[]) // Start the game if successful // error message if some words couldn't be placed + if(wordCount > 19) + { + if(placedWords != 20) + { + exitCode = EXIT_FAILURE; + printf("Only %d of %d possible words could be placed\n", placedWords, 20); + } + } + else + { + if(placedWords != wordCount) + { + exitCode = EXIT_FAILURE; + printf("Only %d of %d possible words could be placed\n", placedWords, wordCount); + } + } + } else { diff --git a/Startcode/makefile b/Startcode/makefile index 7cf1d4c..125e2e1 100644 --- a/Startcode/makefile +++ b/Startcode/makefile @@ -22,6 +22,15 @@ codefolder = . wordsalad_initial: $(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) +# -------------------------- +# Zwischenstände +# -------------------------- +wordsalad_mymain: main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad_mymain main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) + +wordsalad_myinput: input.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad_myinput input.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) + # -------------------------- # Normales Spiel bauen # --------------------------