From 81d932da9f4236ad7290b850eb1c6965e4288f2b Mon Sep 17 00:00:00 2001 From: koestnerlu100214 Date: Thu, 6 Nov 2025 08:56:43 +0100 Subject: [PATCH] =?UTF-8?q?Testdatei=20l=C3=A4uft=20jetzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Start_Windows/game.c | 91 ++++++++++++++++++++++++++++++++++++------- Start_Windows/input.c | 28 ++++++++++++- Start_Windows/main.c | 27 +++++++------ 3 files changed, 116 insertions(+), 30 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..30d83cb 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -1,23 +1,84 @@ +#include "input.h" #include "game.h" -#include -#include -#include +#include // für rand(), srand() +#include // für time() +#include // für strlen() -#define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 +#define MAX_RAND_TRIES_PER_WORD 10 -//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) { + srand(time(NULL)); + int placedWords = 0; -} - -// Prints the word salad to console -void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) -{ - + // Initialisiere das Feld mit 0 (leere Zeichen) + for (unsigned int i = 0; i < searchFieldLen; i++) { + for (unsigned int j = 0; j < searchFieldLen; j++) { + salad[i][j] = EMPTY_CHAR; + } + } + + for (unsigned int w = 0; w < wordCount; w++) { + const char* word = words[w]; + size_t len = strlen(word); + int placed = 0; + + for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) { + int direction = rand() % 2; // 0 = horizontal, 1 = vertical + int row = rand() % searchFieldLen; + int col = rand() % searchFieldLen; + + if (direction == 0) { // horizontal + if (col + len > searchFieldLen) continue; + + int fits = 1; + for (size_t i = 0; i < len; i++) { + if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i]) { + fits = 0; + break; + } + } + + if (fits) { + for (size_t i = 0; i < len; i++) { + salad[row][col + i] = word[i]; + } + placed = 1; + } + } else { // vertical + if (row + len > searchFieldLen) continue; + + int fits = 1; + for (size_t i = 0; i < len; i++) { + if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i]) { + fits = 0; + break; + } + } + + if (fits) { + for (size_t i = 0; i < len; i++) { + salad[row + i][col] = word[i]; + } + placed = 1; + } + } + } + + if (placed) { + placedWords++; + } + } + + // Fülle leere Felder mit zufälligen Buchstaben + 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 placedWords; } diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..d0b8e42 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -1,12 +1,38 @@ #include "input.h" #include #include - +#include //sicherstellen, dass FILE deklariert ist // 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; + char buffer[256]; // temporärer Speicher für Zeilen + // zusätzliche Bedingung "count < maxWordCount" + while (count < maxWordCount && fgets(buffer, sizeof(buffer), file) != NULL) + { + // gleiche Trennzeichen, aber Formatierung beibehalten + char *token = strtok(buffer, " \t\r\n.,;:!?()[]{}\"'"); // Trennzeichen + + while (token != NULL) + { + if (count >= maxWordCount) + break; // Sicherheitsabbruch auch hier + + // toupper(), um Großbuchstaben zu erzwingen + for (int i = 0; token[i]; i++) + token[i] = toupper((unsigned char)token[i]); + + // sicheres Kopieren ins Zielarray + strncpy(words[count], token, MAX_WORD_LEN - 1); + words[count][MAX_WORD_LEN - 1] = '\0'; // String terminieren + count++; + + token = strtok(NULL, " \t\r\n.,;:!?()[]{}\"'"); + } + } + return count; } \ No newline at end of file diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..0c1488a 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -11,7 +11,6 @@ int main(int argc, char *argv[]) { int exitCode = EXIT_SUCCESS; - // Check if the correct number of arguments is provided if(argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); @@ -19,35 +18,35 @@ int main(int argc, char *argv[]) } else { - char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game + char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; unsigned int wordCount = 0; + unsigned int placedWords = 0; // <-- Hierher verschoben + char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; FILE *file = fopen(argv[1], "r"); if(file != NULL) { - unsigned int placedWords = 0; - char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad - - // Read words from file and store in 'words' array wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); fclose(file); - // 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 - } else { - // Print error message if file couldn't be opened fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); exitCode = EXIT_FAILURE; } + + if (placedWords == wordCount) + { + startGame(wordSalad, SALAD_SIZE, words, wordCount, 800); + } + else + { + fprintf(stderr, "Nur %u von %u Wörtern konnten im Buchstabensalat platziert werden.\n", placedWords, wordCount); + exitCode = EXIT_FAILURE; + } } return exitCode;