create Wordsalad gebaut.
This commit is contained in:
parent
10af0980d3
commit
190b40468f
@ -14,16 +14,25 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
Position position[1520]; //Positionsarray, 1520, weil 2 Richtungen, minimalwort größe
|
Position position[(2 * MAX_SEARCH_FIELD_LEN * (MAX_SEARCH_FIELD_LEN - MIN_WORD_LEN + 1))]; //Positionsarray Größe: Formel max. nötige Größe (für minimal großes wort)
|
||||||
|
int gesetzteWörter = 0;
|
||||||
|
clearWordSalad(salad, searchFieldLen);
|
||||||
|
gesetzteWörter = fuelleSalatMitWörtern(salad, searchFieldLen, words, position, wordCount);
|
||||||
|
fillWordsaladRand(salad, searchFieldLen);
|
||||||
|
return gesetzteWörter;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for(int k = 0; k < searchFieldLen; k++)
|
||||||
|
{
|
||||||
|
printf("%c", salad[i][k]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//Logik für createWordSalad: Ein Wort nach dem anderen bis entweder alle Wörter drinnen,
|
//Logik für createWordSalad: Ein Wort nach dem anderen bis entweder alle Wörter drinnen,
|
||||||
@ -53,7 +62,11 @@ void fillWordsaladRand(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], u
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//hierbei: alle Positionen werden zunächst geprüft ob leer (.) danach wird nach rechts und nach unten geschaut ob:
|
||||||
|
//a.) genügend Platz wäre, dass nicht aus dem array herausgeschrieben wird und
|
||||||
|
//b.) alle zu besetzenden Felder leer sind (.).
|
||||||
|
//Positionamount gibt aus wie viele mögliche Positionen es für dieses Wort gibt.
|
||||||
|
//Wird mit 0 initialisiert. Wenns null bleibt, bedeutet dies es gibt keine verfügbaren Positionen.
|
||||||
int findPossiblePositions(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int wordidx, Position positions[])
|
int findPossiblePositions(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int wordidx, Position positions[])
|
||||||
{
|
{
|
||||||
char *word = words[wordidx];
|
char *word = words[wordidx];
|
||||||
@ -94,7 +107,7 @@ int findPossiblePositions(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(rechtsfrei)
|
if(rechtsfrei) //Positions array wird mit gültigen Positionen und Richtung befüllt.
|
||||||
{
|
{
|
||||||
positions[positionamount].x = k;
|
positions[positionamount].x = k;
|
||||||
positions[positionamount].y = i;
|
positions[positionamount].y = i;
|
||||||
@ -125,13 +138,13 @@ int fuelleSalatMitWörtern(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN
|
|||||||
int positionsamount;
|
int positionsamount;
|
||||||
int positiongewählt;
|
int positiongewählt;
|
||||||
char *word;
|
char *word;
|
||||||
for(int i = 0; i < wordcount; i++)
|
for(int i = 0; i < wordcount; i++) //jedes Wort.
|
||||||
{
|
{
|
||||||
positionsamount = findPossiblePositions(salad, searchFieldLen, words, i, position);
|
positionsamount = findPossiblePositions(salad, searchFieldLen, words, i, position); //für dieses Wort werden die Positionen gefunden
|
||||||
if(positionsamount > 0)
|
if(positionsamount > 0)//es gibt Positionen
|
||||||
{
|
{
|
||||||
gesetzteWörter++;
|
gesetzteWörter++;
|
||||||
positiongewählt = rand() % positionsamount;
|
positiongewählt = rand() % positionsamount; //die wie vielte Position des Positionsarrays für dieses Wort wir nehmen
|
||||||
if(position[positiongewählt].richtung == RECHTS)
|
if(position[positiongewählt].richtung == RECHTS)
|
||||||
{
|
{
|
||||||
word = words[i];
|
word = words[i];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user