Vorlaeufig fertiger COde, TESTEN!

This commit is contained in:
silvana884 2025-11-03 11:14:20 +01:00
parent eb67d72627
commit 197c9cb5e7
2 changed files with 50 additions and 51 deletions

View File

@ -17,58 +17,23 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
{ {
srand(time(NULL)); srand(time(NULL));
int value = (rand*rand) / rand; fillSalad(salad, searchFieldLen, words, wordCount);
showWordSalad(salad, searchFieldLen);
if(rand < 0) //wenn rand eine negative Zahl ist, soll das Wort horizontal platziert werden return 1;
{
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) // geht salad durch
{
if(salad[j][i] == NULL) //sucht horizontal nach einer freien Zeile in salad
{
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++) //kopiert die Zeichen in word in die Zeile in salad
{
salad[j][h] = word[value][h]; //value ist der Wert des zufaelligen Wortes, also der Platz im word Array
}
}
}
}
else //vertikal platzieren
{
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) // geht salad durch
{
if(salad[i][j] == NULL) //sucht vertikal nach einer freien Zeile in salad
{
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++) //kopiert die Zeichen in word in die Spalte in salad
{
salad[h][j] = word[value][h]; //value ist der Wert des zufaelligen Wortes, also der Platz im word Array
}
}
}
}
}
//jetzt ist die Wortliste leer oder es wurden alle Worte in Salad geschrieben.
//Restliche Plaetze mit random Buchstaben fuellen:
int num = rand()%(26 - 1 + 1) - 1;
char letter = 64 + num;
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
if(salad[i][j] == NULL)
{
salad[i][j] = letter;
}
}
}
} }
// 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)
{ {
printf("\nWordSalad:\n");
for(int i=0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for(int j=0; j< MAX_SEARCH_FIELD_LEN; j++)
{
printf("%c\t", salad[i][j]);
}
printf("\n");
}
} }
// Decides wether to print horizontally or vertically // Decides wether to print horizontally or vertically
@ -107,8 +72,13 @@ void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned
if(salad[j][0] == NULL) if(salad[j][0] == NULL)
{ {
int numWord = whichWord(words, wordCount); //determines, which word will be written in salad int numWord = whichWord(words, wordCount); //determines, which word will be written in salad
if(numWord != -5){
fillWordinRow(salad, searchFieldLen, words, wordCount, numWord, j); // fills word horizontally in salad and deletes the word afterwards fillWordinRow(salad, searchFieldLen, words, wordCount, numWord, j); // fills word horizontally in salad and deletes the word afterwards
} }
else{
fillRandom(salad, searchFieldLen, words, wordCount, emptyPlaces(wordCount));
}
}
} }
} }
else{ //vertical, immer von oben nach unten else{ //vertical, immer von oben nach unten
@ -117,8 +87,13 @@ void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned
if(salad[0][j] == NULL) if(salad[0][j] == NULL)
{ {
int numWord = whichWord(words, wordCount); int numWord = whichWord(words, wordCount);
if(numWord != -5){
fillWordinColumn(salad, searchFieldLen, words, wordCount, numWord, j); fillWordinColumn(salad, searchFieldLen, words, wordCount, numWord, j);
} }
else{
fillRandom(salad, searchFieldLen, words, wordCount, emptyPlaces(wordCount));
}
}
} }
} }
} }
@ -127,12 +102,13 @@ void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned
// Determines, which word from words is filled into salad // Determines, which word from words is filled into salad
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount) int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount)
{ {
while(true){ for(int i = 0; i< MAX_NUMBERS_OF_WORDS; i++){
int numWord = rand()% (wordCount + 1); int numWord = rand()% (wordCount + 1);
if(words[numWord][0] != NULL) if(words[numWord][0] != NULL)
{ {
return numWord; //return random row, which has not been used yet return numWord; //return random row, which has not been used yet
} }
return -5;
} }
} }
@ -165,7 +141,21 @@ void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], un
} }
} }
// if emptyPlaces, fill these with random letters // if emptyPlaces, fill these with random letters
void fillRandom() void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int empty)
{ {
if(empty)
{
for(int j=0; j < MAX_SEARCH_FIELD_LEN; j++)
{
for(int h= 0; h < MAX_SEARCH_FIELD_LEN; h++)
{
if(salad[j][h] == NULL)
{
int num = rand()%(26 - 1 + 1) - 1;
char letter = 64 + num;
salad[j][h] = letter;
}
}
}
}
} }

View File

@ -7,5 +7,14 @@
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);
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);
int printHorizontal(unsigned int wordCount);
int emptyPlaces(unsigned int wordCount);
void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount);
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);
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);
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int empty);
#endif #endif