From 6d68c832bad2609c4d07f9a4ea4258de603d141c Mon Sep 17 00:00:00 2001 From: Harun Faizi Date: Sun, 2 Nov 2025 13:18:36 +0100 Subject: [PATCH] =?UTF-8?q?readwords()=20und=20Makefile=20f=C3=BCr=20words?= =?UTF-8?q?alad=5Fmyversion=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- input.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ input.h | 11 ++++++++ makefile | 47 ++++++++++++++++++++++++++++++++ test.c | 9 ------ test_readwords.c | 3 ++ 5 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 input.c create mode 100644 input.h create mode 100644 makefile delete mode 100644 test.c create mode 100644 test_readwords.c diff --git a/input.c b/input.c new file mode 100644 index 0000000..c12dff1 --- /dev/null +++ b/input.c @@ -0,0 +1,71 @@ +//erstellt von Harun Faizi am 01.11.2025 +#include "input.h" +#include +#include + +// TODO: +// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. + +/* Liest Wörter aus words.txt und speichert sie in ein 2D-Array "words" + Trennzeichen: Komma, Semikolon, Leerzeichen und Zeilenumbruch + + file: geöffnete Datei (words.txt) + words: 2D-Array zu Speicherung der Wörter + maxWordCount: maximale Anzahl an Wörter, die eingelesen dürfen + + Rückgabe: Anzahl der eingelesen Wörter (0 falls nichts gelesen oder Fehler) +*/ + +// Read words from file and store in 'words' array +int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) +{ + if (file == 0) + return 0; //Sicherheitsprüfung + + char line[MAX_LINE_LEN]; // Puffer für eine Zeile aus Datei + unsigned int wordCount = 0; // zählt wie viele Wörter gefunden wurden + + //Lese Datei Zeile für Zeile + while(fgets(line, sizeof(line), file) != NULL) + { + // Zerlege die Datei in einzelne Wörter + char *token = strtok(line, ",;\n\t"); + + while (token != NULL && wordCount < maxWordCount) + { + //aktuelles Wort in Array kopieren + strncpy(words[wordCount], token, MAX_WORD_LEN -1); + words[wordCount][MAX_WORD_LEN -1] = '\0'; + + + // Entferne führende Leerzeichen + int start = 0; + while (words[wordCount][start] == ' ') + start++; + if (start > 0) + memmove(words[wordCount], words[wordCount] + start, strlen(words[wordCount]) - start + 1); + + // Entferne nachfolgende Leerzeichen + int end = strlen(words[wordCount]) - 1; + while (end >= 0 && words[wordCount][end] == ' ') + { + words[wordCount][end] = '\0'; + end--; + } + + + wordCount++; + + token = strtok(NULL, ",;\n"); + } + // Wenn max Wortzahl erreicht ist abbrechen! + if (wordCount >= maxWordCount) + break; + } + + return wordCount; +} + + + + diff --git a/input.h b/input.h new file mode 100644 index 0000000..032ec28 --- /dev/null +++ b/input.h @@ -0,0 +1,11 @@ +#ifndef INPUT_H +#define INPUT_H + +#include + +#define MAX_WORD_LEN 100 +#define MAX_LINE_LEN 1024 + +int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount); + +#endif diff --git a/makefile b/makefile new file mode 100644 index 0000000..572625f --- /dev/null +++ b/makefile @@ -0,0 +1,47 @@ +CC = gcc +CFLAGS = -g -Wall -I$(raylib_folder) +LDFLAGS = -lopengl32 -lgdi32 -lwinmm +BINARIES = ./windows + +raylib_folder = ./raylib +unityfolder = ./unity + +# -------------------------- +# initiales Spiel bauen +# -------------------------- +wordsalad_initial: + $(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) +wordsalad_myversion: main.o input.o game.o graphicalGame.o + $(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) + +# -------------------------- +# Normales Spiel bauen +# -------------------------- +all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) + +main.o: main.c + $(CC) -c $(CFLAGS) main.c + +input.o: 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 + +# -------------------------- +# Unit Tests +# -------------------------- +TEST_BIN = runTests + +test: input.o game.o unit_tests.c + $(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a + +# -------------------------- +# Clean +# -------------------------- +clean: + del /f *.o *.exe diff --git a/test.c b/test.c deleted file mode 100644 index da74880..0000000 --- a/test.c +++ /dev/null @@ -1,9 +0,0 @@ -// -// Created by faizi on 31.10.2025. -// -#include - -int main(){ -printf("Hallo von der Main"); -return 0; -} \ No newline at end of file diff --git a/test_readwords.c b/test_readwords.c new file mode 100644 index 0000000..d9f2c4a --- /dev/null +++ b/test_readwords.c @@ -0,0 +1,3 @@ +// +// Created by faizi on 02.11.2025. +//