From 5e77016df35c6d345ffc78223dac77550a0e2d2d Mon Sep 17 00:00:00 2001 From: yannick Date: Thu, 30 Oct 2025 15:17:40 +0100 Subject: [PATCH] test --- Start_Windows/game.c | 73 ++++++++++++++++++++++++++++++++++++++++-- Start_Windows/makefile | 7 ++-- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 88c5402..2fb6f79 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -6,7 +6,6 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 -//abcdefghijklmnopqrstuvwxyz //TODO: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren @@ -15,11 +14,81 @@ // 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((unsigned int)time(NULL)); + // Spielfeld leeren + for (unsigned int i = 0; i < searchFieldLen; i++) + for (unsigned int j = 0; j < searchFieldLen; j++) + salad[i][j] = EMPTY_CHAR; + + int placedCount = 0; + + for (unsigned int w = 0; w < wordCount; w++) { + const char *word = words[w]; + unsigned int len = strlen(word); + int placed = 0; + + if (len > searchFieldLen) + continue; // Wort passt niemals ins Feld + + for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD * searchFieldLen && !placed; tries++) { + int horizontal = rand() % 2; + + unsigned int row = rand() % searchFieldLen; + unsigned int col = rand() % searchFieldLen; + + // Stelle sicher, dass Wort im Spielfeld bleibt + if (horizontal) { + if (col + len > searchFieldLen) continue; + } else { + if (row + len > searchFieldLen) continue; + } + + // Prüfen, ob Platz frei ist oder gleiche Buchstaben überlappen + int fits = 1; + for (unsigned int i = 0; i < len; i++) { + char c = horizontal ? salad[row][col + i] : salad[row + i][col]; + if (c != EMPTY_CHAR && c != word[i]) { + fits = 0; + break; + } + } + + if (!fits) continue; + + // Wort einsetzen + for (unsigned int i = 0; i < len; i++) { + if (horizontal) + salad[row][col + i] = word[i]; + else + salad[row + i][col] = word[i]; + } + + placed = 1; + placedCount++; + } + + if (!placed) { + fprintf(stderr, "WARNUNG: Wort \"%s\" konnte nicht platziert werden.\n", word); + } + } + + // Leere Felder auffü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 placedCount; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + printf("%c ", salad[i][j]); + } + printf("\n"); + } } diff --git a/Start_Windows/makefile b/Start_Windows/makefile index 72cccb7..274c9b0 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -1,9 +1,9 @@ CC = gcc -CFLAGS = -g -Wall +CFLAGS = -g -Wall -I$(raylib_folder) LDFLAGS = -lopengl32 -lgdi32 -lwinmm BINARIES = ./windows -raylibfolder = ./raylib +raylib_folder = ./raylib unityfolder = ./unity # -------------------------- @@ -12,6 +12,7 @@ unityfolder = ./unity wordsalad_initial: $(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) + # -------------------------- # Normales Spiel bauen # -------------------------- @@ -28,7 +29,7 @@ game.o: game.c $(CC) -c $(CFLAGS) game.c graphicalGame.o: graphicalGame.c - $(CC) -I$(raylibfolder) -c $(CFLAGS) graphicalGame.c + $(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c # -------------------------- # Unit Tests