diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..3f7f11e Binary files /dev/null and b/.DS_Store differ diff --git a/Start_Mac/.DS_Store b/Start_Mac/.DS_Store new file mode 100644 index 0000000..6ec78d3 Binary files /dev/null and b/Start_Mac/.DS_Store differ diff --git a/Start_Mac/game.c b/Start_Mac/game.c index d8cc133..130ea8c 100644 --- a/Start_Mac/game.c +++ b/Start_Mac/game.c @@ -3,21 +3,110 @@ #include #include +// Wie oft versucht wird, ein Wort zufällig zu platzieren #define MAX_RAND_TRIES_PER_WORD 10 +// Kennzeichen für leere Felder (wird später durch Buchstaben ersetzt) #define EMPTY_CHAR 0 -//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 -int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) -{ +// Richtungen für das Platzieren der Wörter +typedef enum { HORIZONTAL = 0, VERTICAL = 1 } Direction; +// Gibt einen zufälligen Großbuchstaben A–Z zurück +static char randomLetter(void) { + return 'A' + (rand() % 26); } -// Prints the word salad to console -void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) -{ +// Prüft, ob ein Wort an der gegebenen Position und Richtung ins Spielfeld passt +static int canPlace(const char field[][MAX_SEARCH_FIELD_LEN], unsigned int fieldSize, + const char *word, unsigned int row, unsigned int col, Direction dir) { + int length = strlen(word); + + if (dir == HORIZONTAL) { + if (col + length > fieldSize) return 0; // würde rechts rausgehen + for (int i = 0; i < length; i++) { + char current = field[row][col + i]; + if (current != EMPTY_CHAR && current != word[i]) + return 0; // Kollision mit anderem Wort + } + } else { // VERTICAL + if (row + length > fieldSize) return 0; // würde unten rausgehen + for (int i = 0; i < length; i++) { + char current = field[row + i][col]; + if (current != EMPTY_CHAR && current != word[i]) + return 0; + } + } + return 1; // Platz ist frei } + +// Schreibt ein Wort tatsächlich ins Spielfeld +static void placeWord(char field[][MAX_SEARCH_FIELD_LEN], const char *word, + unsigned int row, unsigned int col, Direction dir) { + + int length = strlen(word); + + if (dir == HORIZONTAL) { + for (int i = 0; i < length; i++) + field[row][col + i] = word[i]; + } else { + for (int i = 0; i < length; i++) + field[row + i][col] = word[i]; + } +} + +// Hauptfunktion: erstellt das Buchstabenfeld mit den versteckten Wörtern +int createWordSalad(char field[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + unsigned int fieldSize, const char words[][MAX_WORD_LEN], unsigned int wordCount) +{ + srand((unsigned)time(NULL)); // Zufallsstartwert setzen + + // 1) Feld mit "leer" initialisieren + for (unsigned int r = 0; r < fieldSize; r++) { + for (unsigned int c = 0; c < fieldSize; c++) { + field[r][c] = EMPTY_CHAR; + } + } + + // 2) Wörter zufällig platzieren + int placedWords = 0; + + for (unsigned int w = 0; w < wordCount; w++) { + const char *word = words[w]; + if (!word || word[0] == '\0') continue; // leere Zeilen überspringen + + int placed = 0; + for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) { + Direction dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL; + unsigned int row = rand() % fieldSize; + unsigned int col = rand() % fieldSize; + + if (canPlace(field, fieldSize, word, row, col, dir)) { + placeWord(field, word, row, col, dir); + placedWords++; + placed = 1; + } + } + } + + // 3) Leere Felder mit Zufallsbuchstaben füllen + for (unsigned int r = 0; r < fieldSize; r++) { + for (unsigned int c = 0; c < fieldSize; c++) { + if (field[r][c] == EMPTY_CHAR) + field[r][c] = randomLetter(); + } + } + + return placedWords; +} + +// Gibt das Spielfeld auf der Konsole aus +void showWordSalad(const char field[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int fieldSize) +{ + for (unsigned int r = 0; r < fieldSize; r++) { + for (unsigned int c = 0; c < fieldSize; c++) { + printf("%c ", field[r][c]); + } + printf("\n"); + } +} \ No newline at end of file diff --git a/Start_Mac/input.c b/Start_Mac/input.c index ed77805..38477ad 100644 --- a/Start_Mac/input.c +++ b/Start_Mac/input.c @@ -2,11 +2,38 @@ #include #include -// TODO: -// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. - -// Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + unsigned int count = 0; // Anzahl übernommener Wörter + int j = 0; // aktuelle Wortlänge + int ch; // eingelesenes Zeichen + char buf[MAX_WORD_LEN]; // Puffer für ein Wort + while ((ch = fgetc(file)) != EOF) { + unsigned char c = (unsigned char)ch; + if (isalnum(c)) { + // Teil eines Wortes → Uppercase, sicher begrenzt + if (j < (MAX_WORD_LEN - 1)) { + buf[j++] = (char)toupper(c); + } + } else { + // Trennzeichen: Wort abschließen, wenn vorhanden + if (j > 0) { + buf[j] = '\0'; + strncpy(words[count], buf, MAX_WORD_LEN); + count++; + j = 0; + if (count >= maxWordCount) break; + } + } + } + + // Letztes Wort am Dateiende übernehmen + if (j > 0 && count < maxWordCount) { + buf[j] = '\0'; + strncpy(words[count], buf, MAX_WORD_LEN); + count++; + } + + return (int)count; } \ No newline at end of file diff --git a/Start_Mac/main.c b/Start_Mac/main.c index 03da755..18ceedb 100644 --- a/Start_Mac/main.c +++ b/Start_Mac/main.c @@ -36,10 +36,28 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - // TODO: - // Check if all words were successfully placed - // Start the game if successful - // error message if some words couldn't be placed + if (placedWords == wordCount) { + printf("[OK] %u/%u Wörter platziert.\n", placedWords, wordCount); + showWordSalad(wordSalad, SALAD_SIZE); + + // Optional: Grafische Ausgabe starten, falls vorhanden. + // Aktivieren durch -DSTART_GRAPHICS beim Kompilieren + // und wenn in graphicalGame.h eine passende Funktion existiert. +#ifdef START_GRAPHICS + // Beispiel-Signatur – bitte ggf. an deine API anpassen: + // void startGraphicalGame(const char field[][MAX_SEARCH_FIELD_LEN], unsigned int size, + // const char words[][MAX_WORD_LEN], unsigned int wordCount); + startGraphicalGame((const char (*)[MAX_SEARCH_FIELD_LEN])wordSalad, + SALAD_SIZE, + (const char (*)[MAX_WORD_LEN])words, + wordCount); +#endif + } else { + fprintf(stderr, "[FEHLER] Nur %u von %u Wörtern konnten platziert werden.\n", + placedWords, wordCount); + fprintf(stderr, "Tipp: Größe des Suchfeldes (SALAD_SIZE) erhöhen oder kürzere Wortliste verwenden.\n"); + exitCode = EXIT_FAILURE; + } } else diff --git a/Start_Mac/makefile b/Start_Mac/makefile index 49654ae..226715b 100644 --- a/Start_Mac/makefile +++ b/Start_Mac/makefile @@ -1,5 +1,6 @@ CC = gcc CFLAGS = -g -Wall +CFLAGS += -DSTART_GRAPHICS LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -framework IOKit -framework Cocoa -framework CoreVideo ARCH := $(shell uname -m) BINARIES = ./macos-$(ARCH) @@ -29,7 +30,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 # -------------------------- diff --git a/Start_Mac/runTests b/Start_Mac/runTests new file mode 100755 index 0000000..465819e Binary files /dev/null and b/Start_Mac/runTests differ diff --git a/Start_Mac/wordsalad_initial b/Start_Mac/wordsalad_initial new file mode 100755 index 0000000..b1d7dec Binary files /dev/null and b/Start_Mac/wordsalad_initial differ diff --git a/Start_Windows/.DS_Store b/Start_Windows/.DS_Store new file mode 100644 index 0000000..b0cac30 Binary files /dev/null and b/Start_Windows/.DS_Store differ diff --git a/Start_Windows/words Kopie.txt b/Start_Windows/words Kopie.txt new file mode 100644 index 0000000..31ee099 --- /dev/null +++ b/Start_Windows/words Kopie.txt @@ -0,0 +1,5 @@ +Yeti,Nessie Werwolf; Vampir +Monster +Hydra;Frankenstein +Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist +Gespenst, Oger \ No newline at end of file