89 lines
2.8 KiB
C
89 lines
2.8 KiB
C
#include "game.h"
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_RAND_TRIES_PER_WORD 200
|
|
#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)
|
|
{
|
|
int placedWords = 0;
|
|
// Spielfeld leeren
|
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
|
salad[i][j] = EMPTY_CHAR;
|
|
|
|
srand(time(NULL));
|
|
|
|
for (unsigned int w = 0; w < wordCount; w++) {
|
|
const char *word = words[w];
|
|
unsigned int len = strlen(word);
|
|
int placed = 0;
|
|
|
|
// Versuche, das Wort zu platzieren
|
|
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
|
|
int ausrichtung = rand() % 2; // 0 = horizontal, 1 = vertikal
|
|
unsigned int x = rand() % searchFieldLen;
|
|
unsigned int y = rand() % searchFieldLen;
|
|
|
|
// Passt das Wort überhaupt ins Feld?
|
|
if ((ausrichtung == 0 && y + len > searchFieldLen) ||
|
|
(ausrichtung == 1 && x + len > searchFieldLen))
|
|
continue;
|
|
|
|
// Prüfen auf Überschneidung
|
|
int fits = 1;
|
|
for (unsigned int k = 0; k < len; k++) {
|
|
char c = (ausrichtung == 0) ? salad[x][y + k] : salad[x + k][y];
|
|
if (c != EMPTY_CHAR && c != word[k]) {
|
|
fits = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Wenn es passt, Wort eintragen
|
|
if (fits) {
|
|
for (unsigned int k = 0; k < len; k++) {
|
|
if (ausrichtung == 0)
|
|
salad[x][y + k] = word[k];
|
|
else
|
|
salad[x + k][y] = word[k];
|
|
}
|
|
placed = 1;
|
|
placedWords++;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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 placedWords;
|
|
}
|
|
|
|
|
|
// Prints the word salad to console
|
|
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");
|
|
}
|
|
}
|
|
|