2 Commits
Author SHA1 Message Date
hofmannjo99187 42a5143e9a testing-statements entfernt 2025-11-04 17:03:59 +01:00
hofmannjo99187 acc14b49d8 Funktionaler Wortsalat mit Test-printf()-anweisungen 2025-11-04 16:48:22 +01:00
2 changed files with 19 additions and 15 deletions
+19 -15
View File
@@ -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,26 +113,23 @@ 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.