Implemented Word-&Letterplacement

This commit is contained in:
Henrik Batz committed 2026-10-29 15:51:43 +01:00
1 parent 93db739464
commit 3db13ace33
1 file changed
+88 -1
+88 -1
View File
@@ -2,6 +2,7 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
@@ -13,5 +14,91 @@
// 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)
{
if (searchFieldLen == 0 ||searchFieldLen > MAX_SEARCH_FIELD_LEN)
{return 0;}
}
int placedWOrds = 0;
srand((unsigned int)time(NULL));
//Spielfeld leeren:
for (int r = 0; r < searchFieldLen; r++ )
{
for (int c = 0; c < searchFieldLen; c++) {
salad[r][c] = EMPTY_CHAR;
}
}
//Wörter platzieren
for (int w = 0; w < wordCount; w++)
{
int len = strlen(words[w]); //Wortlänge festgelegt
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;
if (axis == 0) //Vertical~~~~~~~~~~~
{
//Available Space Check & Wordcollision Avoidance//////
for (int i = 0; i < len; i++)
{
if (len + row > searchFieldLen || salad[row+i][column] != EMPTY_CHAR){
error = 1;
break;
}
}
//Write Word
if (error == 0){
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 );}
}else if (axis == 1){ //Horizontal~~~~~~~~~~~~~~~
//Available Space Check & Wordcollision Avoidance
for (int i = 0; i < len; i++)
{
if (len + column > searchFieldLen || salad[row][column+i] != EMPTY_CHAR){
error = 1;
break;
}
}
//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 );}
}
}
}
//Fill empty spaces
return placedWOrds;
}