From a5e963f2a6885fd1df60cba1fdb245d7c5f46821 Mon Sep 17 00:00:00 2001 From: "Chris.F" Date: Sat, 17 Oct 2026 15:26:06 +0200 Subject: [PATCH] game.c implementiert --- Startcode/game.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++ Startcode/input.c | 37 ++++++++---------- Startcode/makefile | 3 ++ 3 files changed, 112 insertions(+), 21 deletions(-) diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..86e06fc 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -13,5 +13,98 @@ // 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)); + int x = 0; + int y = 0; + int placedWords = 0; + int i = 0; + int nichtPlatziert =0; + typedef enum {vertikal, horizontal} Platzierungsrichtung; + + for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen + { + for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) + { + salad[i][j] = '0'; + } + } + + while(placedWords < wordCount) + { + x = rand() % 20 +1; + y = rand() % 20 +1; + + Platzierungsrichtung zufaellige_richtung = rand() % 2; + + if(zufaellige_richtung == 0 && salad[y][x] == '0') + { + + if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-y)) // -1, da Nullzeichen sowieso weg muss + { + for(int j = 0; j < sizeof(words[i]-1); j++) + { + if(salad[(y+j)][x] == '0') + { + strncpy(&salad[(y+j)][x], &words[i][j], 1); + } + else if(&salad[(y+j)][x] == &words[i][j]) + { + + } + else + { + nichtPlatziert = 1; + } + } + if(nichtPlatziert == 0) + { + placedWords++; + i++; + } + } + + } + else if( zufaellige_richtung == 1 && salad[y][x] == '0') + { + if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-x)) // -1, da Nullzeichen sowieso weg muss + { + for(int j = 0; j < sizeof(words[i]-1); j++) + { + if(salad[y][(x+j)] == '0') + { + strncpy(&salad[y][(x+j)], &words[i][j], 1); + + } + else if(&salad[y][(x+j)] == &words[i][j]) + { + + } + else + { + nichtPlatziert = 1; + } + } + if(nichtPlatziert == 0) + { + placedWords++; + i++; + } + } + + } + } + + for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen + { + for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) + { + if(salad[i][j] == '0') + { + salad[i][j] = rand() % (90-65+1)+ 65; + } + } + } + + return placedWords; } diff --git a/Startcode/input.c b/Startcode/input.c index e15db33..cf9a133 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -8,36 +8,31 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - char puffer[MAX_LINE_NUMBER][MAX_LINE_LEN]; + char puffer[MAX_LINE_LEN]; char *teiler = "\n ,;"; char *token; - int i = 0; unsigned int wordCount = 0; - while(fgets(puffer[i], MAX_LINE_LEN, file) != NULL) + while(fgets(puffer, MAX_LINE_LEN, file) != NULL) //Liest die zeilenweise Wörter aus der file in einen Zwischenpuffer bis das Dateiende erreicht ist returnwert der file == NULL { - i++; - } + token = strtok(puffer, teiler); // Holt sich per Teilerbedingung das erste Wort aus dem puffer - i = 0; - - token = strtok(puffer[i], teiler); - - while(token != NULL && wordCount < maxWordCount) - { - strncpy(words[wordCount], token, MAX_WORD_LEN -1); - //words[wordCount][MAX_WORD_LEN - 1] = '\0'; - - wordCount++; - - token = strtok(NULL, teiler); - - if(token == NULL && i < MAX_LINE_NUMBER) + while(token != NULL && wordCount < maxWordCount) { - token = strtok(puffer[i+1], teiler); - } + for(int i = 0; i < MAX_WORD_LEN; i++) //Verwandelt jeden Buchstaben in einen Großbuchstaben + { + token[i] = toupper(token[i]); + } + strncpy(words[wordCount], token, MAX_WORD_LEN -1); //speichert einzelnes Wort in das gegeben word array ab und holt sich das nächste im Puffer, bis array leer token == NULL + //words[wordCount][MAX_WORD_LEN - 1] = '\0'; + + wordCount++; + + token = strtok(NULL, teiler); + } } + return wordCount; } \ No newline at end of file diff --git a/Startcode/makefile b/Startcode/makefile index d1d6985..7452cda 100644 --- a/Startcode/makefile +++ b/Startcode/makefile @@ -29,6 +29,9 @@ wordsalad_initial: wordsalad_myversion: input.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(CC) $(CFLAGS) -o wordsalad_myversion input.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) +wordsalad_final: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad_final main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) + 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)