Compare commits

...

2 Commits

3 changed files with 80 additions and 30 deletions

View File

@ -2,7 +2,6 @@
#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
@ -14,48 +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)
{ {
// Initialisiere Salad mit EMPTY_CHAR if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN) {
for(unsigned int i = 0; i < searchFieldLen; i++) return 0;
{ }
for(unsigned int j = 0; j < searchFieldLen; j++)
{ // Start with empty field
salad[i][j] = EMPTY_CHAR; for (unsigned int r = 0; r < searchFieldLen; ++r) {
for (unsigned int c = 0; c < searchFieldLen; ++c) {
salad[r][c] = EMPTY_CHAR;
} }
} }
// Platziere Wörter (einfache horizontale Platzierung für Tests) // Platzieren der Wörter
unsigned int placedCount = 0; for (unsigned int w = 0; w < wordCount; ++w) {
for(unsigned int w = 0; w < wordCount && w < searchFieldLen; w++) const char *word = words[w];
{ size_t wordLen = strlen(word);
unsigned int wordLen = strlen(words[w]); if (wordLen == 0 || wordLen > searchFieldLen) {
if(wordLen <= searchFieldLen) continue;
{
// Platziere Wort horizontal in Zeile w
for(unsigned int j = 0; j < wordLen; j++)
{
salad[w][j] = words[w][j];
} }
placedCount++;
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;
} }
} }
// Fülle restliche Felder mit zufälligen Buchstaben // Rest mit zufälligen Buchstaben füllen
for(unsigned int i = 0; i < searchFieldLen; i++) for (unsigned int r = 0; r < searchFieldLen; ++r) {
{ for (unsigned int c = 0; c < searchFieldLen; ++c) {
for(unsigned int j = 0; j < searchFieldLen; j++) if (salad[r][c] == EMPTY_CHAR) {
{ salad[r][c] = 'A' + (rand() % 26);
if(salad[i][j] == EMPTY_CHAR)
{
salad[i][j] = 'A' + (rand() % 26);
} }
} }
} }
return placedCount; 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');
}
}

View File

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