generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
11
Commits
7146174e78
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f0f3ceeb9 | ||
|
|
9ef55c581f | ||
|
|
d00feb8401 | ||
|
|
b26202cd9b | ||
|
|
25a13b1dca | ||
|
|
398936b789 | ||
|
|
cba52d17c5 | ||
|
|
42a5143e9a | ||
|
|
acc14b49d8 | ||
|
|
6027c2a061 | ||
|
|
ed22736f68 |
+44
-14
@@ -31,16 +31,33 @@ typedef struct
|
||||
} wordPosition;
|
||||
|
||||
|
||||
void initializeWordsalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
|
||||
for (i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = '=';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Choses a random Position for the current Word
|
||||
wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordLength)
|
||||
{
|
||||
wordPosition position = {0, 0, 0};
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL;
|
||||
position.rowOrColumn = rand()%(searchFieldLen);
|
||||
position.startingCell = rand()%(searchFieldLen - wordLength);
|
||||
|
||||
|
||||
return position;
|
||||
}
|
||||
|
||||
@@ -91,27 +108,28 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]
|
||||
int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPosition position, const char currentWord[], unsigned int currentWordLen, unsigned int serchFieldLen)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
|
||||
if (position.alignment == HORIZONTAL)
|
||||
{
|
||||
|
||||
for (i = position.startingCell; (i < serchFieldLen) && (i < currentWordLen); i++)
|
||||
for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++)
|
||||
{
|
||||
salad[position.rowOrColumn][i] = currentWord[i];
|
||||
salad[position.rowOrColumn][i] = currentWord[j];
|
||||
j++;
|
||||
}
|
||||
salad[position.rowOrColumn][i] = '\0';
|
||||
}
|
||||
else if (position.alignment == VERTIKAL)
|
||||
{
|
||||
for (i = position.startingCell; (i < serchFieldLen) && (i < currentWordLen); i++)
|
||||
for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++)
|
||||
{
|
||||
salad[i][position.rowOrColumn] = currentWord[i];
|
||||
salad[i][position.rowOrColumn] = currentWord[j];
|
||||
j++;
|
||||
}
|
||||
salad[i][position.rowOrColumn] = '\0';
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -123,11 +141,9 @@ void placeRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
int j = 0;
|
||||
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
for (i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (j = 0; i < searchFieldLen; j++)
|
||||
for (j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if ((salad[i][j] < 'A') || (salad[i][j] > 'z') || ((salad[i][j] < 'a') && (salad[i][j] > 'Z')))
|
||||
{
|
||||
@@ -144,24 +160,38 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = 0;
|
||||
int positionFound = 0;
|
||||
int placedWords = 0;
|
||||
char nullWord[MAX_WORD_LEN];
|
||||
char currentWord[MAX_WORD_LEN];
|
||||
wordPosition currentWordPosition = {0,0,0};
|
||||
|
||||
|
||||
for (k = 0; k < MAX_WORD_LEN; k++)
|
||||
{
|
||||
nullWord[k] = '\0';
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
initializeWordsalad(salad, searchFieldLen);
|
||||
|
||||
for (i = 1; i <= wordCount; i++)
|
||||
{
|
||||
j = 0;
|
||||
|
||||
strcpy(currentWord, nullWord);
|
||||
strcpy(currentWord, words[i-1]);
|
||||
|
||||
do
|
||||
{
|
||||
currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i]));
|
||||
positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(words[i]), searchFieldLen);
|
||||
currentWordPosition = choserandomPosition(searchFieldLen, strlen(currentWord));
|
||||
positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(currentWord), searchFieldLen);
|
||||
j++;
|
||||
} while ((j < MAX_RAND_TRIES_PER_WORD) && !(positionFound));
|
||||
|
||||
if (positionFound)
|
||||
{
|
||||
strcpy(currentWord, words[i-1]);
|
||||
placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen);
|
||||
}
|
||||
}
|
||||
@@ -182,7 +212,7 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
|
||||
for (i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (j = 0; i < searchFieldLen; j++)
|
||||
for (j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
printf("%c", salad[i][j]);
|
||||
}
|
||||
|
||||
Binary file not shown.
+10
-2
@@ -32,8 +32,11 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
|
||||
do
|
||||
{
|
||||
// get the next char from the file
|
||||
currentChar = fgetc(file);
|
||||
|
||||
// if current char is a capital letter
|
||||
// write it into the words array
|
||||
if ((currentChar >= 'A') && (currentChar <= 'Z'))
|
||||
{
|
||||
if (!currentlyInWord)
|
||||
@@ -44,6 +47,9 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
words[word][wordChar] = currentChar;
|
||||
wordChar++;
|
||||
}
|
||||
// if the current char is a lowercase letter
|
||||
// change it to its capital version
|
||||
// and write it into the words array
|
||||
else if ((currentChar >= 'a') && (currentChar <= 'z'))
|
||||
{
|
||||
if (!currentlyInWord)
|
||||
@@ -54,6 +60,9 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
words[word][wordChar] = toupper(currentChar);
|
||||
wordChar++;
|
||||
}
|
||||
// if the current char is NOT a letter
|
||||
// the end of a word has been reached
|
||||
// skip chars untill a new letter is found
|
||||
else if (currentlyInWord)
|
||||
{
|
||||
words[word][wordChar] = '\0';
|
||||
@@ -62,8 +71,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
currentlyInWord = 0;
|
||||
}
|
||||
|
||||
} while ((currentChar != EOF) && (word < maxWordCount));
|
||||
|
||||
} while (((currentChar != EOF) && (word <= maxWordCount)));
|
||||
|
||||
return word;
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -43,6 +43,8 @@ int main(int argc, char *argv[])
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
printf("placed Words: %d\n", placedWords);
|
||||
printf("wordCount = %d\n", wordCount);
|
||||
// DONE:
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Hund,Katze; Maus
|
||||
@@ -0,0 +1,3 @@
|
||||
Apfel
|
||||
Banane
|
||||
Kiwi
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user