Angefagen, zu verschoenern

This commit is contained in:
silvana884 2025-11-05 11:12:54 +01:00
parent 2ba9296d4a
commit ec60cd9994
2 changed files with 27 additions and 27 deletions

View File

@ -26,14 +26,14 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
placedWords = fillSalad(salad, searchFieldLen, words, wordCount, usedWords); placedWords = fillSalad(salad, searchFieldLen, words, wordCount, usedWords);
showWordSalad(salad, searchFieldLen); showWordSalad(salad);
printf("\nPlacedWords: %d\n", placedWords); printf("\nPlacedWords: %d\n", placedWords);
return placedWords; return placedWords;
} }
// 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])
{ {
printf("\nWordSalad:\n"); printf("\nWordSalad:\n");
for(int i=0; i < MAX_SEARCH_FIELD_LEN; i++) for(int i=0; i < MAX_SEARCH_FIELD_LEN; i++)
@ -48,7 +48,7 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
// Decides wether to print horizontally or vertically // Decides wether to print horizontally or vertically
// returns 1, when horizontal // returns 1, when horizontal
int printHorizontal(unsigned int wordCount) int printHorizontal()
{ {
return rand() % 2; return rand() % 2;
} }
@ -70,41 +70,41 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
{ {
int addedWords = 0; int addedWords = 0;
// empties salad // empties salad
for(int i = 0; i < searchFieldLen; i++) { for(unsigned int i = 0; i < searchFieldLen; i++) {
for(int j = 0; j < searchFieldLen; j++) { for(unsigned int j = 0; j < searchFieldLen; j++) {
salad[i][j] = '\0'; salad[i][j] = '\0';
} }
} }
// inserts words // inserts words
for(int w = 0; w < wordCount; w++) { for(unsigned int w = 0; w < wordCount; w++) {
int tries = 0; int tries = 0;
int placed = 0; int placed = 0;
while(tries < MAX_RAND_TRIES_PER_WORD && !placed) { while(tries < MAX_RAND_TRIES_PER_WORD && !placed) {
int numWord = whichWord(words, wordCount, usedWords); int numWord = whichWord(wordCount, usedWords);
if(numWord == -5) { if(numWord == -5) {
// found no unused word // found no unused word
break; break;
} }
int horizontal = printHorizontal(wordCount); int horizontal = printHorizontal();
if(horizontal) { if(horizontal) {
int row = rand() % searchFieldLen; int row = rand() % searchFieldLen;
// checks if words fits // checks if words fits
int wordLen = strlen(words[numWord]); unsigned int wordLen = strlen(words[numWord]);
if(wordLen <= searchFieldLen) { if(wordLen <= searchFieldLen) {
int startCol = rand() % (searchFieldLen - wordLen + 1); int startCol = rand() % (searchFieldLen - wordLen + 1);
int canPlace = 1; int canPlace = 1;
for(int i = 0; i < wordLen; i++) { //checks if word fits for(unsigned int i = 0; i < wordLen; i++) { //checks if word fits
if(salad[row][startCol + i] != '\0') { if(salad[row][startCol + i] != '\0') {
canPlace = 0; canPlace = 0;
break; break;
} }
} }
if(canPlace) { //word fits and is inserted if(canPlace) { //word fits and is inserted
for(int i = 0; i < wordLen; i++) { for(unsigned int i = 0; i < wordLen; i++) {
salad[row][startCol + i] = words[numWord][i]; salad[row][startCol + i] = words[numWord][i];
} }
placed = 1; placed = 1;
@ -118,14 +118,14 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
if(wordLen <= searchFieldLen) { if(wordLen <= searchFieldLen) {
int startRow = rand() % (searchFieldLen - wordLen + 1); int startRow = rand() % (searchFieldLen - wordLen + 1);
int canPlace = 1; int canPlace = 1;
for(int i = 0; i < wordLen; i++) { for(unsigned int i = 0; i < wordLen; i++) {
if(salad[startRow + i][col] != '\0') { if(salad[startRow + i][col] != '\0') {
canPlace = 0; canPlace = 0;
break; break;
} }
} }
if(canPlace) { if(canPlace) {
for(int i = 0; i < wordLen; i++) { for(unsigned int i = 0; i < wordLen; i++) {
salad[startRow + i][col] = words[numWord][i]; salad[startRow + i][col] = words[numWord][i];
} }
placed = 1; placed = 1;
@ -142,7 +142,7 @@ int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned i
} }
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]) int whichWord(unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS])
{ {
int tries = 0; int tries = 0;
@ -178,9 +178,9 @@ int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount, int used
//Fills word in salad and deletes row of words of the used word //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) void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],unsigned int searchFieldLen,const char words[][MAX_WORD_LEN], int numWord,int row)
{ {
int wordLen = strlen(words[numWord]); unsigned int wordLen = strlen(words[numWord]);
// Returns if word longer than game field // Returns if word longer than game field
if (wordLen > searchFieldLen || wordLen == 0) if (wordLen > searchFieldLen || wordLen == 0)
@ -190,14 +190,14 @@ void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],unsign
int startCol = rand() % (searchFieldLen - wordLen + 1); int startCol = rand() % (searchFieldLen - wordLen + 1);
// Determines wether word fits // Determines wether word fits
for (int i = 0; i < wordLen; i++) for (unsigned int i = 0; i < wordLen; i++)
{ {
if (salad[row][startCol + i] != '\0') if (salad[row][startCol + i] != '\0')
{ {
return; return;
} }
} }
for (int i = 0; i < wordLen; i++) for (unsigned int i = 0; i < wordLen; i++)
{ {
salad[row][startCol + i] = words[numWord][i]; salad[row][startCol + i] = words[numWord][i];
} }
@ -205,16 +205,16 @@ void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],unsign
//Fills word in salad and deletes row of words of the used word, vertical from top to bottom //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) void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int numWord, int column)
{ {
int wordLen = strlen(words[numWord]); unsigned int wordLen = strlen(words[numWord]);
if (wordLen > searchFieldLen || wordLen == 0) if (wordLen > searchFieldLen || wordLen == 0)
return; return;
int startRow = rand() % (searchFieldLen - wordLen + 1); int startRow = rand() % (searchFieldLen - wordLen + 1);
for (int i = 0; i < wordLen; i++) for (unsigned int i = 0; i < wordLen; i++)
{ {
if (salad[startRow + i][column] != '\0') if (salad[startRow + i][column] != '\0')
{ {
@ -222,7 +222,7 @@ void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], un
} }
} }
for (int i = 0; i < wordLen; i++) for (unsigned int i = 0; i < wordLen; i++)
{ {
salad[startRow + i][column] = words[numWord][i]; salad[startRow + i][column] = words[numWord][i];
} }

View File

@ -7,13 +7,13 @@
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
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]);
int printHorizontal(unsigned int wordCount); int printHorizontal();
int emptyPlaces(unsigned int wordCount); int emptyPlaces(unsigned int wordCount);
int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]); int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]);
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]); int whichWord(unsigned int wordCount, int usedWords[MAX_NUMBER_OF_WORDS]);
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 fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], 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 fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], int numWord, int column);
void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]); void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);