small changes, works now

This commit is contained in:
Henrik Batz committed 2026-10-29 15:51:45 +01:00
1 parent 4f43ad6429
commit e001da9bd3
1 file changed
+91 -79
+91 -79
View File
@@ -7,103 +7,115 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: int placeWord(int len, int column, int row,
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
* restliche Felder mit zufälligen Buchstaben füllen */ int searchFieldLen, const char* word, int axis);
// 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 searchFieldLen,
const char words[][MAX_WORD_LEN],
unsigned int wordCount)
{ {
if (searchFieldLen == 0 ||searchFieldLen > MAX_SEARCH_FIELD_LEN) if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN)
{return 0;} return 0;
int placedWOrds = 0; int placedWords = 0;
srand((unsigned int)time(NULL));
//Spielfeld leeren: srand(time(NULL));
for (int r = 0; r < searchFieldLen; r++ )
{
for (int c = 0; c < searchFieldLen; c++) {
salad[r][c] = EMPTY_CHAR;
}
}
//Wörter platzieren // Spielfeld leeren:
for (int w = 0; w < wordCount; w++) for (int x = 0; x < searchFieldLen; x++)
{ for (int y = 0; y < searchFieldLen; y++)
int len = strlen(words[w]); //Wortlänge festgelegt salad[x][y] = EMPTY_CHAR;
for (int atempts = 0; atempts < MAX_RAND_TRIES_PER_WORD; atempts++)
{
//Muss in for-Schleife sein damit immer neu erzeugt wird
int axis = rand() % 2;
int row = rand() % searchFieldLen;
int column = rand() % searchFieldLen;
int error = 0;
// Wörter platzieren
for (int w = 0; w < wordCount; w++)
{
int wordlenght = strlen(words[w]); // Wortlänge festgelegt
if (wordlenght == 0 || wordlenght > searchFieldLen)
continue;
if (axis == 0) //Vertical~~~~~~~~~~~ for (int attempts = 0;
{ attempts < MAX_RAND_TRIES_PER_WORD;
//Available Space Check & Wordcollision Avoidance////// attempts++)
for (int i = 0; i < len; i++) {
// Muss in for-Schleife sein damit immer neu erzeugt wird
int axis = rand() % 2;
int row = rand() % searchFieldLen;
int column = rand() % searchFieldLen;
int success = placeWord(
wordlenght, column, row, salad,
searchFieldLen, words[w], axis);
if (success)
{ {
if (len + row > searchFieldLen || salad[row+i][column] != EMPTY_CHAR){ placedWords++;
error = 1; break;
break;
}
} }
//Write Word printf("Platzierung zum %d. Mal fehlgeschlagen!\n",
if (error == 0){ attempts + 1);
for (int i = 0; i < len; i++) }
{ }
salad[row+i][column] = words[w][i];
}
placedWOrds++;
break;
}else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );}
//Fill empty spaces
for (int r = 0; r < searchFieldLen; r++)
{
}else if (axis == 1){ //Horizontal~~~~~~~~~~~~~~~ for (int c = 0; c < searchFieldLen; c++)
{
//Available Space Check & Wordcollision Avoidance if (salad[r][c] == EMPTY_CHAR)
for (int i = 0; i < len; i++)
{ {
if (len + column > searchFieldLen || salad[row][column+i] != EMPTY_CHAR){ salad[r][c] = 'A' + rand() % 26;
error = 1;
break;
}
} }
}
}
printf("%d placed" ,placedWords);
return placedWords;
}
//Write Word
if (error == 0){
for (int i = 0; i < len; i++)
{
salad[row][column+i] = words[w][i];
}
placedWOrds++;
break;
}else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );}
} int placeWord(int len, int column, int row,
} char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
int searchFieldLen, const char* word, int axis)
{
// Available space check & word collision avoidance
for (int i = 0; i < len; i++)
{
if (axis == 0) // Horizontal
{
if (column + len > searchFieldLen ||
salad[row][column + i] != EMPTY_CHAR)
{
return 0;
}
}
else // Vertical
{
if (row + len > searchFieldLen ||
salad[row + i][column] != EMPTY_CHAR)
{
return 0;
}
}
}
} //Write Word
for (int i = 0; i < len; i++)
{
if (axis == 0) // Horizontal
{
salad[row][column + i] = word[i];
}
else // Vertical
{
salad[row + i][column] = word[i];
}
}
//Fill empty spaces return 1;
for (int r = 0; r < searchFieldLen; r++)
{
for (int c = 0; c < searchFieldLen; c++)
{
if (salad[r][c] == EMPTY_CHAR)
{
salad[r][c] = 'A' + rand() % 26;
}
}
}
return placedWOrds;
} }