generated from freudenreichan/info2Praktikum-Wortsalat
Neue Struktur mit versch. Funktionen
This commit is contained in:
parent
82fafb02fd
commit
eb67d72627
@ -16,14 +16,7 @@
|
|||||||
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)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
for(int i = 0; i < MAX_NUMBER_OF_WORDS; i++) //geht Wortliste durch und fuellt die Woerter erstmal in salad
|
|
||||||
{
|
|
||||||
if(word[i][0] == NULL)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
int rand = rand() % (wordCount - (-wordCount) + 1) - wordCount;
|
|
||||||
int value = (rand*rand) / rand;
|
int value = (rand*rand) / rand;
|
||||||
|
|
||||||
if(rand < 0) //wenn rand eine negative Zahl ist, soll das Wort horizontal platziert werden
|
if(rand < 0) //wenn rand eine negative Zahl ist, soll das Wort horizontal platziert werden
|
||||||
@ -77,3 +70,102 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decides wether to print horizontally or vertically
|
||||||
|
// returns 1, when horizontal
|
||||||
|
int printHorizontal(unsigned int wordCount)
|
||||||
|
{
|
||||||
|
int rand = rand() % (wordCount - (-wordCount) + 1) - wordCount;
|
||||||
|
if(rand > 0)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// checks wether after all words are filled, empty spaces are left
|
||||||
|
// returns 1, when empty spaces are left
|
||||||
|
int emptyPlaces(unsigned int wordCount)
|
||||||
|
{
|
||||||
|
if(wordCount < MAX_SEARCH_FIELD_LEN)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// fills salad array with max. words from word array either horizontally or vertically
|
||||||
|
void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
|
{
|
||||||
|
for(int i= 0; i< searchFieldLen; i++) //for every word in salad, it is determined wether hor or ver
|
||||||
|
{
|
||||||
|
if(printHorizontal(wordCount)) //horizontal
|
||||||
|
{
|
||||||
|
for(int j= 0; j < searchFieldLen; j++) //sucht freie Zeile in salad
|
||||||
|
{
|
||||||
|
if(salad[j][0] == NULL)
|
||||||
|
{
|
||||||
|
int numWord = whichWord(words, wordCount); //determines, which word will be written in salad
|
||||||
|
fillWordinRow(salad, searchFieldLen, words, wordCount, numWord, j); // fills word horizontally in salad and deletes the word afterwards
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{ //vertical, immer von oben nach unten
|
||||||
|
for(int j = 0; j< searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
if(salad[0][j] == NULL)
|
||||||
|
{
|
||||||
|
int numWord = whichWord(words, wordCount);
|
||||||
|
fillWordinColumn(salad, searchFieldLen, words, wordCount, numWord, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determines, which word from words is filled into salad
|
||||||
|
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
|
{
|
||||||
|
while(true){
|
||||||
|
int numWord = rand()% (wordCount + 1);
|
||||||
|
if(words[numWord][0] != NULL)
|
||||||
|
{
|
||||||
|
return numWord; //return random row, which has not been used yet
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Fills word in salad and deletes row of words of the used word
|
||||||
|
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int row)
|
||||||
|
{
|
||||||
|
|
||||||
|
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++)
|
||||||
|
{
|
||||||
|
for(int g = 0; g < MAX_WORD_LEN; g++)
|
||||||
|
{
|
||||||
|
salad[row][h] = words[numWord][g];
|
||||||
|
words[numWord][g] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Fills word in salad and deletes row of words of the used word, vertical from top to bottom
|
||||||
|
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int column)
|
||||||
|
{
|
||||||
|
for(int h = 0; h< MAX_SEARCH_FIELD_LEN; h++)
|
||||||
|
{
|
||||||
|
for(int g = 0; g < MAX_WORD_LEN; g++)
|
||||||
|
{
|
||||||
|
salad[h][column] = word[numWord][g];
|
||||||
|
words[numWord][g] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if emptyPlaces, fill these with random letters
|
||||||
|
void fillRandom()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user