Finished Method createWordSalad()

This commit is contained in:
Tobias Grampp 2025-11-03 11:54:22 +01:00
parent 125723f313
commit e702373347

View File

@ -13,8 +13,8 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren - erledigt /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren - erledigt
* restliche Felder mit zufälligen Buchstaben füllen - erldeigt * restliche Felder mit zufälligen Buchstaben füllen - erldeigt
* Rückgabewert ist Anzahl der platzierten Wörter - erledigt * Rückgabewert ist Anzahl der platzierten Wörter - erledigt
* Es muss gecheckt werden, ob ein anderes platziertes Word fertig ist * Es muss gecheckt werden, ob ein anderes platziertes Word fertig ist - erledigt
* Wenn ein Wort nicht zufällig platziert werden kann, muss es systemtatisch platziert werden!*/ * Wenn ein Wort nicht zufällig platziert werden kann, muss es systemtatisch platziert werden! - erledigt*/
// 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)
@ -30,28 +30,81 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int wordamount = 0; int wordamount = 0;
for(int i = 0; i < wordCount; i++) for(int i = 0; i < wordCount; i++)
{ {
int wordlength = 0; int wordPlaced = 0, amountFailed = 0;
while(words[i][wordlength] != '0')//gets the length of the current word do
wordlength++;
int startPosition1 = RANDOMNUMBER(0, MAX_SEARCH_FIELD_LEN - wordlength);//gets a random starting position for the word
int startPosition2 = RANDOMNUMBER(0, MAX_SEARCH_FIELD_LEN);//gest a second coordinate for the starting position
if(RANDOMNUMBER(0,1) < 1)//Flips a coin whether the number is vertical or horizontal
{ {
for(int j = 0; j < wordlength; j++) int wordlength = 0;
{ while(words[i][wordlength] != '0')//gets the length of the current word
salad[startPosition1 + j][startPosition2] = words[i][j] wordlength++;
int startPosition1 = RANDOMNUMBER(0, MAX_SEARCH_FIELD_LEN - wordlength);//gets a random starting position for the word
int startPosition2 = RANDOMNUMBER(0, MAX_SEARCH_FIELD_LEN);//gest a second coordinate for the starting position
if(RANDOMNUMBER(0,1) < 1)//Flips a coin whether the number is vertical or horizontal
{//Horizontal
int allClear = 0;
for(int j = 0; j < wordlength; j++)//Checks, if there already is a word, where the new word should be placed.
{
if(salad[startPosition1 + j][startPosition2] != 63)
allClear++;
}
if(allClear == 0)//Places the word
{
for(int j = 0; j < wordlength; j++)
{
salad[startPosition1 + j][startPosition2] = words[i][j]
}
wordPlaced++;
wordamount++;
}
else//Notes the failed attempt to place the word
{
amountFailed++;
}
}
else
{//Vertical
int allClear = 0;
for(int j = 0; j < wordlength; j++)//Checks, if there already is a word, where the new word should be placed.
{
if(salad[startPosition2][startPosition1 + j] != 63)
allClear++;
}
if(allClear == 0)//Places the word
{
for(int j = 0; j < wordlength; j++)
{
salad[startPosition2][startPosition1 + j] = words[i][j]
}
wordPlaced++;
wordamount++;
}
else//Notes the failed attempt to place the word
{
amountFailed++;
}
} }
} }
else int index = 0;
while((wordPlaced == 0)&&(index < searchFieldLen))//Places Words that couln't be put in normally in the highest up horizontal place.
{ {
for(int j = 0; j < wordlength; j++) int allClear = 0;
for(int j = 0; j < wordlength; j++)//Checks, if there already is a word, where the new word should be placed.
{ {
salad[startPosition2][startPosition1 + j] = words[i][j] if(salad[index + j][0] != 63)
allClear++;
} }
if(allClear == 0)//Places the word
{
for(int j = 0; j < wordlength; j++)
{
salad[index + j][0] = words[i][j]
}
wordPlaced++;
wordamount++;
}
index++;
} }
wordamount++;
}
}
for(int i = 0; i < searchFieldLen; i++)//Randomly fills the rest of the array for(int i = 0; i < searchFieldLen; i++)//Randomly fills the rest of the array
{ {
for(int j = 0; j < searchFieldLen; j++) for(int j = 0; j < searchFieldLen; j++)