Compare commits
No commits in common. "ea14dbbcfc0495c1782597bf135af4aa57ef1b37" and "558c63f4a6e110314aa57282d985d58d91e83c93" have entirely different histories.
ea14dbbcfc
...
558c63f4a6
@ -13,94 +13,13 @@
|
|||||||
// 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 || wordCount ==0)
|
printf("Hello World\n");
|
||||||
return 0;
|
return rand() % wordCount;
|
||||||
|
|
||||||
// Initialisiert das gesamte Spielfeld, indem alle Zellen auf EMPTY_CHAR gesetzt werden.
|
|
||||||
// Dadurch werden alle Felder als leer markiert, bevor Wörter eingefügt werden.
|
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++) {
|
|
||||||
for (unsigned int j = 0; j< searchFieldLen; j++){
|
|
||||||
salad[i][j] = EMPTY_CHAR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
srand((unsigned int)time(NULL)); // Zufallsgenerator mit Zeit initialisieren
|
|
||||||
|
|
||||||
int placedWords = 0; // Zähler für erfolgreich platzierte Wörter
|
|
||||||
|
|
||||||
|
|
||||||
for (unsigned int w = 0; w < wordCount; w++){ // Zählt alle Wörter durch
|
|
||||||
const char *word = words[w]; // Abkürzung zu word[w]
|
|
||||||
size_t len = strlen(word); // Misst die länge des aktuellen Wortes
|
|
||||||
int placed = 0; // Schaut, ob das aktuelle Wort platziert wurde
|
|
||||||
|
|
||||||
|
|
||||||
if (len == 0 || len > searchFieldLen) { // Überspringt Wörter, die nicht ins Spielfeld passen
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) { // Versucht die Wörter zu platzieren
|
|
||||||
int dir = rand() %2; // 0 = horizontal, 1 = vertikal
|
|
||||||
unsigned int row = rand() % searchFieldLen; // Zufällige Richtung für Startposition
|
|
||||||
unsigned int col = rand() % searchFieldLen;
|
|
||||||
|
|
||||||
// horizontal
|
|
||||||
if (dir == 0 && col + len <= searchFieldLen) { // Prüft, ob das Wort horizontal reinpasst
|
|
||||||
int fits = 1; // geht davon aus, dass es passt
|
|
||||||
for (size_t k = 0; k < len; k++) { // Prüft jeden Buchstaben des Wortes
|
|
||||||
if (salad[row][col + k] != EMPTY_CHAR && // Prüft ob der Buchstabe reinpasst
|
|
||||||
salad[row][col + k] != word[k]) {
|
|
||||||
fits = 0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fits) { // Prüft, ob das Wort in diese Position passt
|
|
||||||
for (size_t k = 0; k < len; k++) // Läuft über jeden Buchstaben des Wortes
|
|
||||||
salad[row][col + k] = word[k]; // Schreibt den Buchstaben ins Spielfeld
|
|
||||||
placed = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// vertikal
|
|
||||||
else if (dir == 1 && row + len <=searchFieldLen) {
|
|
||||||
int fits = 1;
|
|
||||||
for (size_t k = 0; k < len; k++) {
|
|
||||||
if (salad[row + k][col] != EMPTY_CHAR && salad[row + k][col] != word[k]) {
|
|
||||||
fits = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fits) {
|
|
||||||
for (size_t k = 0; k < len; k++)
|
|
||||||
salad[row + k][col] = word[k];
|
|
||||||
placed = 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (placed)
|
|
||||||
placedWords++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++) { // Gehe jede Zelle des Spielfelds durch
|
|
||||||
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
|
||||||
if (salad[i][j] ==EMPTY_CHAR) // Nur leere Felder betrachten
|
|
||||||
salad[i][j] = 'A' + (rand() % 26); // zufällige Buchstaben erzeugen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return placedWords; // Gibt an, wie viele Wörter platziert wurden
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++) { // Läuft über jede Zeile des Spielfelds
|
printf("Hello World\n");
|
||||||
for (unsigned int j = 0; j < searchFieldLen; j++) { // Läuft über jede Spalte in dieser Zeile
|
}
|
||||||
printf("%c ", salad[i][j]); // Gibt den Buchstaben in der Zelle aus
|
|
||||||
}
|
|
||||||
printf("\n"); // Beginnt eine neue Zeile nach jeder Reihe
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -40,9 +40,7 @@ int main(int argc, char *argv[])
|
|||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
if (!placedWords) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user