diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..d98ba94 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -6,18 +6,107 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 -//TODO: Spiellogik implementieren: +// TODO: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren - * restliche Felder mit zufälligen Buchstaben füllen */ + * restliche Felder mit zufälligen Buchstaben füllen */ // Creates the word salad by placing words randomly and filling empty spaces 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)); + const char buchstaben[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + int placedWords = 0; + + for (int i = 0; i < wordCount; i++) + { + + int vertikal = rand() % 2; + + size_t länge = strlen(words[i]); + int positionX; + int positionY; + int tries = 0; + int belegt; + + while (tries <= MAX_RAND_TRIES_PER_WORD) + { + if (vertikal) + { + positionX = rand() % (searchFieldLen); + positionY = rand() % (searchFieldLen - länge); + + for (int y = positionY; y < (länge + positionY); y++) + { + if (salad[y][positionX] != '0') + { + tries++; + belegt = 1; + break; + } + } + + if(belegt){ + continue; + belegt = 0; + } + + + for (int y = positionY, j = 0; y < (länge + positionY); y++, j++) + { + salad[y][positionX] = words[i][j]; + } + placedWords++; + break; // Wort wurde hinzugefügt, while Schleife wird verlassen + } + else + { + positionX = rand() % (searchFieldLen - länge); + positionY = rand() % (searchFieldLen); + + for (int x = positionX; x < (länge + positionX); x++) + { + if (salad[positionY][x] != '0') + { + tries++; + break; + } + } + + if(belegt){ + continue; + belegt = 0; + } + + for (int x = positionX, j = 0; x < (länge + positionX); x++, j++) + { + salad[positionY][x] = words[i][j]; + } + placedWords++; + break; // Wort wurde hinzugefügt, while Schleife wird verlassen + } + } + } + + for(int i = 0; i< searchFieldLen;i++){ + for(int j = 0; j< searchFieldLen;j++){ + if(salad[i][j] == '0'){ + salad[i][j] = buchstaben[rand() % 26]; + } + } + } + + return placedWords; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + for(int i = 0; i< searchFieldLen;i++){ + for(int j = 0; j< searchFieldLen;j++){ + printf("%c",salad[i][j]); + } + printf("\n"); + } } diff --git a/Start_Windows/game.o b/Start_Windows/game.o new file mode 100644 index 0000000..1c87a6b Binary files /dev/null and b/Start_Windows/game.o differ diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..90cb0cb 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -8,5 +8,39 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + unsigned int count = 0; + char line[MAX_LINE_LEN]; + char *teiler = " ,;\n\t"; + char *token; + + while(fgets(line, sizeof(line), file)){ + + //Yeti,Nessie Werwolf; Vampir + // token = Yeti + token = strtok(line, teiler); + + while (token != NULL) + { + if (strlen(token) >= MAX_WORD_LEN) { + printf("Fehler: eigelesenes Wort ist zu lang\n"); + return count; + } + if (count >= maxWordCount){ + printf("Fehler: zu viele Wörter\n"); + return count; + } + + strncpy(words[count], token, MAX_WORD_LEN-1); + words[count][MAX_WORD_LEN-1] = '\0'; + + //Nessie Werwolf; Vampir + //token= Nessie + token = strtok(NULL, teiler); + + count++; + } + } + + return count; } \ No newline at end of file diff --git a/Start_Windows/input.o b/Start_Windows/input.o new file mode 100644 index 0000000..4cde895 Binary files /dev/null and b/Start_Windows/input.o differ diff --git a/Start_Windows/main.c b/Start_Windows/main.c index ff73e74..3ea455a 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) int exitCode = EXIT_SUCCESS; // Check if the correct number of arguments is provided - if(argc != 2) + if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exitCode = EXIT_FAILURE; @@ -24,11 +24,19 @@ int main(int argc, char *argv[]) FILE *file = fopen(argv[1], "r"); - if(file != NULL) + if (file != NULL) { unsigned int placedWords = 0; char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad + for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++) + { + for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) + { + wordSalad[i][j] = '0'; + } + } + // Read words from file and store in 'words' array wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); fclose(file); @@ -38,15 +46,17 @@ int main(int argc, char *argv[]) // TODO: // Check if all words were successfully placed - if (placedWords == wordCount) { + if (placedWords == wordCount) + { // Start the game if successful showWordSalad(wordSalad, SALAD_SIZE); - } else { + } + else + { // error message if some words couldn't be placed printf("Some words could not be placed"); exitCode = EXIT_FAILURE; } - } else { diff --git a/Start_Windows/main.o b/Start_Windows/main.o index fdb28b1..cdcc4fc 100644 Binary files a/Start_Windows/main.o and b/Start_Windows/main.o differ diff --git a/Start_Windows/makefile b/Start_Windows/makefile index 3ec2437..7f31fc3 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -24,7 +24,7 @@ wordsalad_my: main.o graphicalGame.o $(BINARIES)/libraylib.a # -------------------------- # Normales Spiel bauen # -------------------------- -all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a +wordsalad: 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) main.o: main.c diff --git a/Start_Windows/wordsalad.exe b/Start_Windows/wordsalad.exe new file mode 100644 index 0000000..65feb88 Binary files /dev/null and b/Start_Windows/wordsalad.exe differ