readwords() und Makefile für wordsalad_myversion hinzugefügt

This commit is contained in:
Harun Faizi 2025-11-02 13:18:36 +01:00
parent 16ddb916ba
commit 6d68c832ba
5 changed files with 132 additions and 9 deletions

71
input.c Normal file
View File

@ -0,0 +1,71 @@
//erstellt von Harun Faizi am 01.11.2025
#include "input.h"
#include <string.h>
#include <ctype.h>
// 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;
}

11
input.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef INPUT_H
#define INPUT_H
#include <stdio.h>
#define MAX_WORD_LEN 100
#define MAX_LINE_LEN 1024
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
#endif

47
makefile Normal file
View File

@ -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

9
test.c
View File

@ -1,9 +0,0 @@
//
// Created by faizi on 31.10.2025.
//
#include <stdio.h>
int main(){
printf("Hallo von der Main");
return 0;
}

3
test_readwords.c Normal file
View File

@ -0,0 +1,3 @@
//
// Created by faizi on 02.11.2025.
//