From a373e95c9f4b8bd2134fee002d9b364df4046805 Mon Sep 17 00:00:00 2001 From: Thilo Date: Mon, 3 Nov 2025 19:35:58 +0100 Subject: [PATCH] added new, updated files --- .vscode/tasks.json | 28 ++++++++++ Start_Windows/game.c | 115 ++++++++++++++++++++++++++++++++++++++++-- Start_Windows/input.c | 48 ++++++++++++++++-- Start_Windows/input.h | 1 + Start_Windows/main.c | 12 ++++- 5 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 .vscode/tasks.json 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..85800a3 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -2,22 +2,129 @@ #include #include #include +#include -#define MAX_RAND_TRIES_PER_WORD 10 +#define MAX_RAND_TRIES_PER_WORD 100 #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 */ +* 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) { + int wordlength; + int direction; + int horizontal; + int vertical; + int possible_coordinate = 0; + int place_word = 0; + // Initialisierung Zufallszahl + srand(time(NULL)); + // Generierung des Wortfeldes: Alle Felder auf "Leer" setzen + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = '\0'; + } + } + + for (int w = 0; w < wordCount; w++) + { + wordlength = strlen(words[w]); + possible_coordinate = 0; + + + // Versuch bis zu MAX_RAND_TRIES_PER_WORD eine Position zu finden + for (int c = 0; c < MAX_RAND_TRIES_PER_WORD && possible_coordinate == 0; c++) + { + possible_coordinate = 1; + + direction = rand() % 2; + + if (direction == 0) // Vertikale Wortpositionierung + { + vertical = rand() % (searchFieldLen - wordlength + 1); + horizontal = rand() % searchFieldLen; + + for (int i = 0; i < wordlength; i++) + { + char current = salad[vertical + i][horizontal]; + + // Feld muss leer oder mit gleichem Buchstaben belegt sein + if (current != '\0' && current != words[w][i]) + { + possible_coordinate = 0; + break; + } + } + } + else // Horizontale Wortpositionierung + { + vertical = rand() % searchFieldLen; + horizontal = rand() % (searchFieldLen - wordlength + 1); + + for (int i = 0; i < wordlength; i++) + { + char current = salad[vertical][horizontal + i]; + + if (current != '\0' && current != words[w][i]) + { + possible_coordinate = 0; + break; + } + } + } + } + + if (possible_coordinate == 0) + return place_word; // Rückgabe: Anzahl der positionierten Wörter + + // Platzierung des Wortes + if (direction == 0) // Vertikal Wortpositionierung + { + for (int i = 0; i < wordlength; i++) + { + salad[vertical + i][horizontal] = words[w][i]; + } + } + else // Horizontal Wortpositionierung + { + for (int i = 0; i < wordlength; i++) + { + salad[vertical][horizontal + i] = words[w][i]; + } + } + place_word = w + 1; + } + + // Auffüllen der leeren Felder + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + if (salad[i][j] == '\0') + { + salad[i][j] = 'A' + rand() % 26; + } + } + } + + return place_word; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + printf("---Wortsalat---\n"); + 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/input.c b/Start_Windows/input.c index ed77805..bc1de36 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -1,12 +1,54 @@ #include "input.h" #include #include +#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) -{ -} \ No newline at end of file +int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) +{ + if (file == NULL) + { + printf("Error! File cannot be opened!\n"); + return EXIT_FAILURE; + } + + unsigned int count = 0; + char line[MAX_LINE_LEN]; + const char *delimiter = " ,;.\n"; + + while (count < maxWordCount && fgets(line, MAX_LINE_LEN, file) != NULL) + { + char *token = strtok(line, delimiter); //Teilt die Zeile nach den Trennzeichen auf + + while (token != NULL && count < maxWordCount) + { + strncpy(words[count], token, MAX_WORD_LEN - 1); //Kopieren des Wortes in dasd Array words + words[count][MAX_WORD_LEN - 1] = '\0'; //Händisches Einfügen des Null-Termminators, da dieser nicht automatisch eingefügt wird + + toUpperCase(words[count]); //Umwandeln der Kleinbuchstaben in Großbuchstaben + + count++; + + token = strtok(NULL, delimiter); + } + + } + printf("Total words read: %d\n", count); + + return count; +} + +void toUpperCase(char *str) +{ + for (int i = 0; str[i] != '\0'; i++) + { + str[i] = toupper(str[i]); + } + +} + \ No newline at end of file diff --git a/Start_Windows/input.h b/Start_Windows/input.h index 032ec28..5441fdb 100644 --- a/Start_Windows/input.h +++ b/Start_Windows/input.h @@ -7,5 +7,6 @@ #define MAX_LINE_LEN 1024 int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount); +void toUpperCase(char *str); #endif diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..ea20c6b 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -36,12 +36,22 @@ 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) + { + startGame(wordSalad, SALAD_SIZE, words, placedWords, 800); + } + + else + { + printf("The game can not start! %d words can not be placed!", wordCount - placedWords); + exitCode = EXIT_FAILURE; + } } + else { // Print error message if file couldn't be opened