diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 3643d59..912df75 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -20,7 +20,6 @@ int createWordSalad( unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { - srand(time(NULL)); enum Richtung { HORIZONTAL, VERTIKAL }; // wipe field @@ -33,7 +32,7 @@ int createWordSalad( salad[y][x] = EMPTY_CHAR; } } - int placedWords = 0; // variable platzierte Wörter + unsigned int placedWords = 0; // variable platzierte Wörter // place words // for schleife mit wortzahl = 0 bis wordCount @@ -59,18 +58,16 @@ int createWordSalad( int y = rand() % searchFieldLen; int fits = 1; - if (r == HORIZONTAL) { + if (r == VERTIKAL) { if ((y + wortLen) > (searchFieldLen)) { fits = 0; } - if (fits) { - for (int i = 0; i < wortLen; i++) { - char var = salad[y + i][x]; - if (var != EMPTY_CHAR) { - fits = 0; - } + for (int i = 0; (i < wortLen) && (fits != 0); i++) { + char var = salad[y + i][x]; + if (var != EMPTY_CHAR) { + fits = 0; } } @@ -80,26 +77,22 @@ int createWordSalad( salad[y + i][x] = words[wortNummer][i]; } - - platziert = 1; placedWords++; + platziert = 1; } } - else if (r == VERTIKAL) { + else if (r == HORIZONTAL) { if ((x + wortLen) > searchFieldLen) { fits = 0; } - if (fits) { - - for (int i = 0; i < wortLen; i++) { - char var = salad[y][x + i]; - if (var != EMPTY_CHAR) { - fits = 0; - } + for (int i = 0; (i < wortLen) && (fits != 0); i++) { + char var = salad[y][x + i]; + if (var != EMPTY_CHAR) { + fits = 0; } } @@ -109,9 +102,8 @@ int createWordSalad( salad[y][x + i] = words[wortNummer][i]; } - - platziert = 1; placedWords++; + platziert = 1; } } diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 0c687a2..8912dca 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -2,10 +2,6 @@ #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) { @@ -15,40 +11,34 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], return 0; } - // zunächst fehlerhaften String einlesen und in anderem Array - // zwischenspeichern - char line[MAX_LINE_LEN]; - char *token; - int wordCount = 0; + unsigned int wordCount = 0; - while (fgets(line, sizeof(line), file) != - NULL && // während mit fgets alle Zeichen aus dem file eingelesen - // werden - wordCount < maxWordCount) { + while ((fgets(line, sizeof(line), file) != NULL) && + (wordCount < maxWordCount)) { + + // Token initialisieren + char *token = strtok(line, " ,;.\n"); - token = strtok(line, " ;,.\n"); // Erstes Wort mit strtok aus dem - // fehlerhaften String herauslösen + while (token && (wordCount < maxWordCount)) { - while (token != NULL && - wordCount < maxWordCount) { // while strtok nicht am Ende ist und - // noch Wörter in words passen - - for (int i = 0; token[i] != '\0'; i++) { - token[i] = toupper((unsigned char)token[i]); + if (*token == '\0') { + token = strtok(NULL, " ,;.\n"); + continue; } - strncpy(words[wordCount], token, - sizeof(words[wordCount]) - - 1); // mit strcpy das aktuelle Wort in words kopieren - words[wordCount][sizeof(words[wordCount]) - 1] = - '\0'; // Nullterminator mit sizeof des aktuellen Worts - 1 an Ende des - // Worts setzen + // Token in Großbuchstaben konvertieren + for (int i = 0; token[i] != '\0'; i++) { + token[i] = toupper(token[i]); + } + + // In das words-Array kopieren + strcpy(words[wordCount], token); wordCount++; // Nächstes Wort - token = strtok(NULL, " ;,.\n"); // Nächstes Token + token = strtok(NULL, " ,;.\n"); // Nächstes Token } } return wordCount; // Anzahl der eingelesenen Wörter -} \ No newline at end of file +} diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 74024e1..d34f0e6 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -3,11 +3,13 @@ #include "input.h" #include #include +#include #define MAX_NUMBER_OF_WORDS 100 #define SALAD_SIZE 20 int main(int argc, char *argv[]) { + srand(time(NULL)); int exitCode = EXIT_SUCCESS; // Check if the correct number of arguments is provided @@ -15,8 +17,8 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Usage: %s \n", argv[0]); exitCode = EXIT_FAILURE; } else { - char words[MAX_NUMBER_OF_WORDS] - [MAX_WORD_LEN]; // Array to hold the words to be used in the game + char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN] = { + {0}}; // Array to hold the words to be used in the game unsigned int wordCount = 0; FILE *file = fopen(argv[1], "r"); @@ -37,11 +39,21 @@ int main(int argc, char *argv[]) { // Check if all words were successfully placed // Start the game if successful // error message if some words couldn't be placed + printf("Placed Words: %d\n", placedWords); + printf("Word Count:%d\n", wordCount); + int var = 0; + for (unsigned int i = 0; i < wordCount; i++) { + printf("Word %u: %s ", i, words[i]); + if (var == 10) { + printf("\n"); + var = 0; + } + } if (placedWords == wordCount) { - startGame(wordSalad, SALAD_SIZE, words, wordCount, - MAX_SEARCH_FIELD_LEN); + startGame(wordSalad, SALAD_SIZE, words, placedWords, 1024); + } else diff --git a/Start_Windows/makefile b/Start_Windows/makefile index 9d66779..71c2fab 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -15,16 +15,18 @@ wordsalad_initial: #--------------------------- # eigenes Target bauen #--------------------------- -wordsalad_myversion: main.o graphicalGame.o - $(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS) - +wordsalad_myversion: main.o graphicalGame.o input.o game.o + $(CC) main.o graphicalGame.o input.o game.o -o wordsalad_myversion $(BINARIES)/libraylib.a $(LDFLAGS) # -------------------------- # Normales Spiel bauen # -------------------------- -all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a +all: $(BINARIES)/libwordsalad.a main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) +$(BINARIES)/libwordsalad.a: main.o input.o game.o graphicalGame.o + ar rcs $(BINARIES)/libwordsalad.a main.o input.o game.o graphicalGame.o + main.o: main.c $(CC) -c $(CFLAGS) main.c @@ -49,4 +51,4 @@ test: input.o game.o unit_tests.c # Clean # -------------------------- clean: - rm -f *.o *.exe + rm -f *.o *.exe $(BINARIES)/libwordsalad.a diff --git a/Start_Windows/windows/libwordsalad.a b/Start_Windows/windows/libwordsalad.a deleted file mode 100644 index 06a447c..0000000 Binary files a/Start_Windows/windows/libwordsalad.a and /dev/null differ