This commit is contained in:
zidanmo99094 committed 2026-10-27 12:48:19 +01:00
1 parent fb4211872e
commit ef95895501
1 file changed
+11 -4
+11 -4
View File
@@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_RAND_TRIES_PER_WORD 1000 #define MAX_RAND_TRIES_PER_WORD 100
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
// TODO: Spiellogik implementieren: // TODO: Spiellogik implementieren:
@@ -11,9 +11,16 @@
* restliche Felder mit zufälligen Buchstaben füllen */ * restliche Felder mit zufälligen Buchstaben füllen */
// Creates the word salad by placing words randomly and filling empty spaces // 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) int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int wordCount, char **words)
{ {
// 1. Spielfeld vollständig mit EMPTY_CHAR initialisieren // Zufallsgenerator einmalig initialisieren
static int seeded = 0;
if (!seeded) {
srand((unsigned int)time(NULL));
seeded = 1;
}
// 1. Spielfeld vollständig mit EMPTY_CHAR (0) initialisieren
for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) { for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) {
for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) { for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) {
salad[r][c] = EMPTY_CHAR; salad[r][c] = EMPTY_CHAR;
@@ -29,7 +36,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int len = (int)strlen(words[i]); int len = (int)strlen(words[i]);
if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue; if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue;
// Versuche bis zu MAX_RAND_TRIES_PER_WORD Mal, eine freie Position zu finden // Versuche bis zu MAX_RAND_TRIES_PER_WORD (10) Mal, eine freie Position zu finden
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) { for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) {
// 0 = Horizontal, 1 = Vertikal // 0 = Horizontal, 1 = Vertikal
int dir = rand() % 2; int dir = rand() % 2;