This commit is contained in:
Simon 2025-10-25 17:49:24 +02:00
parent 9026fa88ec
commit 30ef5e53f5

View File

@ -14,6 +14,18 @@
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)
{ {
// set seed // set seed
// showWordSalad(salad, 20);
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
salad[i][j] = '\0';
}
}
showWordSalad(salad, 20);
srand(time(NULL)); srand(time(NULL));
int wordsPlaced = 0; int wordsPlaced = 0;
@ -27,17 +39,13 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
// get random coordinates // get random coordinates
int rndX = rand() % (searchFieldLen - wordLength + 1); int rndX = rand() % (searchFieldLen - wordLength + 1);
int rndY = rand() % (searchFieldLen - wordLength + 1); int rndY = rand() % (searchFieldLen - wordLength + 1);
// rand() % 2
// 0 = horizontal 1 = vertikal if (1)
int direction = rand() % 2;
switch (direction)
{ {
case 0:
retries = 0;
while (letterPlaced < wordLength) while (letterPlaced < wordLength)
{ {
// checks if word fits into gamefield // checks if word fits into gamefield
while (!(checkSquare == wordLength) && (retries < 10)) while (checkSquare != wordLength && retries < 10)
{ {
checkSquare = 0; checkSquare = 0;
// checks if squares to paste into is already taken // checks if squares to paste into is already taken
@ -53,16 +61,16 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
rndX = rand() % (searchFieldLen - wordLength + 1); rndX = rand() % (searchFieldLen - wordLength + 1);
rndY = rand() % (searchFieldLen - wordLength + 1); rndY = rand() % (searchFieldLen - wordLength + 1);
retries++; // retries++;
} }
} }
if (retries >= 10) if (retries >= 10)
{ {
printf("Word %u couldn't be placed", n); printf("Word %d couldn't be placed", n);
break; break;
} }
printf("%d %d",n, checkSquare);
// pastes word into the line // pastes word into the line
if (checkSquare == wordLength) if (checkSquare == wordLength)
{ {
@ -73,15 +81,15 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
} }
} }
wordsPlaced++; wordsPlaced++;
case 1: }
retries = 0; else
{
while (letterPlaced < wordLength) while (letterPlaced < wordLength)
{ {
// checks if word fits into gamefield // checks if word fits into gamefield
while (!(checkSquare == wordLength) && (retries < 10)) while (checkSquare != wordLength && retries < 10)
{ {
checkSquare = 0; checkSquare = 0;
// checks if squares to paste into is already taken // checks if squares to paste into is already taken
@ -117,7 +125,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
} }
} }
wordsPlaced++; wordsPlaced++;
} }
} }
@ -130,10 +137,12 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
if(salad[y][x] == '\0') if(salad[y][x] == '\0')
{ {
// random number from 65 (A) to 90 (Z) with ASCII // random number from 65 (A) to 90 (Z) with ASCII
salad[y][x] = (char) (rand() % (25 + 1) + 65); // salad[y][x] = (char) (rand() % (25 + 1) + 65);
salad[y][x] = 120;
} }
} }
} }
return 15;
} }
// Prints the word salad to console // Prints the word salad to console
@ -150,3 +159,16 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
printf(" %c\n", salad[i][searchFieldLen]); printf(" %c\n", salad[i][searchFieldLen]);
} }
} }
// Clear Wordsalad
void clearWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
int i, j = 0;
for (i = 0; i < searchFieldLen; i++)
{
for (j = 0; j < searchFieldLen; j++)
{
salad[i][j] = '\0';
}
}
}