generated from freudenreichan/info2Praktikum-Wortsalat
added a wordSort function in game.c to reduce failure rate
This commit is contained in:
parent
3d51605223
commit
a95c956cac
@ -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
|
// Choses a random Position for the current Word
|
||||||
wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordLength)
|
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
|
// Creates the word salad by placing words randomly and filling empty spaces
|
||||||
// returnes the number of sucessfully placed words
|
// 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 i = 0;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
@ -165,6 +193,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
int placedWords = 0;
|
int placedWords = 0;
|
||||||
char nullWord[MAX_WORD_LEN];
|
char nullWord[MAX_WORD_LEN];
|
||||||
char currentWord[MAX_WORD_LEN];
|
char currentWord[MAX_WORD_LEN];
|
||||||
|
char sortedWords[maxWordCount][MAX_WORD_LEN];
|
||||||
wordPosition currentWordPosition = {0,0,0};
|
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';
|
nullWord[k] = '\0';
|
||||||
}
|
}
|
||||||
|
for (k = 0; k < wordCount; k++)
|
||||||
|
{
|
||||||
|
strcpy(sortedWords[k], words[k]);
|
||||||
|
}
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
initializeWordsalad(salad, searchFieldLen);
|
initializeWordsalad(salad, searchFieldLen);
|
||||||
|
sortWordList(sortedWords, wordCount, nullWord);
|
||||||
|
|
||||||
for (i = 1; i <= wordCount; i++)
|
for (i = 1; i <= wordCount; i++)
|
||||||
{
|
{
|
||||||
j = 0;
|
j = 0;
|
||||||
|
|
||||||
strcpy(currentWord, nullWord);
|
strcpy(currentWord, nullWord);
|
||||||
strcpy(currentWord, words[i-1]);
|
strcpy(currentWord, sortedWords[i-1]);
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#define MAX_SEARCH_FIELD_LEN 100
|
#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);
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Binary file not shown.
@ -63,7 +63,6 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
}
|
}
|
||||||
|
|
||||||
} while (((currentChar != EOF) && (word <= maxWordCount)));
|
} while (((currentChar != EOF) && (word <= maxWordCount)));
|
||||||
printf("wortzahl: %d\n", word);
|
|
||||||
|
|
||||||
return word;
|
return word;
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@ -41,7 +41,7 @@ int main(int argc, char *argv[])
|
|||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
// Create the word salad by placing words into grid
|
// 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("placed Words: %d\n", placedWords);
|
||||||
printf("wordCount = %d\n", wordCount);
|
printf("wordCount = %d\n", wordCount);
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user