updated game.c / game.h

This commit is contained in:
Jonas Urban 2025-10-20 22:18:45 +02:00
parent f3cdd0b307
commit b6bc24b6d2
2 changed files with 45 additions and 35 deletions

View File

@ -16,7 +16,7 @@ void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
{
for (int j = 0; j < searchFieldLen; j++)
{
if (salad[i][j] == '0') {
if (salad[i][j] == EMPTY_CHAR) {
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
}
}
@ -32,75 +32,84 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
{
int placedWords = 0;
//Feld komplettmit EMPTY_CHAR füllen
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = '0';
salad[i][j] = EMPTY_CHAR;
}
}
for (int i = 0; i < wordCount; i++)
//Wörter plazieren
for (int wordNumber = 0; wordNumber < wordCount; wordNumber++)
{
int wordLength = strlen(words[i]);
int xCoordinat = rand() % (searchFieldLen - wordLength + 1);
int yCoordinat = rand() % searchFieldLen;
if (rand() % 2) // erzeugt Zufallszahl 0 oder 1: 0 -> Horizontal; 1 -> Vertical
int wordLength = strlen(words[wordNumber]);
int placed = 0;
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
{
for (int placementTry = 0; placementTry < MAX_RAND_TRIES_PER_WORD; placementTry++)
int horizontal = rand() % 2; // 0 = vertikal, 1 = horizontal
int x = rand() % searchFieldLen;
int y = rand() % searchFieldLen;
if (horizontal)
{
if (x + wordLength > searchFieldLen) continue; // passt nicht
//Prüfen ob kein anderes Wort im Weg
int fits = 1;
for (int j = xCoordinat; j < wordLength + xCoordinat; j++)
for (int i = 0; i < wordLength; i++)
{
if(salad[yCoordinat][j] != '0')
if (salad[y][x + i] != EMPTY_CHAR)
{
fits = 0;
break;
}
}
if (!fits) //neue Position versuchen, wenn ein Buchstabe schon belegt
//Wort plazieren
if (fits)
{
continue;
}
else //Wort platzieren
{
for (int j = xCoordinat; j < wordLength + xCoordinat; j++)
for (int i = 0; i < wordLength; i++)
{
salad[yCoordinat][j] = words[i][j - xCoordinat];
salad[y][x + i] = words[wordNumber][i];
}
placedWords += 1;
break;
placed = 1;
placedWords++;
}
}
}
else
{
for (int placementTry = 0; placementTry < MAX_RAND_TRIES_PER_WORD; placementTry++)
else //Wort vertikal plazieren
{
int fits = 1;
for (int j = yCoordinat; j < wordLength + yCoordinat; j++)
if (y + wordLength > searchFieldLen) continue;
//Prüfen ob kein anderes Wort im Weg
int fits = 1;
for (int i = 0; i < wordLength; i++)
{
if(salad[j][yCoordinat] != '0')
if (salad[y + i][x] != EMPTY_CHAR)
{
fits = 0;
break;
}
}
if (!fits) //neue Position versuchen, wenn ein Buchstabe schon belegt
//Wort plazieren
if (fits)
{
continue;
}
else //Wort platzieren
{
for (int j = yCoordinat; j < wordLength + yCoordinat; j++)
for (int i = 0; i < wordLength; i++)
{
salad[j][yCoordinat] = words[i][j - yCoordinat];
salad[y + i][x] = words[wordNumber][i];
}
placedWords += 1;
break;
placed = 1;
placedWords++;
}
}
}
}
fillRestWithRandom(salad, searchFieldLen);
return placedWords;
}
// Prints the word salad to console

View File

@ -7,5 +7,6 @@
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
#endif