5 changed files with 74 additions and 36 deletions

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
# Wortsalat Projekt
Ein GeneDas Programm liest eine Liste von Wörtern aus einer übergebenen Textdatei (z.B. `words.txt`), versteckt diese zufällig in einem Raster und füllt die restlichen leeren Felder mit zufälligen Buchstaben auf, sodass ein Wortsuchrätsel entsteht.
Code frisch bauen
make clean
make wordsalad_myversion
Das Spiel starten
./wordsalad_myversion words.txt
Uni-Tests ausführen
make test
./runTests
+1 -1
View File
@@ -3,7 +3,7 @@
#include "input.h"
#define MAX_SEARCH_FIELD_LEN 20
#define MAX_SEARCH_FIELD_LEN 100
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
-33
View File
@@ -1,33 +0,0 @@
#include <stdio.h>
#include "graphicalGame.h"
#include "game.h"
int main(void)
{
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
char words[][MAX_WORD_LEN] = {
"HALLO",
"WELT",
"AUTO",
"BIRNE",
"APFEL",
"BANANE",
"LANGESWORT",
"REITSPORT",
"APFELBAUM",
"DRACULA",
"LAPTOP",
"BILDSCHRIM",
"HAUS"
};
unsigned int wordCount = sizeof(words) / sizeof(words[0]);
int placedWords = createWordSalad(salad, MAX_SEARCH_FIELD_LEN, words, wordCount);
startGame(salad, MAX_SEARCH_FIELD_LEN, words, wordCount, 1000);
printf("Placed words: %d\n", placedWords);
return 0;
}
+57
View File
@@ -6,7 +6,64 @@
// 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
// Liest Wörter aus einer Textdatei und speichert sie im Array.
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
// Prüft, ob die Eingaben gültig sind.
if (file == NULL || words == NULL || maxWordCount == 0) {
return 0;
}
unsigned int count = 0;
char currentWord[MAX_WORD_LEN];
size_t length = 0;
int tooLong = 0;
int ch;
while (count < maxWordCount && (ch = fgetc(file)) != EOF) {
// fgetc(file) liest jeweils ein Zeichen aus der Datei.
if (ch == ',' || ch == ';' || isspace((unsigned char)ch)) {
// isspace() erkennt Leerraumzeichen wie Leerzeichen und Zeilenumbrüche. Kommas und Semikolons werden separat geprüft.
// Speichert das Wort, wenn es nicht leer und nicht zu lang ist.
if (length > 0 && !tooLong) {
currentWord[length] = '\0';
strncpy(words[count], currentWord, MAX_WORD_LEN);
// strncpy() kopiert das fertige Wort mit einer festgelegten maximalen Länge in das Zielarray.
count++; // Erhöht die Anzahl der gespeicherten Wörter.
}
// Setzt die Variablen für das nächste Wort zurück.
length = 0;
tooLong = 0;
}
else if (!tooLong) {
// Prüft, ob noch Platz für das Zeichen und '\0' vorhanden ist.
if (length < MAX_WORD_LEN - 1) {
// toupper() wandelt Kleinbuchstaben in Großbuchstaben um.
// MAX_WORD_LEN - 1 Lässt Platz für '\0', das das Ende eines C-Strings kennzeichnet und verhindert, dass über den Wortpuffer hinausgeschrieben wird.
currentWord[length] = (char)toupper((unsigned char)ch);
length++;
}
else {
// Überspringt das Wort, wenn es zu lang ist.
tooLong = 1;
length = 0;
}
}
}
// Speichert das letzte Wort, falls die Datei ohne Trennzeichen endet.
if (count < maxWordCount && length > 0 && !tooLong) {
currentWord[length] = '\0';
strncpy(words[count], currentWord, MAX_WORD_LEN);
count++;
}
// Gibt die Anzahl der gespeicherten Wörter zurück.
return (int)count;
}
+2 -2
View File
@@ -43,8 +43,8 @@ graphicalGame.o: $(codefolder)/graphicalGame.c
# --------------------------
# mein Spiel bauen
#---------------------------
wordsalad_myversion: main.o
$(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
# --------------------------