word.txt zurückgesetzt, damit es mit der Originalversion übereinstimmt.
This commit is contained in:
parent
208e35e934
commit
b8074e6751
@ -13,11 +13,95 @@
|
|||||||
// 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) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start with empty field
|
||||||
|
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||||
|
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||||
|
salad[r][c] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Platzieren der Wörter
|
||||||
|
for (unsigned int w = 0; w < wordCount; ++w) {
|
||||||
|
const char *word = words[w];
|
||||||
|
size_t wordLen = strlen(word);
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placed = 0;
|
||||||
|
for (int attempts = 0; attempts < MAX_RAND_TRIES_PER_WORD && !placed; ++attempts) {
|
||||||
|
int vertical = rand() % 2; // 0=horizontal, 1=vertical
|
||||||
|
unsigned int row, col;
|
||||||
|
|
||||||
|
if (vertical) {
|
||||||
|
row = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
col = rand() % searchFieldLen;
|
||||||
|
} else {
|
||||||
|
row = rand() % searchFieldLen;
|
||||||
|
col = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||||
|
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||||
|
if (salad[r][c] == EMPTY_CHAR) {
|
||||||
|
salad[r][c] = 'A' + (rand() % 26);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1 +1,5 @@
|
|||||||
Yeti Nessie Werwolf Vampir Monster Hydra Frankenstein Dracula KingKong Gremlin Kobold Hexe Poltergeist Gespenst Oger
|
Yeti,Nessie Werwolf; Vampir
|
||||||
|
Monster
|
||||||
|
Hydra;Frankenstein
|
||||||
|
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||||
|
Gespenst, Oger
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user