2025-10-25 16:28:05 +02:00

146 lines
4.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
// TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
// 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)
// {
// }
// // Prints the word salad to console
// void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
// {
// }
// Funktion, die einen zufälligen Großbuchstaben (AZ) erzeugt
char randomLetter()
{
return 'A' + rand() % 26;
}
// Erstellt das Wortsalat-Spielfeld und platziert die Wörter zufällig
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen,
const char words[][MAX_WORD_LEN],
unsigned int wordCount)
{
// 1. Spielfeld mit leeren Zeichen initialisieren
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));
int placedWords = 0;
// 2. Jedes Wort zufällig auf dem Spielfeld platzieren
for (unsigned int w = 0; w < wordCount; w++)
{
char word[MAX_WORD_LEN];
strncpy(word, words[w], MAX_WORD_LEN);
word[MAX_WORD_LEN - 1] = '\0';
// Alle Buchstaben des Wortes in Großbuchstaben umwandeln
for (size_t k = 0; k < strlen(word); k++)
{
word[k] = (char)toupper((unsigned char)word[k]);
}
size_t len = strlen(word);
if (len > searchFieldLen)
continue; // Wort ist zu lang → überspringen
int placed = 0;
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
{
int dir = rand() % 2; // 0 = horizontal, 1 = vertikal
int row = rand() % searchFieldLen;
int col = rand() % searchFieldLen;
if (dir == 0 && col + len <= searchFieldLen)
{
int ok = 1;
// Prüfen, ob der Platz frei ist oder die Buchstaben passen
for (size_t i = 0; i < len; i++)
{
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i])
{
ok = 0;
break;
}
}
if (ok)
{
// Wort horizontal einfügen
for (size_t i = 0; i < len; i++)
salad[row][col + i] = word[i];
placed = 1;
}
}
else if (dir == 1 && row + len <= searchFieldLen)
{
int ok = 1;
// Prüfen, ob der Platz frei ist oder die Buchstaben passen
for (size_t i = 0; i < len; i++)
{
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i])
{
ok = 0;
break;
}
}
if (ok)
{
// Wort vertikal einfügen
for (size_t i = 0; i < len; i++)
salad[row + i][col] = word[i];
placed = 1;
}
}
}
if (placed)
placedWords++; // Anzahl der erfolgreich platzierten Wörter erhöhen
}
// 3. Alle leeren Felder mit zufälligen Buchstaben füllen
for (unsigned int i = 0; i < searchFieldLen; i++)
{
for (unsigned int j = 0; j < searchFieldLen; j++)
{
if (salad[i][j] == EMPTY_CHAR)
salad[i][j] = randomLetter();
}
}
// Rückgabewert: Anzahl der platzierten Wörter
return placedWords;
}
// Gibt das Spielfeld in der Konsole aus
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen)
{
for (unsigned int i = 0; i < searchFieldLen; i++)
{
for (unsigned int j = 0; j < searchFieldLen; j++)
{
printf("%c ", salad[i][j]);
}
printf("\n");
}
}