15 Commits
3 changed files with 179 additions and 1 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
+108 -1
View File
@@ -6,7 +6,7 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
//DONE: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
@@ -14,4 +14,111 @@
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((unsigned int)time(NULL));
typedef enum{
Horizontal,
Vertikal
} Direction;
for (unsigned int row = 0; row < searchFieldLen; row++)
{
for (unsigned int col = 0; col < searchFieldLen; col++)
{
salad[row][col] = EMPTY_CHAR; // Alle Felder leer setzen
}
}
unsigned int placedWords = 0;
// 2. Alle Wörter nacheinander platzieren
for (unsigned int i = 0; i < wordCount; i++)
{
unsigned int wordLen = strlen(words[i]);
int placed = 0;
// Wort darf nicht länger als das Spielfeld sein
if (wordLen == 0 || wordLen > searchFieldLen)
{
continue;
}
for (unsigned int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
{
unsigned int startRow = 0;
unsigned int startCol = 0;
Direction direction = (Direction)(rand()%2);
if (direction == Horizontal)
{
startRow = rand() % searchFieldLen;
startCol = rand() % (searchFieldLen - wordLen + 1);
}
else // Vertikal
{
startRow = rand() % (searchFieldLen - wordLen + 1);
startCol = rand() % searchFieldLen;
}
// Überprüfen ob das Wort an der gewählten Position platziert werden kann
int canPlace = 1;
for (unsigned int j = 0; j < wordLen; j++)
{
if (direction == Horizontal)
{
if (salad[startRow][startCol + j] != EMPTY_CHAR && salad[startRow][startCol + j] != words[i][j])
{
canPlace = 0;
break;
}
}
else // Vertikal
{
if (salad[startRow + j][startCol] != EMPTY_CHAR && salad[startRow + j][startCol] != words[i][j])
{
canPlace = 0;
break;
}
}
}
if (canPlace)
{
// Wort platzieren
for (unsigned int j = 0; j < wordLen; j++)
{
if (direction == Horizontal)
{
salad[startRow][startCol + j] = words[i][j];
}
else // Vertikal
{
salad[startRow + j][startCol] = words[i][j];
}
}
placedWords++;
placed = 1; //Markiert als gesetzt
printf("Wort '%s' nach %u Versuchen platziert.\n", words[i], attempt + 1); //Ausgabe als Test
}
}
}
// 4. Restliche Felder mit zufälligen Buchstaben füllen
for (unsigned int row = 0; row < searchFieldLen; row++)
{
for (unsigned int col = 0; col < searchFieldLen; col++)
{
if (salad[row][col] == EMPTY_CHAR)
{
salad[row][col] = 'A' + (rand() % 26); // Zufälliger Buchstabe von A-Z
}
}
}
printf("Placed %u words out of %u\n", placedWords, wordCount);
return placedWords; // Gibt Anzahl gesetzter Wörter zurück
}
+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;
}