diff --git a/game.c b/game.c index 803301f..21ee85c 100644 --- a/game.c +++ b/game.c @@ -1,5 +1,4 @@ #include "game.h" -#include #include #include @@ -13,5 +12,77 @@ // 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) { + // 0. Grundlegende Validierung der Parameter (nur bei echten Fehlern -1) + if (salad == NULL || words == NULL || searchFieldLen == 0) { + return -1; + } + // 1. Spielfeld mit EMPTY_CHAR (0) initialisieren + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + salad[i][j] = EMPTY_CHAR; + } + } + + int placedWordsCount = 0; + + // 2. Jedes Wort der Liste einzeln im Spielfeld platzieren + for (unsigned int w = 0; w < wordCount; w++) { + const char *word = words[w]; + unsigned int len = strlen(word); + + if (len == 0 || len > searchFieldLen) { + continue; // Zu lange Wörter überspringen + } + + for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) { + int direction = rand() % 2; // 0 = Horizontal, 1 = Vertikal + + unsigned int startRow = 0; + unsigned int startCol = 0; + + if (direction == 0) { // Horizontal + startRow = rand() % searchFieldLen; + startCol = rand() % (searchFieldLen - len + 1); + } else { // Vertikal + startRow = rand() % (searchFieldLen - len + 1); + startCol = rand() % searchFieldLen; + } + + // Kollisionsprüfung + int collision = 0; + for (unsigned int i = 0; i < len; i++) { + unsigned int currRow = (direction == 0) ? startRow : (startRow + i); + unsigned int currCol = (direction == 0) ? (startCol + i) : startCol; + + char currentCell = salad[currRow][currCol]; + if (currentCell != EMPTY_CHAR && currentCell != word[i]) { + collision = 1; + break; + } + } + + // Wenn keine Kollision vorliegt, eintragen + if (!collision) { + for (unsigned int i = 0; i < len; i++) { + unsigned int currRow = (direction == 0) ? startRow : (startRow + i); + unsigned int currCol = (direction == 0) ? (startCol + i) : startCol; + salad[currRow][currCol] = word[i]; + } + placedWordsCount++; + break; + } + } + } + + // 3. Übrige leere Felder mit zufälligen Buchstaben füllen + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + if (salad[i][j] == EMPTY_CHAR) { + salad[i][j] = 'A' + (rand() % 26); + } + } + } + + return placedWordsCount; } diff --git a/input.c b/input.c index ed77805..15b0944 100644 --- a/input.c +++ b/input.c @@ -8,5 +8,28 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + const char *delim = ", ; \n\t"; + char buffer[MAX_WORD_LEN * MAX_WORD_LEN]; + int i = 0; // Wort-Zähler + // Prüfen, ob das Lesen erfolgreich war (EOF oder Fehler abfangen) + while (fgets(buffer, sizeof(buffer), file) != NULL && i #define MAX_NUMBER_OF_WORDS 100 #define SALAD_SIZE 20 @@ -10,6 +11,7 @@ int main(int argc, char *argv[]) { int exitCode = EXIT_SUCCESS; + srand(time(NULL)); // Check if the correct number of arguments is provided if(argc != 2) @@ -36,6 +38,15 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); + if (placedWords == wordCount) + { + startGame(wordSalad, SALAD_SIZE, words, wordCount, 100); + } else { + fprintf(stderr, "Es konnten nicht alle Wörter platziert werden :(\n"); + exitCode = EXIT_FAILURE; + } + + // TODO: // Check if all words were successfully placed // Start the game if successful diff --git a/makefile b/makefile index 285abb8..2dcff90 100644 --- a/makefile +++ b/makefile @@ -44,20 +44,17 @@ codefolder = . # Direkt einen bauen # ------------------------------- -wordsalad: main.o $(BINARIES)/libwordsalad.a graphicalGame.o $(BINARIES)/libraylib.a +wordsalad: main.o game.o input.o graphicalGame.o $(BINARIES)/libraylib.a $(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) -libwordsalad: game.o input.o - $(CC) $(CFLAGS) -o libwordsalad game.o input.o - main.o: $(codefolder)/main.c - $(CC) -c $(CFLAGS) $(codefolder)/main.c + $(CC) -c $(CFLAGS) $(codefolder)/main.c -o main.o input.o: $(codefolder)/input.c - $(CC) -c $(CFLAGS) $(codefolder)/input.c + $(CC) -c $(CFLAGS) $(codefolder)/input.c -o input.o game.o: $(codefolder)/game.c - $(CC) -c $(CFLAGS) $(codefolder)/game.c + $(CC) -c $(CFLAGS) $(codefolder)/game.c -o game.o graphicalGame.o: $(codefolder)/graphicalGame.c $(CC) -I$(raylibfolder) -c $(CFLAGS) $(codefolder)/graphicalGame.c diff --git a/words.txt b/words.txt index 31ee099..ddb8b1f 100644 --- a/words.txt +++ b/words.txt @@ -2,4 +2,4 @@ Yeti,Nessie Werwolf; Vampir Monster Hydra;Frankenstein Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist -Gespenst, Oger \ No newline at end of file +Gespenst, Oger, Alex, Lorenz, Claire \ No newline at end of file