From 58b94eb41764a32af5d8f959b7c566022612b76f Mon Sep 17 00:00:00 2001 From: Jamin Deschner Date: Fri, 16 Oct 2026 10:54:41 +0200 Subject: [PATCH] =?UTF-8?q?Implementierung=20der=20Gamelogic=20in=20Main?= =?UTF-8?q?=20und=20Test=20f=C3=BCr=20Game=20im=20Makefile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Startcode/game.c | 100 +++++++++++++++++++++++++++++++++++++++++++++ Startcode/makefile | 6 +++ 2 files changed, 106 insertions(+) diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..8d9b30c 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -6,6 +6,8 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 +int randomNumberInRange(unsigned int range); + //TODO: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ @@ -13,5 +15,103 @@ // 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) { + //Rückgabewert ist die Anzahl an platzierten Wörtern + srand(time(NULL)); + int placedWordsSuccess = 0; + + //Array auf Spielfläche initialisieren mit leerem Char + for (int i = 0; i < searchFieldLen; i++) { + for (int u = 0; u < searchFieldLen; u++) { + salad[i][u] = EMPTY_CHAR; + } + } + + + for (int wordToBePlaced = 0; wordToBePlaced < wordCount; wordToBePlaced++) { + int triesPerWord = 1; + while (triesPerWord <= MAX_RAND_TRIES_PER_WORD) { + unsigned int xPosition = randomNumberInRange(searchFieldLen-1); + unsigned int yPosition = randomNumberInRange(searchFieldLen-1); + + if (salad[yPosition][xPosition] != EMPTY_CHAR) { + triesPerWord++; + continue; + } + + int putWordHorizontal = randomNumberInRange(1); + + //Horizontal einsetzen + if (putWordHorizontal) { + int writePossible = 1; + //Schleife um alle Stellen auf EMPTY_SPACE zu prüfen + for (int t = 0; t < MAX_WORD_LEN; t++) { + if (words[wordToBePlaced][t] == EMPTY_CHAR) break; + if (xPosition + t >= searchFieldLen) { + triesPerWord++; + writePossible = 0; + break; + } + if (salad[yPosition][xPosition + t] != EMPTY_CHAR) { + triesPerWord++; + writePossible = 0; + break; + } + } + // Nach Prüfung Wort einsetzen + if (writePossible) { + for (int t = 0; t < MAX_WORD_LEN; t++) { + if (words[wordToBePlaced][t] == EMPTY_CHAR) break; + salad[yPosition][xPosition + t] = words[wordToBePlaced][t]; + } + placedWordsSuccess++; + break; + } + } + + // Sonst vertikal einsetzen + else { + int writePossible = 1; + //Auf EMPTY_SPACE prüfen + for (int t = 0; t < MAX_WORD_LEN; t++) { + if (words[wordToBePlaced][t] == EMPTY_CHAR) break; + if (yPosition + t >= searchFieldLen) { + triesPerWord++; + writePossible = 0; + break; + } + if (salad[yPosition + t][xPosition] != EMPTY_CHAR) { + triesPerWord++; + writePossible = 0; + break; + } + } + //Einsetzen, wenn Felder frei + if (writePossible) { + for (int t = 0; t < MAX_WORD_LEN; t++) { + if (words[wordToBePlaced][t] == EMPTY_CHAR) break; + salad[yPosition + t][xPosition] = words[wordToBePlaced][t]; + } + placedWordsSuccess++; + break; + } + } + } + } + + //Restliche Felder mit Random-Buchstaben füllen + for (int zeile = 0; zeile < searchFieldLen; zeile++) { + for (int spalte = 0; spalte < searchFieldLen; spalte++) { + if (salad[zeile][spalte] == EMPTY_CHAR) { + salad[zeile][spalte] = (char)(randomNumberInRange(25)+65); //Buchstabe (ASCII) 65 - 90 + } + } + } + + return placedWordsSuccess; //Anzahl eingeschriebener Wörter zurückgeben } + +int randomNumberInRange(unsigned int range) { + int number = (rand() % (range+1)); + return number; +} \ No newline at end of file diff --git a/Startcode/makefile b/Startcode/makefile index 7cf1d4c..953f9bd 100644 --- a/Startcode/makefile +++ b/Startcode/makefile @@ -22,6 +22,12 @@ codefolder = . wordsalad_initial: $(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) +# -------------------------- +# NUR Game Test +# -------------------------- +wordsalad_mein_game: game.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad_mein_test game.o $(BINARIES)/libraylib.a $(LDFLAGS) $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) + # -------------------------- # Normales Spiel bauen # --------------------------