Wörter werden in Gitter eingefügt und Gitter ist auf console ausgebbar

This commit is contained in:
Bastian 2025-10-25 15:38:37 +02:00
parent c5501c1bd3
commit cf584df1b6

View File

@ -11,27 +11,52 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */ * restliche Felder mit zufälligen Buchstaben füllen */
int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[])
{
for(size_t i = 0 ; i < strlen(wort); i++ )
{
short xoffset = (richtung) ? i : 0;
short yoffset = (richtung) ? 0 : i;
if (!(salad[x+xoffset][y+yoffset]=='#' || salad[x+xoffset][y+yoffset]==wort[i]))
{
return(-1);
}
}
return(1);
}
// 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)
{ {
for(wordCount;wordCount>0;wordCount--) for(short i = searchFieldLen; i>0; i--)
{ {
size_t platzbedarf = strlen(words[wordCount-1]); for(short j = searchFieldLen; j>0; j--)
{
salad[i][j]= "#";
}
}
for(short i = wordCount;i>0;i--)
{
size_t platzbedarf = strlen(words[i-1]);
if (platzbedarf >= searchFieldLen) if (platzbedarf >= searchFieldLen)
{ {
printf("%s konnte nicht eingefuegt werden da es groeßer als das Feld ist",words[wordCount-1]); printf("%s konnte nicht eingefuegt werden da es groeßer als das Feld ist",words[wordCount-1]);
continue; continue;
} }
srand(time(NULL));
short x,y,waagrecht;
do do
{ {
srand(time(NULL));
short max = searchFieldLen - platzbedarf; short max = searchFieldLen - platzbedarf;
short position = rand() % (max +1); short position1 = rand() % (max +1);
srand(time(NULL)); short position2 = rand() % (searchFieldLen);
short waagrecht = rand()%2; waagrecht = rand()%2;
x = (waagrecht) ? position1:position2;
y = (waagrecht) ? position2:position1;
}while(1); }while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1);
} }
@ -42,5 +67,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
// 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 y = 0; y < searchFieldLen; y++) {
for (unsigned int x = 0; x < searchFieldLen; x++) {
printf("%c ", salad[x][y]); // oder printf("%c", salad[x][y]); ohne Leerzeichen
}
printf("\n"); // neue Zeile nach jeder Zeile
}
} }