From c5c2824a976bfc722e8a7f898b5b1a1083271e56 Mon Sep 17 00:00:00 2001 From: Kristin Date: Thu, 30 Oct 2025 21:20:06 +0100 Subject: [PATCH] =?UTF-8?q?tests=20ok,=20programm=20noch=20aufr=C3=A4umen!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Start_Windows/game.c | 136 ++++++++++++++++++++++++++++++++++++++--- Start_Windows/input.c | 50 +++++++++++++-- Start_Windows/makefile | 10 +-- 3 files changed, 180 insertions(+), 16 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..3643d59 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -1,23 +1,145 @@ #include "game.h" -#include #include #include +#include #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 -//TODO: Spiellogik implementieren: +// 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 createWordSalad( + + char salad[MAX_SEARCH_FIELD_LEN] + [MAX_SEARCH_FIELD_LEN], // salad ist Spielfeld 2D Array mit 20*20 + // Buchstaben + unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], + unsigned int wordCount) { + + srand(time(NULL)); + enum Richtung { HORIZONTAL, VERTIKAL }; + + // wipe field + // array auf emptychar + + for (int x = 0; x < searchFieldLen; x++) { + + for (int y = 0; y < searchFieldLen; y++) { + + salad[y][x] = EMPTY_CHAR; + } + } + int placedWords = 0; // variable platzierte Wörter + + // place words + // for schleife mit wortzahl = 0 bis wordCount + + for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) { + + // strlen mit Wortlänge + int wortLen = strlen(words[wortNummer]); + + int platziert = 0; + int versuch = 0; + + // rand % 2 vertikal oder horizontal + // rand % searchFieldLen um Ort zu generieren + // Länge überprüfen und überprüfen, ob wort sich überschneidet ist nicht + // randomtries + + while (versuch < MAX_RAND_TRIES_PER_WORD && platziert == 0) { + + enum Richtung r = (rand() % 2) ? HORIZONTAL : VERTIKAL; + + int x = rand() % searchFieldLen; + int y = rand() % searchFieldLen; + int fits = 1; + + if (r == HORIZONTAL) { + + if ((y + wortLen) > (searchFieldLen)) { + fits = 0; + } + + if (fits) { + for (int i = 0; i < wortLen; i++) { + char var = salad[y + i][x]; + if (var != EMPTY_CHAR) { + fits = 0; + } + } + } + + if (fits != 0) { + + for (int i = 0; i < wortLen; i++) { + + salad[y + i][x] = words[wortNummer][i]; + } + + platziert = 1; + placedWords++; + } + + } + + else if (r == VERTIKAL) { + + if ((x + wortLen) > searchFieldLen) { + fits = 0; + } + + if (fits) { + + for (int i = 0; i < wortLen; i++) { + char var = salad[y][x + i]; + if (var != EMPTY_CHAR) { + fits = 0; + } + } + } + + if (fits != 0) { + + for (int i = 0; i < wortLen; i++) { + + salad[y][x + i] = words[wortNummer][i]; + } + + platziert = 1; + placedWords++; + } + } + + versuch++; + } + } + + for (int i = 0; i < searchFieldLen; i++) { + + for (int j = 0; j < searchFieldLen; j++) { + + if (salad[i][j] == EMPTY_CHAR) { + + salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; + } + } + } + return placedWords; + // emptychar rand() % ('Z' - 'A' + 1) + 'A' } // Prints the word salad to console -void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) -{ +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]); + } + } } diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..0c687a2 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -1,12 +1,54 @@ #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. +// 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) -{ +int readWords(FILE *file, char words[][MAX_WORD_LEN], + unsigned int maxWordCount) { + if (file == NULL) { + perror("File couldn't be opened"); + return 0; + } + + // zunächst fehlerhaften String einlesen und in anderem Array + // zwischenspeichern + + char line[MAX_LINE_LEN]; + char *token; + int wordCount = 0; + + while (fgets(line, sizeof(line), file) != + NULL && // während mit fgets alle Zeichen aus dem file eingelesen + // werden + wordCount < maxWordCount) { + + token = strtok(line, " ;,.\n"); // Erstes Wort mit strtok aus dem + // fehlerhaften String herauslösen + + while (token != NULL && + wordCount < maxWordCount) { // while strtok nicht am Ende ist und + // noch Wörter in words passen + + for (int i = 0; token[i] != '\0'; i++) { + token[i] = toupper((unsigned char)token[i]); + } + + strncpy(words[wordCount], token, + sizeof(words[wordCount]) - + 1); // mit strcpy das aktuelle Wort in words kopieren + words[wordCount][sizeof(words[wordCount]) - 1] = + '\0'; // Nullterminator mit sizeof des aktuellen Worts - 1 an Ende des + // Worts setzen + + wordCount++; // Nächstes Wort + token = strtok(NULL, " ;,.\n"); // Nächstes Token + } + } + + return wordCount; // Anzahl der eingelesenen Wörter } \ No newline at end of file diff --git a/Start_Windows/makefile b/Start_Windows/makefile index a10f550..9d66779 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -g -Wall -I$(raylibfolder) +CFLAGS = -g -Wall LDFLAGS = -lopengl32 -lgdi32 -lwinmm BINARIES = ./windows @@ -15,7 +15,7 @@ wordsalad_initial: #--------------------------- # eigenes Target bauen #--------------------------- -wordsalad_myversion: +wordsalad_myversion: main.o graphicalGame.o $(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS) @@ -29,13 +29,13 @@ main.o: main.c $(CC) -c $(CFLAGS) main.c input.o: input.c - $(CC) -c $(CFLAGS)input.c + $(CC) -c $(CFLAGS) input.c game.o: game.c $(CC) -c $(CFLAGS) game.c graphicalGame.o: graphicalGame.c - $(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c + $(CC) -I$(raylib_folder) $(CFLAGS) -c graphicalGame.c # -------------------------- # Unit Tests @@ -49,4 +49,4 @@ test: input.o game.o unit_tests.c # Clean # -------------------------- clean: - del /f *.o *.exe + rm -f *.o *.exe