This commit is contained in:
Kruschat 2025-10-28 18:45:25 +01:00
commit 1b4d5e1e0b
4 changed files with 96 additions and 1 deletions

View File

@ -6,6 +6,8 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
@ -13,7 +15,100 @@
// 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 direction = 0;
int step = 0;
int added = 0;
int addedWords = 0;
int positionX = 0;
int positionY = 0;
srand(time(NULL));
for(int i = 0; i < searchFieldLen; i++)
{
for(int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = EMPTY_CHAR;
}
}
while(step <= wordCount)
{
int tries = 0;
int wordLength = 0;
direction = rand() % 1;
for (int i = 0; i < MAX_WORD_LEN; i++)
{
if (words[step][i] == '\0')
break;
wordLength++;
}
if(direction == 0)
{
while(added <= 0 && tries < MAX_RAND_TRIES_PER_WORD){
positionY = rand() % searchFieldLen-1;
positionX = rand() % searchFieldLen-1;
if(searchFieldLen - positionX >= wordLength)
{
for(int i = 0; i < searchFieldLen; i++)
{
if(salad[positionY][positionX+i] != EMPTY_CHAR)
{
tries++;
break;
}
for (int j = 0; j < searchFieldLen; j++)
{
salad[positionY][positionX+j] = words[step][j];
added = 1;
}
}
}
else
{
tries++;
}
}
step++;
tries = 0;
added = 0;
}
if(direction == 1)
{
while(added <= 0 && tries < MAX_RAND_TRIES_PER_WORD){
positionY = rand() % searchFieldLen-1;
positionX = rand() % searchFieldLen-1;
if(searchFieldLen - positionY >= wordLength)
{
for(int i = 0; i < searchFieldLen; i++)
{
if(salad[positionY+i][positionX] != EMPTY_CHAR)
{
tries++;
break;
}
for (int j = 0; j < searchFieldLen; j++)
{
salad[positionY+j][positionX] = words[step][j];
added = 1;
}
}
}
else {
tries++;
}
step++;
tries = 0;
added = 0;
}
}
return addedWords;
}
// Prints the word salad to console

Binary file not shown.

View File

@ -14,7 +14,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
char* token;
while(fgets(zeile, MAX_LINE_LEN, file)!=NULL)
{
for (int i = 0; i < 1024; i++)
for (int i = 0; i < MAX_LINE_LEN; i++)
{
if (zeile[i] >= 'a'&&zeile[i] <= 'z')
{

Binary file not shown.