From 125eb6e9e88d79d85667dfa3e69aae30c1481652 Mon Sep 17 00:00:00 2001 From: Simon Schuerer Date: Mon, 20 Oct 2025 12:08:02 +0000 Subject: [PATCH 1/3] Start_Windows/game.c aktualisiert --- Start_Windows/game.c | 65 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..75c9a1b 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -6,18 +6,73 @@ #define MAX_RAND_TRIES_PER_WORD 10 #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 */ + // [NEU] Hilfsfunktion: zufälliger Buchstabe +char randomChar() { // zufälliges Char wird erzeugt + return 'A' + rand() % 26; // A + zufallszahl bis 26 für alle möglichen zahlen mit ä,ö,ü +} + +// [NEU] Hilfsfunktion: Wort platzieren +int tryPlaceWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char* word, int horizontal) { + int len = strlen(word); + int maxRow = horizontal ? searchFieldLen : searchFieldLen - len; + int maxCol = horizontal ? searchFieldLen - len : searchFieldLen; + + int row = rand() % maxRow; // Maximale Reihen definieren + int col = rand() % maxCol; // Maximale Spalten definieren + + // Prüfen auf Kollision + for (int i = 0; i < len; i++) { //for schleife, beginnt bei 0 und zählt bis zur eingegebenen länge + int r = row + (horizontal ? 0 : i); // + int c = col + (horizontal ? i : 0); + if (salad[r][c] != EMPTY_CHAR && salad[r][c] != word[i]) + return 0; // Kollision + } + + // Wort platzieren + for (int i = 0; i < len; i++) { + int r = row + (horizontal ? 0 : i); + int c = col + (horizontal ? i : 0); + salad[r][c] = word[i]; + } + + return 1; +} // 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) { + // [NEU] Initialisiere das Feld + for (unsigned int i = 0; i < searchFieldLen; i++) + for (unsigned int j = 0; j < searchFieldLen; j++) + salad[i][j] = EMPTY_CHAR; + // [NEU] Wörter platzieren + for (unsigned int w = 0; w < wordCount; w++) { + int placed = 0; + for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) { + int horizontal = rand() % 2; + placed = tryPlaceWord(salad, searchFieldLen, words[w], horizontal); + } + if (!placed) return 0; // Fehler beim Platzieren + } + + // [NEU] Restliche Felder fü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] = randomChar(); + + return 1; // Erfolg } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - -} + // [NEU] Ausgabe des Gitters + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + printf("%c ", salad[i][j]); + } + printf("\n"); + } +} \ No newline at end of file From c6e0cbd667960aefcd891112671fbf69765bcbcc Mon Sep 17 00:00:00 2001 From: Simon Schuerer Date: Thu, 30 Oct 2025 09:59:58 +0000 Subject: [PATCH 2/3] Start_Windows/game.c aktualisiert --- Start_Windows/game.c | 74 ++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 55 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 75c9a1b..20b2a46 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -1,69 +1,33 @@ -#include "game.h" -#include -#include -#include +#include "game.h" // Einbindung der Header-Datei mit Funktionsdeklarationen und Konstanten +#include // Für die Initialisierung des Zufallszahlengenerators +#include // Für rand(), srand() und Speicherfunktionen +#include // Für strlen() zur Ermittlung der Wortlänge +#include // Für printf() in showWordSalad -#define MAX_RAND_TRIES_PER_WORD 10 -#define EMPTY_CHAR 0 +#define MAX_RAND_TRIES_PER_WORD 10 // Maximale Versuche, ein Wort zufällig zu platzieren +#define EMPTY_CHAR 0 // Kennzeichnung für ein leeres Feld im Spielfeld - // [NEU] Hilfsfunktion: zufälliger Buchstabe -char randomChar() { // zufälliges Char wird erzeugt - return 'A' + rand() % 26; // A + zufallszahl bis 26 für alle möglichen zahlen mit ä,ö,ü -} - -// [NEU] Hilfsfunktion: Wort platzieren -int tryPlaceWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char* word, int horizontal) { - int len = strlen(word); - int maxRow = horizontal ? searchFieldLen : searchFieldLen - len; - int maxCol = horizontal ? searchFieldLen - len : searchFieldLen; - - int row = rand() % maxRow; // Maximale Reihen definieren - int col = rand() % maxCol; // Maximale Spalten definieren - - // Prüfen auf Kollision - for (int i = 0; i < len; i++) { //for schleife, beginnt bei 0 und zählt bis zur eingegebenen länge - int r = row + (horizontal ? 0 : i); // - int c = col + (horizontal ? i : 0); - if (salad[r][c] != EMPTY_CHAR && salad[r][c] != word[i]) - return 0; // Kollision - } - - // Wort platzieren - for (int i = 0; i < len; i++) { - int r = row + (horizontal ? 0 : i); - int c = col + (horizontal ? i : 0); - salad[r][c] = word[i]; - } - - return 1; -} - -// Creates the word salad by placing words randomly and filling empty spaces +// Hauptfunktion zur Erstellung des Wortsalats int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { - // [NEU] Initialisiere das Feld + srand(time(NULL)); // Initialisiert den Zufallsgenerator mit der aktuellen Uhrzeit + + // Setzt alle Felder im Spielfeld auf EMPTY_CHAR (also leer) for (unsigned int i = 0; i < searchFieldLen; i++) for (unsigned int j = 0; j < searchFieldLen; j++) salad[i][j] = EMPTY_CHAR; - // [NEU] Wörter platzieren + // Schleife über alle Wörter, die platziert werden sollen for (unsigned int w = 0; w < wordCount; w++) { - int placed = 0; + const char* word = words[w]; // Zeiger auf das aktuelle Wort + size_t len = strlen(word); // Länge des aktuellen Wortes + int placed = 0; // Flag, ob das Wort erfolgreich platziert wurde + + // Versuche das Wort bis zu MAX_RAND_TRIES_PER_WORD-mal zu platzieren for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) { - int horizontal = rand() % 2; - placed = tryPlaceWord(salad, searchFieldLen, words[w], horizontal); - } - if (!placed) return 0; // Fehler beim Platzieren - } + int dir = rand() % 2; // Zufällige Richtung: 0 = horizontal, 1 = vertikal + int row = rand() % searchFieldLen; // Zufällige Zeile - // [NEU] Restliche Felder fü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] = randomChar(); - - return 1; // Erfolg -} // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) From 6bf06ab198e6c0597491dc590f3201a6fe03725c Mon Sep 17 00:00:00 2001 From: Simon Schuerer Date: Thu, 30 Oct 2025 10:02:19 +0000 Subject: [PATCH 3/3] Start_Windows/graphicalGame.c aktualisiert --- Start_Windows/graphicalGame.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Start_Windows/graphicalGame.c b/Start_Windows/graphicalGame.c index fc648b6..3b95e29 100644 --- a/Start_Windows/graphicalGame.c +++ b/Start_Windows/graphicalGame.c @@ -66,8 +66,27 @@ typedef struct // Creates a helper message to guide the user static HelperMessage createHelperMessage(unsigned int screenWidth) { - const char *text = "Please search below for the words located at the bottom \nand draw a line exactly on the desired characters ..."; - HelperMessage msg = {"", {0, 0}, {screenWidth, 0}, 18}; + // Der Hilfetext, der dem Spieler angezeigt wird, um das Spielprinzip zu erklären + const char *text = "Please search below for the words located at the bottom \n" + "and draw a line exactly on the desired characters ..."; + + // Initialisiert eine HelperMessage-Struktur: + // - text: der Hinweistext + // - start: Startposition des Textes (x=0, y=0) + // - end: Endposition des Textes (x=screenWidth, y=0) – also über die gesamte Breite + // - fontSize: Schriftgröße des Textes (hier: 18) + HelperMessage msg = { "", {0, 0}, {screenWidth, 0}, 18 }; + + // Kopiert den Hinweistext in das msg.text-Feld (sicher mit strncpy) + strncpy(msg.text, text, sizeof(msg.text) - 1); + + // Stellt sicher, dass der Text nullterminiert ist + msg.text[sizeof(msg.text) - 1] = '\0'; + + // Gibt die fertige HelperMessage zurück + return msg; +} +`` // Copy text into msg, ensuring does not exceed max length strncpy(msg.text, text, MAX_MESSAGE_LEN);