From 629f5ed15eb01075b8f9fe4a85a9ab0a25cb8787 Mon Sep 17 00:00:00 2001 From: Anton Lewedei Date: Tue, 4 Nov 2025 16:43:46 +0100 Subject: [PATCH] fertog --- Start_Windows/game.c | 208 +++++++++++++++++++++++++++++++++++++++--- Start_Windows/game.h | 1 + Start_Windows/input.c | 26 ++++++ 3 files changed, 220 insertions(+), 15 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 6e67461..5ae188b 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -2,29 +2,32 @@ #include #include #include -#include -#include "game.h" #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR '.' -//TODO: Spiellogik implementieren: -/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren - * restliche Felder mit zufälligen Buchstaben füllen */ -static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], - unsigned int SearchFieldLen) //Array initialisieren & mit Filler füllen +// TODO: Spiellogik implementieren: +/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren + * restliche Felder mit zufälligen Buchstaben füllen */ + + + +static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Array initialisieren & mit Filler füllen { - for (int i = 0; i < SearchFieldLen; i++) + + for (int i = 0; i < searchFieldLen; i++) + { + + for (int j = 0; j < searchFieldLen; j++) { - for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) - { salad[i][j] = EMPTY_CHAR; } } } -static void col_roq (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Wortanfang + +static void col_row (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Anfang von Wort { *x = rand() % searchFieldLen; @@ -32,14 +35,189 @@ static void col_roq (unsigned int searchFieldLen, int *x, int *y) //Zufällige x } -// 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) +static int place(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int searchFieldLen, int len [],const char words [] [MAX_WORD_LEN], unsigned int wordCount) //Prüfen ob plazierbar und plazieren { + int x, y; + int placedWords = 0; //Counter plazierte Wörter + + + for(int i = 0; i < wordCount; i++) + { + int placed = 0; + + + for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) + { + len[i] = (int)strlen(words[i]); + + col_row(searchFieldLen, &x, &y); + + int orient = rand() % 2; + + + if(orient == 0) //wir prüfen und füllen horizontal (in x-Richtung) + { + int works = 1; + + if(((x + len[i]) > searchFieldLen)) + { + + works = 0; + + } + + + else + { + + for(int j = x; j < (x + len[i]); j++) + { + + if(salad[y][j] != EMPTY_CHAR) + { + + works = 0; + break; + + } + } + } + + // ab hier platzieren wir + + if(works) + { + + for (int k = 0; k < len[i]; k++) + { + + salad[y][x + k] = words[i][k]; + + } + + placed = 1; + placedWords++; + + } + } + + + else //wir prüfen und füllen vertikal (in y-Richtung) + { + int works = 1; + + if(((y + len[i]) > searchFieldLen)) + { + + works = 0; + + } + + else + { + + for(int j = y; j < (y + len[i]); j++) + { + + if(salad[j][x] != EMPTY_CHAR) + { + + works = 0; + break; + + } + } + } + + // ab hier platzieren wir + + if(works) + { + + for (int k = 0; k < len[i]; k++) + { + + salad[y + k][x] = words[i][k]; + + } + + placed = 1; + placedWords++; + + } + } + } + } + +return placedWords; } -// Prints the word salad to console -void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) + +static void fillLetters(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Übrige freie Felder mit Buchstaben füllen { + for (int i = 0; i < searchFieldLen; i++) + { + + for (int j = 0; j < searchFieldLen; j++) + { + + if (salad[i][j] == EMPTY_CHAR) + { + + char random_letter = 'A' + (rand() % 26); + salad[i][j] = random_letter; + + } + } + } +} + +//Erstellt Wortsalat, plaziert Wörter zufällig & füllt die Lücken + +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(time(NULL)); + + initializeArray(salad, searchFieldLen); + + int len [wordCount]; + int count = 0; + + for(int i = 0; i < wordCount; i++) + { + + if(words [i] [0] != '\0') + count++; + + } + + int placeWords = place(salad, searchFieldLen, len, words, count); + + + fillLetters(salad, searchFieldLen); + + return placeWords; + +} + +// Ausgabe von Wortsalat auf Konsole + +void showWordSalad(const char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) +{ + + for (int i = 0; i < searchFieldLen; i++) + { + + for (int j = 0; j < searchFieldLen; j++) + { + + printf("%c ", salad [i][j]); + + } + + printf("\n"); + + } } diff --git a/Start_Windows/game.h b/Start_Windows/game.h index 95346af..f6fbdcb 100644 --- a/Start_Windows/game.h +++ b/Start_Windows/game.h @@ -9,3 +9,4 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen); #endif + diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..2d8778f 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -1,6 +1,7 @@ #include "input.h" #include #include +#include // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. @@ -9,4 +10,29 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + unsigned int count = 0; + char line[MAX_LINE_LEN]; + + while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount) + { + const char *delims = "\t\n\r ,;"; + char *token = strtok(line, delims); + + + while (token != NULL && count < maxWordCount) + { + for(unsigned int i = 0; token[i] != '\0'; i++) + { + token[i] = toupper((unsigned char)token[i]); + } + strncpy(words[count], token, MAX_WORD_LEN -1); + words[count][MAX_WORD_LEN -1] = '\0'; + count++; + token = strtok(NULL, delims); //hier + + } + + } + return count; + } \ No newline at end of file