added a wordSort function in game.c to reduce failure rate

This commit is contained in:
Jonas Hofmann 2025-11-04 20:02:26 +01:00
parent 3d51605223
commit a95c956cac
8 changed files with 41 additions and 8 deletions

View File

@ -47,6 +47,34 @@ void initializeWordsalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
}
void sortWordList(char words[][MAX_WORD_LEN], unsigned int wordCount, char nullWord[MAX_WORD_LEN])
{
int i = 0;
int j = 0;
char wordBuffer[MAX_WORD_LEN];
strcpy(wordBuffer, nullWord);
for (j = wordCount - 1; j > 0; j--)
{
for (i = 0; i < j; i++)
{
if (strlen(words[i]) < strlen(words[j]))
{
strcpy(wordBuffer, words[j]);
strcpy(words[j], nullWord);
strcpy(words[j], words[i]);
strcpy(words[i], nullWord);
strcpy(words[i], wordBuffer);
strcpy(wordBuffer, nullWord);
}
}
}
}
// Choses a random Position for the current Word
wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordLength)
{
@ -156,7 +184,7 @@ void placeRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
// Creates the word salad by placing words randomly and filling empty spaces
// returnes the number of sucessfully placed words
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, unsigned int maxWordCount)
{
int i = 0;
int j = 0;
@ -165,6 +193,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int placedWords = 0;
char nullWord[MAX_WORD_LEN];
char currentWord[MAX_WORD_LEN];
char sortedWords[maxWordCount][MAX_WORD_LEN];
wordPosition currentWordPosition = {0,0,0};
@ -172,16 +201,21 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
{
nullWord[k] = '\0';
}
for (k = 0; k < wordCount; k++)
{
strcpy(sortedWords[k], words[k]);
}
srand(time(NULL));
initializeWordsalad(salad, searchFieldLen);
initializeWordsalad(salad, searchFieldLen);
sortWordList(sortedWords, wordCount, nullWord);
for (i = 1; i <= wordCount; i++)
{
j = 0;
strcpy(currentWord, nullWord);
strcpy(currentWord, words[i-1]);
strcpy(currentWord, sortedWords[i-1]);
do
{
@ -195,7 +229,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen);
}
}
placeRandomLetters(salad, searchFieldLen);

View File

@ -5,7 +5,7 @@
#define MAX_SEARCH_FIELD_LEN 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, unsigned int maxWordCount);
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
#endif

Binary file not shown.

View File

@ -63,7 +63,6 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
}
} while (((currentChar != EOF) && (word <= maxWordCount)));
printf("wortzahl: %d\n", word);
return word;
}

Binary file not shown.

View File

@ -41,7 +41,7 @@ int main(int argc, char *argv[])
fclose(file);
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount, MAX_NUMBER_OF_WORDS);
printf("placed Words: %d\n", placedWords);
printf("wordCount = %d\n", wordCount);

Binary file not shown.

Binary file not shown.