diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 3e05c0c..8f80747 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -15,9 +15,9 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { srand(time(NULL)); - int placed_words = 0; + int placedWords = 0; - // Array mit 0 füllen + // Array mit . füllen for (int r = 0; r < searchFieldLen; r++){ for(int s = 0; s < searchFieldLen; s++){ salad[r][s] = '.'; @@ -29,65 +29,69 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi // Wörter in salad einsortieren - for (int i = 0; i < wordCount; i++){ + for (int num_words = 0; num_words < wordCount; num_words++){ - int laenge = strlen(words[i]); + int laenge = strlen(words[num_words]); // muss noch prüfen, ob in der Zeile schon was ist // 1 ist horizontal, 0 ist vertikal - // frei == 1 -> Zeile nicht frei // isalpha == 0 -> kein Buchstabe for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){ int richtung = rand()% 2; - int frei = 0; + int belegt = 0; // horizontale Eingabe if (richtung == 1){ int zeile = rand()% searchFieldLen; - int spalte = rand()% (searchFieldLen- laenge + 1); + int spalte = rand()% (searchFieldLen- laenge +1); //prüft, ob die herausgesuchte Zeile noch frei ist + // belegt == 1 -> Zeile nicht frei for (int o = spalte; o < spalte + laenge; o++){ if(salad[zeile][o] != '.'){ - frei = 1; + belegt = 1; break; } } // setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl - if (frei == 0){ + if (belegt == 0){ for(int k = 0; k < laenge; k++){ - salad[zeile][spalte + k] = words[i][k]; + salad[zeile][spalte + k] = words[num_words][k]; } - placed_words++; + placedWords++; break; } } // vertikale Eingabe else if (richtung == 0){ - int zeile = rand()% (searchFieldLen - laenge + 1); + int zeile = rand()% (searchFieldLen - laenge +1); int spalte = rand()% searchFieldLen; //prüft, ob die herausgesuchte Zeile noch frei ist for (int n = zeile; n < zeile + laenge; n++){ if(salad[n][spalte] != '.'){ - frei = 1; + belegt = 1; break; } } // setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl - if (frei == 0){ + if (belegt == 0){ for(int j = 0; j < laenge; j++){ - salad[zeile + j][spalte] = words[i][j]; + salad[zeile + j][spalte] = words[num_words][j]; } - placed_words++; + placedWords++; break; } } + else{ + break; + } } + } // fügt zufällige Buchstaben ein @@ -95,7 +99,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi for(int m = 0; m < searchFieldLen; m++ ){ if(isalpha(salad[l][m]) == 0 ){ // zufällige Buchstaben erzeugen - char alphabet [] = "abcdefghijklmnopqrstuvwxyz"; + char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int laenge = strlen(alphabet); int zufallszahl = rand()% laenge; char zufallsbuchstabe = alphabet[zufallszahl]; @@ -105,7 +109,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi } } - return placed_words; + return placedWords; } // Prints the word salad to console diff --git a/Start_Windows/graphicalGame.o b/Start_Windows/graphicalGame.o deleted file mode 100644 index 8998c9a..0000000 Binary files a/Start_Windows/graphicalGame.o and /dev/null differ diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 6dbbd97..32872f0 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -2,6 +2,55 @@ #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) +{ + // MAX_WORD_LEN 100 + // MAX_LINE_LEN 1024 + // *file ist Datei + // words ist Array + // MAX_WORD_LEN ist maximale Wortlänge + // maxWordCount ist maximale Anzahl an Wörtern + char puffer[MAX_LINE_LEN]; + int counter = 0; + + while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount) + { + char *parts = strtok(puffer, ",;\n\t/. "); + + while(parts != NULL && counter < maxWordCount) + { + //strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1); + //words[counter][MAX_WORD_LEN-1] = "\0"; + //Großbuchstaben + for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++) + { + words[counter][i] = toupper(parts[i]); + words[counter][i] = '\0'; + } + } + counter++; // Wort eingelesen -> Wortzähler erhöhen + parts = strtok(NULL, ",;\n\t/. "); + + } + return counter; +} + + + + + + + + + +/* #include "input.h" +#include +#include + #define MAX_NUMBER_OF_WORDS 100 #define MAX_WORD_LEN 100 // TODO: diff --git a/Start_Windows/main.o b/Start_Windows/main.o deleted file mode 100644 index dad84e3..0000000 Binary files a/Start_Windows/main.o and /dev/null differ diff --git a/Start_Windows/makefile b/Start_Windows/makefile index e0558be..5c539c9 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -35,7 +35,7 @@ graphicalGame.o: graphicalGame.c # -------------------------- TEST_BIN = runTests -test: input.o game.o unit_tests.c +test: input.o game.o unit_tests.c $(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a # --------------------------