Compare commits

..

No commits in common. "15f5dfbd40b28b68acc6bddf5cb5f3a0f0fc4f9c" and "aed3cce12e21a58e090e213f12e0d1fe29075922" have entirely different histories.

3 changed files with 30 additions and 80 deletions

View File

@ -2,6 +2,7 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
@ -13,95 +14,48 @@
// Creates the word salad by placing words randomly and filling empty spaces // 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], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{ {
if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN) { // Initialisiere Salad mit EMPTY_CHAR
return 0; for(unsigned int i = 0; i < searchFieldLen; i++)
} {
for(unsigned int j = 0; j < searchFieldLen; j++)
// Start with empty field {
for (unsigned int r = 0; r < searchFieldLen; ++r) { salad[i][j] = EMPTY_CHAR;
for (unsigned int c = 0; c < searchFieldLen; ++c) {
salad[r][c] = EMPTY_CHAR;
} }
} }
// Platzieren der Wörter // Platziere Wörter (einfache horizontale Platzierung für Tests)
for (unsigned int w = 0; w < wordCount; ++w) { unsigned int placedCount = 0;
const char *word = words[w]; for(unsigned int w = 0; w < wordCount && w < searchFieldLen; w++)
size_t wordLen = strlen(word); {
if (wordLen == 0 || wordLen > searchFieldLen) { unsigned int wordLen = strlen(words[w]);
continue; if(wordLen <= searchFieldLen)
} {
// Platziere Wort horizontal in Zeile w
int placed = 0; for(unsigned int j = 0; j < wordLen; j++)
for (int attempts = 0; attempts < MAX_RAND_TRIES_PER_WORD && !placed; ++attempts) { {
int vertical = rand() % 2; // 0=horizontal, 1=vertical salad[w][j] = words[w][j];
unsigned int row, col;
if (vertical) {
row = rand() % (searchFieldLen - wordLen + 1);
col = rand() % searchFieldLen;
} else {
row = rand() % searchFieldLen;
col = rand() % (searchFieldLen - wordLen + 1);
} }
placedCount++;
// Check if placeable
int ok = 1;
for (size_t i = 0; i < wordLen; ++i) {
unsigned int r = row + (vertical ? i : 0);
unsigned int c = col + (vertical ? 0 : i);
char current = salad[r][c];
if (current != EMPTY_CHAR && current != word[i]) {
ok = 0;
break;
}
}
if (!ok) {
continue;
}
// Set letters
for (size_t i = 0; i < wordLen; ++i) {
unsigned int r = row + (vertical ? i : 0);
unsigned int c = col + (vertical ? 0 : i);
salad[r][c] = word[i];
}
placed = 1;
}
if (!placed) {
// Ein Wort konnte nicht platziert werden, Abbruch möglich
return 0;
} }
} }
// Rest mit zufälligen Buchstaben füllen // Fülle restliche Felder mit zufälligen Buchstaben
for (unsigned int r = 0; r < searchFieldLen; ++r) { for(unsigned int i = 0; i < searchFieldLen; i++)
for (unsigned int c = 0; c < searchFieldLen; ++c) { {
if (salad[r][c] == EMPTY_CHAR) { for(unsigned int j = 0; j < searchFieldLen; j++)
salad[r][c] = 'A' + (rand() % 26); {
if(salad[i][j] == EMPTY_CHAR)
{
salad[i][j] = 'A' + (rand() % 26);
} }
} }
} }
return 1; return placedCount;
} }
// Prints the word salad to console // 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 (unsigned int r = 0; r < searchFieldLen; ++r) {
for (unsigned int c = 0; c < searchFieldLen; ++c) {
char ch = salad[r][c];
if (ch == EMPTY_CHAR) {
ch = '.';
}
putchar(ch);
if (c + 1 < searchFieldLen) {
putchar(' ');
}
}
putchar('\n');
}
}
}

View File

@ -1,5 +1 @@
Yeti,Nessie Werwolf; Vampir Yeti Nessie Werwolf Vampir Monster Hydra Frankenstein Dracula KingKong Gremlin Kobold Hexe Poltergeist Gespenst Oger
Monster
Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
Gespenst, Oger

Binary file not shown.