Funktionaler Wortsalat mit Test-printf()-anweisungen

This commit is contained in:
Jonas Hofmann 2025-11-04 16:48:22 +01:00
parent 6027c2a061
commit acc14b49d8
2 changed files with 20 additions and 8 deletions

View File

@ -55,7 +55,10 @@ wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordL
//srand(time(NULL)); //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);
printf("Wordlength: %d\n", wordLength);
position.startingCell = rand()%(searchFieldLen - wordLength); position.startingCell = rand()%(searchFieldLen - wordLength);
printf("Starting Cell: %d\n", position.startingCell);
return position; return position;
} }
@ -112,7 +115,7 @@ 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]); printf("%c",currentWord[j]);
@ -122,7 +125,7 @@ int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPositi
} }
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]); printf("%c",currentWord[j]);
@ -130,7 +133,7 @@ int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPositi
} }
salad[i][position.rowOrColumn] = '\0'; salad[i][position.rowOrColumn] = '\0';
} }
printf("\n");
return 1; return 1;
} }
@ -162,11 +165,19 @@ 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); showWordSalad(salad, searchFieldLen);
@ -174,18 +185,19 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
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("%d, %d, %d \n", currentWordPosition.alignment, currentWordPosition.rowOrColumn, currentWordPosition.startingCell);
printf("%s\n", currentWord); printf("%s\n", currentWord);
placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen); placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen);

Binary file not shown.