From 2e24fdcec313ddcb9a0cfbd7703540a1a9a970db Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 26 Oct 2026 12:32:21 +0100 Subject: [PATCH] Test --- .gitignore | 3 +- Startcode/game.c | 108 +++++++++++++++++++++++++++++++++++++++++++++ Startcode/game.h | 1 + Startcode/input.c | 37 ++++++++++++++++ Startcode/main.c | 13 ++++++ Startcode/makefile | 3 +- 6 files changed, 163 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 0fbd721..e645cf8 100644 --- a/.gitignore +++ b/.gitignore @@ -35,9 +35,10 @@ *.i*86 *.x86_64 *.hex +Startcode/wordsalad_myversion Startcode/wordsalad_initial Startcode/wordsalad -test +runtest # Debug files *.dSYM/ diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..4bf47fc 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -6,12 +6,120 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 +typedef enum{ + HORIZONTAL, + VERTICAL +} Richtung; + //TODO: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ // Creates the word salad by placing words randomly and filling empty spaces +// Buchstabengitter Koordinaten Seitenlänge die Wörter, der Buchstabe Anzahl der Wörter int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { + + for(int i = 0; i < searchFieldLen; i++) + { + for(int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = EMPTY_CHAR; + } + } + + int placeWords = 0; + + for(int i = 0; i < wordCount; i++) + { + int Spalte; + int Zeile; + int WORD_LEN = strlen(words[i]); //Buchstaben zählen + int platziert = 0; + + for(int try = 0; try < MAX_RAND_TRIES_PER_WORD; try++) // Max 10 Versuche pro Wort + { + Richtung dir = rand() % 2; + int passt = 1; + + if(dir == HORIZONTAL) + { + + Spalte = rand() % searchFieldLen; + Zeile = rand() % searchFieldLen; + + if(Spalte + WORD_LEN <= searchFieldLen) // Prüft auf Border + { + + for(int k = 0; k < WORD_LEN; k++) + { + if(salad[Zeile][Spalte+k] != EMPTY_CHAR) // Koalisionsprüfung + { + passt = 0; //Kollision gefunden + break; + } + } + if(passt == 1) + { + for(int s = 0; s < WORD_LEN; s++) + { + salad[Zeile][Spalte + s] = words[i][s]; // Das Wort eintragen in einer Zeile + } + platziert = 1; + break; // Das Wort kann eingetragen werden, keine Kollision + } + } + } + + + else // Vertikal + { + + Spalte = rand() % searchFieldLen; + Zeile = rand() % searchFieldLen; + if(Zeile + WORD_LEN <= searchFieldLen) + { + int passt = 1; + for(int k = 0; k < WORD_LEN; k++) + { + if(salad[Zeile+k][Spalte] != EMPTY_CHAR) + { + passt = 0; //Kollision gefunden + break; + } + } + if(passt == 1) + { + for(int z = 0; z < WORD_LEN; z++) + { + salad[Zeile + z][Spalte] = words[i][z]; // // Das Wort eintragen in einer Spalte + } + platziert = 1; + break; // Das Wort kann eingetragen werden, keine Kollision + } + } + } + } + if(platziert == 1) + { + placeWords++; + } + } + + + for(int i = 0; i < searchFieldLen; i++) + { + for(int j = 0; j < searchFieldLen; j++) + { + if(salad[i][j] == EMPTY_CHAR) // Nur wenn Feld noch frei ist, dann mit random Buchstaben belegen + { + salad[i][j] = 'A' + (rand() % 26); + } + + } + } + + return placeWords; } + diff --git a/Startcode/game.h b/Startcode/game.h index cc66572..7bdeb34 100644 --- a/Startcode/game.h +++ b/Startcode/game.h @@ -7,4 +7,5 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount); + #endif diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..7674aeb 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -4,9 +4,46 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. +int leseEinWort(FILE *file, char zielWort[]) +{ + int c; + int i = 0; + while((c = fgetc(file)) != EOF) // liest aus Textdatei und überprüft ob End Of File ist + { + + if(isalpha(c) != 0) + { + if(i < MAX_WORD_LEN - 1) // -1 wegen '\0' + { + zielWort[i] = (char)toupper(c); // Für Großbuchstaben + i++; + } + } + else if(i > 0) + { + zielWort[i] = '\0'; + return 1; + } + + } + if(i > 0) // Datei endet direkt nach ein WOrt ohne folgendes Leerzeichen oder so + { + zielWort[i] = '\0'; + return 1; + } + return 0; +} // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + + int anzahl = 0; + while((leseEinWort(file, words[anzahl])) == 1 && anzahl < maxWordCount) + { + anzahl++; + } + return anzahl; + } \ No newline at end of file diff --git a/Startcode/main.c b/Startcode/main.c index 03da755..ee9b849 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -3,12 +3,16 @@ #include "input.h" #include "game.h" #include "graphicalGame.h" +#include #define MAX_NUMBER_OF_WORDS 100 #define SALAD_SIZE 20 + int main(int argc, char *argv[]) { + srand(time(NULL)); + int exitCode = EXIT_SUCCESS; // Check if the correct number of arguments is provided @@ -40,6 +44,15 @@ int main(int argc, char *argv[]) // Check if all words were successfully placed // Start the game if successful // error message if some words couldn't be placed + if(placedWords == wordCount) // Wenn alle Wörter reinpassten, rufe das grafische Fenster auf + { + startGame(wordSalad, SALAD_SIZE, words, wordCount, 800); + } + else + { + fprintf(stderr, "Could not place all words\n"); + exitCode = EXIT_FAILURE; + } } else diff --git a/Startcode/makefile b/Startcode/makefile index 7cf1d4c..71bdc7b 100644 --- a/Startcode/makefile +++ b/Startcode/makefile @@ -21,7 +21,8 @@ codefolder = . # -------------------------- wordsalad_initial: $(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) - +wordsalad_myversion: main.o game.o graphicalGame.o + $(CC) $(CFLAGS) -o wordsalad_myversion main.o game.o graphicalGame.o -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS) # -------------------------- # Normales Spiel bauen # --------------------------