generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
9
Commits
6027c2a061
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f0f3ceeb9 | ||
|
|
9ef55c581f | ||
|
|
d00feb8401 | ||
|
|
b26202cd9b | ||
|
|
25a13b1dca | ||
|
|
398936b789 | ||
|
|
cba52d17c5 | ||
|
|
42a5143e9a | ||
|
|
acc14b49d8 |
+19
-15
@@ -52,11 +52,12 @@ wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordL
|
|||||||
{
|
{
|
||||||
wordPosition position = {0, 0, 0};
|
wordPosition position = {0, 0, 0};
|
||||||
|
|
||||||
//srand(time(NULL));
|
|
||||||
position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL;
|
position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL;
|
||||||
position.rowOrColumn = rand()%(searchFieldLen);
|
position.rowOrColumn = rand()%(searchFieldLen);
|
||||||
position.startingCell = rand()%(searchFieldLen - wordLength);
|
position.startingCell = rand()%(searchFieldLen - wordLength);
|
||||||
|
|
||||||
|
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,25 +113,22 @@ int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPositi
|
|||||||
|
|
||||||
if (position.alignment == HORIZONTAL)
|
if (position.alignment == HORIZONTAL)
|
||||||
{
|
{
|
||||||
for (i = position.startingCell; (i < serchFieldLen) || (j < currentWordLen); i++)
|
for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++)
|
||||||
{
|
{
|
||||||
salad[position.rowOrColumn][i] = currentWord[j];
|
salad[position.rowOrColumn][i] = currentWord[j];
|
||||||
printf("%c",currentWord[j]);
|
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
salad[position.rowOrColumn][i] = '\0';
|
salad[position.rowOrColumn][i] = '\0';
|
||||||
}
|
}
|
||||||
else if (position.alignment == VERTIKAL)
|
else if (position.alignment == VERTIKAL)
|
||||||
{
|
{
|
||||||
for (i = position.startingCell; (i < serchFieldLen) || (j < currentWordLen); i++)
|
for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++)
|
||||||
{
|
{
|
||||||
salad[i][position.rowOrColumn] = currentWord[j];
|
salad[i][position.rowOrColumn] = currentWord[j];
|
||||||
printf("%c",currentWord[j]);
|
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
salad[i][position.rowOrColumn] = '\0';
|
salad[i][position.rowOrColumn] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -162,36 +160,42 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
|
int k = 0;
|
||||||
int positionFound = 0;
|
int positionFound = 0;
|
||||||
int placedWords = 0;
|
int placedWords = 0;
|
||||||
|
char nullWord[MAX_WORD_LEN];
|
||||||
char currentWord[MAX_WORD_LEN];
|
char currentWord[MAX_WORD_LEN];
|
||||||
wordPosition currentWordPosition = {0,0,0};
|
wordPosition currentWordPosition = {0,0,0};
|
||||||
|
|
||||||
|
|
||||||
|
for (k = 0; k < MAX_WORD_LEN; k++)
|
||||||
|
{
|
||||||
|
nullWord[k] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
initializeWordsalad(salad, searchFieldLen);
|
initializeWordsalad(salad, searchFieldLen);
|
||||||
showWordSalad(salad, searchFieldLen);
|
|
||||||
|
|
||||||
for (i = 1; i <= wordCount; i++)
|
for (i = 1; i <= wordCount; i++)
|
||||||
{
|
{
|
||||||
j = 0;
|
j = 0;
|
||||||
|
|
||||||
|
strcpy(currentWord, nullWord);
|
||||||
|
strcpy(currentWord, words[i-1]);
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i]));
|
currentWordPosition = choserandomPosition(searchFieldLen, strlen(currentWord));
|
||||||
positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(words[i]), searchFieldLen);
|
positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(currentWord), searchFieldLen);
|
||||||
j++;
|
j++;
|
||||||
} while ((j < MAX_RAND_TRIES_PER_WORD) && !(positionFound));
|
} while ((j < MAX_RAND_TRIES_PER_WORD) && !(positionFound));
|
||||||
|
|
||||||
if (positionFound)
|
if (positionFound)
|
||||||
{
|
{
|
||||||
strcpy(currentWord, "\0");
|
|
||||||
strcpy(currentWord, words[i-1]);
|
|
||||||
|
|
||||||
printf("%d, %d, %d \n", currentWordPosition.alignment, currentWordPosition.rowOrColumn, currentWordPosition.startingCell);
|
|
||||||
printf("%s\n", currentWord);
|
|
||||||
placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen);
|
placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
showWordSalad(salad, searchFieldLen);
|
|
||||||
placeRandomLetters(salad, searchFieldLen);
|
placeRandomLetters(salad, searchFieldLen);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
+10
-2
@@ -32,8 +32,11 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
// get the next char from the file
|
||||||
currentChar = fgetc(file);
|
currentChar = fgetc(file);
|
||||||
|
|
||||||
|
// if current char is a capital letter
|
||||||
|
// write it into the words array
|
||||||
if ((currentChar >= 'A') && (currentChar <= 'Z'))
|
if ((currentChar >= 'A') && (currentChar <= 'Z'))
|
||||||
{
|
{
|
||||||
if (!currentlyInWord)
|
if (!currentlyInWord)
|
||||||
@@ -44,6 +47,9 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
words[word][wordChar] = currentChar;
|
words[word][wordChar] = currentChar;
|
||||||
wordChar++;
|
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'))
|
else if ((currentChar >= 'a') && (currentChar <= 'z'))
|
||||||
{
|
{
|
||||||
if (!currentlyInWord)
|
if (!currentlyInWord)
|
||||||
@@ -54,8 +60,11 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
words[word][wordChar] = toupper(currentChar);
|
words[word][wordChar] = toupper(currentChar);
|
||||||
wordChar++;
|
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)
|
else if (currentlyInWord)
|
||||||
{
|
{
|
||||||
words[word][wordChar] = '\0';
|
words[word][wordChar] = '\0';
|
||||||
word++;
|
word++;
|
||||||
wordChar = 0;
|
wordChar = 0;
|
||||||
@@ -63,7 +72,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.
Binary file not shown.
@@ -0,0 +1 @@
|
|||||||
|
Hund,Katze; Maus
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Apfel
|
||||||
|
Banane
|
||||||
|
Kiwi
|
||||||
Binary file not shown.
Reference in New Issue
Block a user