Added fixes and new Mechanics to game.c to also Pass Test 4.
Also I killed the comments and wrote worse new ones
This commit is contained in:
+13
-15
@@ -14,10 +14,9 @@
|
||||
// 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
|
||||
//Random Zeit initialisieren
|
||||
srand(time(NULL));
|
||||
|
||||
// 1. Spielfeld leeren
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
@@ -26,7 +25,9 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Wörter platzieren
|
||||
int placed = 0; //Anzahl der Erfolgreich platzierten Worte
|
||||
|
||||
//Platzieren
|
||||
for (unsigned int w = 0; w < wordCount; w++)
|
||||
{
|
||||
const char *word = words[w];
|
||||
@@ -34,29 +35,25 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
|
||||
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)
|
||||
//Ob EIN Wort platziert werden kann (wird noch nicht platziert!)
|
||||
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
|
||||
//Ränder des Spielfeldes beachten
|
||||
if (r >= (int)searchFieldLen || c >= (int)searchFieldLen)
|
||||
{
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Wenn belegt mit anderem Buchstaben -> abbrechen
|
||||
//Ist die Stelle nicht leer und passt Die stelle nicht zum passenden Buchstaben des Wortes
|
||||
if (salad[r][c] != EMPTY_CHAR && salad[r][c] != word[k])
|
||||
{
|
||||
canPlace = 0;
|
||||
@@ -64,8 +61,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn möglich, das Wort setzen
|
||||
if (canPlace)
|
||||
if (canPlace) //Wenn geprüft wurde ob ein Wort erfolgreich platziert werden kann.
|
||||
{
|
||||
for (unsigned int k = 0; k < wordLen; k++)
|
||||
{
|
||||
@@ -74,12 +70,13 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
if (r < (int)searchFieldLen && c < (int)searchFieldLen)
|
||||
salad[r][c] = word[k];
|
||||
}
|
||||
break; // Wort erfolgreich platziert
|
||||
placed++;
|
||||
break; //Beim Nächsten Wort weiter machen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Leere Felder mit Zufallsbuchstaben füllen
|
||||
//Mit Zufälligen Buchstaben füllen
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
@@ -91,9 +88,10 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return placed;
|
||||
}
|
||||
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user