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++) 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'; 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; int placedWords = 0;
//Feld komplettmit EMPTY_CHAR füllen
for (int i = 0; i < searchFieldLen; i++) for (int i = 0; i < searchFieldLen; i++)
{ {
for (int j = 0; j < searchFieldLen; j++) 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 wordLength = strlen(words[wordNumber]);
int xCoordinat = rand() % (searchFieldLen - wordLength + 1); int placed = 0;
int yCoordinat = rand() % searchFieldLen;
if (rand() % 2) // erzeugt Zufallszahl 0 oder 1: 0 -> Horizontal; 1 -> Vertical 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; 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; fits = 0;
break; break;
} }
} }
if (!fits) //neue Position versuchen, wenn ein Buchstabe schon belegt
//Wort plazieren
if (fits)
{ {
continue; for (int i = 0; i < wordLength; i++)
}
else //Wort platzieren
{
for (int j = xCoordinat; j < wordLength + xCoordinat; j++)
{ {
salad[yCoordinat][j] = words[i][j - xCoordinat]; salad[y][x + i] = words[wordNumber][i];
} }
placedWords += 1; placed = 1;
break; placedWords++;
} }
} }
} else //Wort vertikal plazieren
else
{
for (int placementTry = 0; placementTry < MAX_RAND_TRIES_PER_WORD; placementTry++)
{ {
int fits = 1; if (y + wordLength > searchFieldLen) continue;
for (int j = yCoordinat; j < wordLength + yCoordinat; j++)
//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; fits = 0;
break; break;
} }
} }
if (!fits) //neue Position versuchen, wenn ein Buchstabe schon belegt
//Wort plazieren
if (fits)
{ {
continue; for (int i = 0; i < wordLength; i++)
}
else //Wort platzieren
{
for (int j = yCoordinat; j < wordLength + yCoordinat; j++)
{ {
salad[j][yCoordinat] = words[i][j - yCoordinat]; salad[y + i][x] = words[wordNumber][i];
} }
placedWords += 1; placed = 1;
break; placedWords++;
} }
} }
} }
} }
fillRestWithRandom(salad, searchFieldLen);
return placedWords;
} }
// Prints the word salad to console // 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); 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 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 #endif