From 9803a8e339d3796c0d3bb41dea768590563a9f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helena=20K=C3=B6hler?= Date: Tue, 4 Nov 2025 17:21:05 +0100 Subject: [PATCH] neu --- .vscode/settings.json | 6 +++ .vscode/tasks.json | 28 +++++++++++++ Start_Windows/game.c | 67 ++++++++++++++++++++++++++++++ Start_Windows/input.c | 36 ++++++++++++++++ Start_Windows/main.c | 13 ++++++ game.c | 97 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 247 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 game.c diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..67b3e59 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "string.h": "c", + "ctype.h": "c" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..15bd63e --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc.exe build active file", + "command": "C:\\ProgramData\\mingw64\\mingw64\\bin\\gcc.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..8b36180 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -10,11 +10,78 @@ /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ +//Funktion: Initialisiert das Spielfeld mit leeren Zeichen +static void intitSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse) +{ + for (unsigned int i =0; i < feldGroesse; i++) + for (unsigned int j=0; j < feldGroesse; j++) + salad[i][j]= EMPTY_CHAR; +} + +//Funktion: Versucht, ein Wort an einer bestimmten Position zu platzieren +//Richtung: 0 = horizontal, 1 = vertikal +static int platziereWort(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse, const char *wort, int zeile, int spalte, int richtung) +{ + unsigned int wortLaenge = strlen(wort); + + //Überprüfen, ob das Word ins Feld passt + if (richtung == 0 && spalte + wortLaenge > feldGroesse) return 0; //horizontal + if (richtung == 1 && zeile + wortLaenge > feldGroesse) return 0; //vertikal + + //Prüfen, ob die Positionen frei oder kompatibel sind + for (unsigned int i = 0; i < wortLaenge; i++){ + char c = (richtung == 0) ? salad[zeile][spalte + i] : salad[zeile + i][spalte]; + if (c != EMPTY_CHAR && c != wort[i]) + return 0; //Kollision + } + + //Wort eintragen + for (unsigned int i = 0; i < wortLaenge; i++){ + if (richtung == 0) + salad[zeile][spalte + i ] = wort[i]; + else + salad[zeile + i][spalte] = wort[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) { + srand((unsigned int)time(NULL)); + initSalad(salad, searchFieldLen); + +unsigned int erfolgreichGesetzt = 0; + + //Worter zufällig platzieren + for (unsigned int w = 0; w < wordCount; w++) + { + int platziert = 0; + for (int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD && !platziert; versuch++) + { + int richtung = rand() % 2; + int zeile = rand() % searchFieldLen; + int spalte= rand() % searchFieldLen; + platziert = platziereWort(salad, searchFieldLen, words[w], zeile, spalte, richtung); + } + if (platziert) + erfolgreichGesetzt++; + else + fprintf(stderr, "Warnung: Konnte Wort nicht platzieren: %s\n", words[w]); + } } +//Leere Felder mit Zufallsbuchstaben 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] = ´A´ + rand() % 26; + } + return erfolgreichGesetzt; +} + // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..3903a93 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -8,5 +8,41 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + char line[MAX_LINE_LEN]; + int word_count=0; + //Öffnen der Textdatei + if (file == 0){ + perror("Fehler beim Öffnen der Datei"); + return 1; + } + + //Einlesen der Datei Zeile für Zeile + while (fgets(line, sizeof(line), file)){ + char *token = strtok(line, " ,;\n"); + //Extrahieren von jedem Wort + while (token != NULL && word_count < maxWordCount){ + //Entferne führende und nachfolgende Leerzeichen + while (isspace((unsigned char) *token)){ + token++; + } + size_t len = strlen(token); + while (len > 0 && isspace((unsigned char) token[len - 1])){ + token[--len] = '\n'; + } + //Speichere das Wort im Array, falls es nicht leer ist + if (len > 0){ + //Wandel das Wort in Großbuchstaben um + for (size_t = 0; i < len; i++ ){ + token[i] = toupper ((unsigned char)token[i]); + } + strncpy(words[word_count], token, MAX_WORD_LEN -1); + words[word_count][MAX_WORD_LEN -1] = '\0'; + word_count++ + } + //Nächstes Wort + token = strtok(NULL, " ,;\n"); + } + } + return word_count; } \ No newline at end of file diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..652b61e 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -40,6 +40,19 @@ 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) + { + printf("Alle %u Wörter wurden erfolgreich platziert!\n\n", wordCount); + + //Spiel starten + + startGame(wordSalad, SALAD_SIZE, words, wordCount, 800); + } + else + { + fprintf(stderr, "Fehler: Nur %u von %u Wörtern konnten platziert werden.\n", placedWords, wordCount); + exitCode = EXIT_FAILURE; + } } else diff --git a/game.c b/game.c new file mode 100644 index 0000000..8ed53df --- /dev/null +++ b/game.c @@ -0,0 +1,97 @@ +#include "game.h" +#include +#include +#include + +#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 */ + +//Funktion: Initialisiert das Spielfeld mit leeren Zeichen +static void intitSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse) +{ + for (unsigned int i =0; i < feldGroesse; i++) + for (unsigned int j=0; j < feldGroesse; j++) + salad[i][j]= EMPTY_CHAR; +} + +//Funktion: Versucht, ein Wort an einer bestimmten Position zu platzieren +//Richtung: 0 = horizontal, 1 = vertikal +static int platziereWort(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse, const char *wort, int zeile, int spalte, int richtung) +{ + unsigned int wortLaenge = strlen(wort); + + //Überprüfen, ob das Word ins Feld passt + if (richtung == 0 && spalte + wortLaenge > feldGroesse) return 0; //horizontal + if (richtung == 1 && zeile + wortLaenge > feldGroesse) return 0; //vertikal + + //Prüfen, ob die Positionen frei oder kompatibel sind + for (unsigned int i = 0; i < wortLaenge; i++){ + char c = (richtung == 0) ? salad[zeile][spalte + i] : salad[zeile + i][spalte]; + if (c != EMPTY_CHAR && c != wort[i]) + return 0; //Kollision + } + + //Wort eintragen + for (unsigned int i = 0; i < wortLaenge; i++){ + if (richtung == 0) + salad[zeile][spalte + i ] = wort[i]; + else + salad[zeile + i][spalte] = wort[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) +{ + srand((unsigned int)time(NULL)); + initSalad(salad, searchFieldLen); + +unsigned int erfolgreichGesetzt = 0; + + //Worter zufällig platzieren + for (unsigned int w = 0; w < wordCount; w++) + { + int platziert = 0; + for (int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD && !platziert; versuch++) + { + int richtung = rand() % 2; + int zeile = rand() % searchFieldLen; + int spalte= rand() % searchFieldLen; + platziert = platziereWort(salad, searchFieldLen, words[w], zeile, spalte, richtung); + } + if (platziert) + erfolgreichGesetzt++; + else + fprintf(stderr, "Warnung: Konnte Wort nicht platzieren: %s\n", words[w]); + } + +} +//Leere Felder mit Zufallsbuchstaben 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] = 'A' + rand() % 26; + } + return erfolgreichGesetzt; +} + + +// Prints the word salad to console +void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) +{ + for (unsigned int i = 0; i < searchFieldLen; i++){ + for (unsigned int j = 0; j < searchFieldLen; j++) + { + printf("%c ", salad[i][j]); + } + printf("\n"); + } + +}