Helena Köhler 9803a8e339 neu
2025-11-04 17:21:05 +01:00

91 lines
2.9 KiB
C
Raw 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>
#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 */
//Funktion: Initialisiert das Spielfeld mit leeren Zeichen
static void intitSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse)
{
for (unsigned int i =0; i < feldGroesse; i++)
for (unsigned int j=0; j < feldGroesse; j++)
salad[i][j]= EMPTY_CHAR;
}
//Funktion: Versucht, ein Wort an einer bestimmten Position zu platzieren
//Richtung: 0 = horizontal, 1 = vertikal
static int platziereWort(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int feldGroesse, const char *wort, int zeile, int spalte, int richtung)
{
unsigned int wortLaenge = strlen(wort);
//Überprüfen, ob das Word ins Feld passt
if (richtung == 0 && spalte + wortLaenge > feldGroesse) return 0; //horizontal
if (richtung == 1 && zeile + wortLaenge > feldGroesse) return 0; //vertikal
//Prüfen, ob die Positionen frei oder kompatibel sind
for (unsigned int i = 0; i < wortLaenge; i++){
char c = (richtung == 0) ? salad[zeile][spalte + i] : salad[zeile + i][spalte];
if (c != EMPTY_CHAR && c != wort[i])
return 0; //Kollision
}
//Wort eintragen
for (unsigned int i = 0; i < wortLaenge; i++){
if (richtung == 0)
salad[zeile][spalte + i ] = wort[i];
else
salad[zeile + i][spalte] = wort[i];
}
return 1;
}
// 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)
{
srand((unsigned int)time(NULL));
initSalad(salad, searchFieldLen);
unsigned int erfolgreichGesetzt = 0;
//Worter zufällig platzieren
for (unsigned int w = 0; w < wordCount; w++)
{
int platziert = 0;
for (int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD && !platziert; versuch++)
{
int richtung = rand() % 2;
int zeile = rand() % searchFieldLen;
int spalte= rand() % searchFieldLen;
platziert = platziereWort(salad, searchFieldLen, words[w], zeile, spalte, richtung);
}
if (platziert)
erfolgreichGesetzt++;
else
fprintf(stderr, "Warnung: Konnte Wort nicht platzieren: %s\n", words[w]);
}
}
//Leere Felder mit Zufallsbuchstaben 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] = ´A´ + rand() % 26;
}
return erfolgreichGesetzt;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
}