129 lines
3.7 KiB
C
129 lines
3.7 KiB
C
#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 */
|
|
|
|
// 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 generiert
|
|
char randomLetter()
|
|
{
|
|
return 'A' + rand() % 26;
|
|
}
|
|
|
|
// Hauptfunktion: Erstellt das Wortsalat-Spiel
|
|
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. Leeres Spielfeld 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));
|
|
|
|
// 2. Jedes Wort zufällig im Spielfeld platzieren
|
|
for (unsigned int w = 0; w < wordCount; w++)
|
|
{
|
|
const char *word = words[w];
|
|
size_t len = strlen(word);
|
|
if (len > searchFieldLen)
|
|
continue; // Überspringen, falls das Wort zu lang ist
|
|
|
|
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;
|
|
|
|
// Prüfen, ob das Wort an die Position passt
|
|
if (dir == 0 && col + len <= searchFieldLen)
|
|
{
|
|
// Prüfen, ob keine Überschneidungen mit anderen Buchstaben auftreten
|
|
int ok = 1;
|
|
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)
|
|
{
|
|
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;
|
|
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)
|
|
{
|
|
for (size_t i = 0; i < len; i++)
|
|
salad[row + i][col] = word[i];
|
|
placed = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Leere 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();
|
|
}
|
|
}
|
|
|
|
return 0; // 0 = erfolgreich
|
|
}
|
|
|
|
// Funktion zur Anzeige des Spielfelds in der Konsole
|
|
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");
|
|
}
|
|
} |