Kommentierungen
This commit is contained in:
parent
79c2531601
commit
940526fa7e
@ -12,44 +12,48 @@
|
||||
|
||||
// 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)
|
||||
{// Länge der searchFieldLen wird in Main unter Salad size festgelegt (20)
|
||||
{
|
||||
|
||||
srand((unsigned int)time(NULL)); // Seed für Zufallsgenerator
|
||||
|
||||
|
||||
// Initialisiere das Spielfeld mit EMPTY_CHAR
|
||||
// Initialisiere das Spielfeld
|
||||
for (unsigned int i = 0; i < searchFieldLen; ++i)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; ++j)
|
||||
{
|
||||
salad[i][j] = ' ';
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int placedWords = 0;
|
||||
unsigned int placedWords = 0; // Zähler für die Anzahl platzierter Wörter für spätere Überprüfung
|
||||
|
||||
for (unsigned int word = 0; word < wordCount; ++word)
|
||||
{
|
||||
int placed = 0;
|
||||
int placed = 0; //Flag um zu kennzechneinen, ob ein Wort platziert worden ist oder nicht
|
||||
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; ++tries)
|
||||
{
|
||||
// Festlegen der Parameter zum Platzieren des Worts
|
||||
int direction = rand() % 2; // 1 = horizontal, 0 = vertical
|
||||
int row = rand() % searchFieldLen;
|
||||
int col = rand() % searchFieldLen;
|
||||
int len = (int)strlen(words[word]);
|
||||
|
||||
if (direction == 1 && col + len <= searchFieldLen) // prüft ob das Wort horizontal in die Tabelle passt
|
||||
// prüfen, ob das Wort horizontal in die Tabelle passt
|
||||
if (direction == 1 && col + len <= searchFieldLen)
|
||||
{
|
||||
int canPlace = 1;
|
||||
int canPlace = 1; // Flag um zu kennzeichnen, dass das Wort platziert werden kann
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
|
||||
// Wenn in den Feldern schon andere Buchstaben stehen, kann das Feld nicht platziert werden
|
||||
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != words[word][i])
|
||||
{
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Wenn das Wort platziert werden kann, wird es platziert
|
||||
if (canPlace)
|
||||
{
|
||||
for (int i = 0; i < len; ++i)
|
||||
@ -57,11 +61,13 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
placed = 1;
|
||||
}
|
||||
}
|
||||
// prüfen, ob das Wort vertikal in die Tabelle passt
|
||||
else if (direction == 0 && row + len <= searchFieldLen) // vertical
|
||||
{
|
||||
int canPlace = 1;
|
||||
int canPlace = 1;// Flag um zu kennzeichnen, dass das Wort platziert werden kann
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
// Wenn in den Feldern schon andere Buchstaben stehen, kann das Feld nicht platziert werden
|
||||
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != words[word][i])
|
||||
{
|
||||
canPlace = 0;
|
||||
@ -69,6 +75,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Wenn das Wort platziert werden kann, wird es platziert
|
||||
if (canPlace)
|
||||
{
|
||||
for (int i = 0; i < len; ++i)
|
||||
@ -77,7 +84,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Zähler hochzählen, wenn ein Wort platziert wurde
|
||||
if (placed)
|
||||
placedWords++;
|
||||
}
|
||||
@ -90,7 +97,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
{
|
||||
if (salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
salad[i][j] = 'A' + rand() % 26;
|
||||
salad[i][j] = 'A' + (rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +46,6 @@ test: input.o game.o unit_tests.c
|
||||
$(BINARIES)/libwordsalad_complete.a: input.o game.o
|
||||
ar rcs $(BINARIES)/libwordsalad_complete.a input.o game.o
|
||||
|
||||
|
||||
wordsalad_myversion: main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user