Spiel-Logik works maybe
noch nicht getestet
This commit is contained in:
parent
a8e47993e6
commit
4fcf133a29
@ -13,11 +13,96 @@
|
||||
// Creates the word salad by placing words randomly and filling empty spaces
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||
{
|
||||
// Zufall initialisieren
|
||||
srand(time(NULL));
|
||||
|
||||
// 1. Spielfeld leeren
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Wörter platzieren
|
||||
for (unsigned int w = 0; w < wordCount; w++)
|
||||
{
|
||||
const char *word = words[w];
|
||||
unsigned int wordLen = strlen(word);
|
||||
|
||||
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++)
|
||||
{
|
||||
// zufällige Startposition
|
||||
int row = rand() % searchFieldLen;
|
||||
int col = rand() % searchFieldLen;
|
||||
|
||||
// zufällige Richtung: 0 = horizontal, 1 = vertikal
|
||||
int direction = rand() % 2;
|
||||
|
||||
int canPlace = 1;
|
||||
|
||||
// Nur prüfen, ob Buchstaben passen (gleiche erlaubt, leer erlaubt)
|
||||
for (unsigned int k = 0; k < wordLen; k++)
|
||||
{
|
||||
int r = row + (direction == 1 ? k : 0);
|
||||
int c = col + (direction == 0 ? k : 0);
|
||||
|
||||
// Wenn außerhalb des Feldes -> abbrechen
|
||||
if (r >= (int)searchFieldLen || c >= (int)searchFieldLen)
|
||||
{
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Wenn belegt mit anderem Buchstaben -> abbrechen
|
||||
if (salad[r][c] != EMPTY_CHAR && salad[r][c] != word[k])
|
||||
{
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn möglich, das Wort setzen
|
||||
if (canPlace)
|
||||
{
|
||||
for (unsigned int k = 0; k < wordLen; k++)
|
||||
{
|
||||
int r = row + (direction == 1 ? k : 0);
|
||||
int c = col + (direction == 0 ? k : 0);
|
||||
if (r < (int)searchFieldLen && c < (int)searchFieldLen)
|
||||
salad[r][c] = word[k];
|
||||
}
|
||||
break; // Wort erfolgreich platziert
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Leere Felder mit Zufallsbuchstaben füllen
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if (salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
salad[i][j] = 'A' + (rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
printf("%c ", salad[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user