generated from freudenreichan/info2Praktikum-Wortsalat
127 lines
3.5 KiB
C
127 lines
3.5 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) {
|
|
|
|
int addedWords = 0;
|
|
int attempts = 0;
|
|
int added = 0; // = 1 wäre Wort hinzugefügt
|
|
int space = 0; // = 1 wäre Wort zu lang für diese Stelle
|
|
unsigned long long wordLength = 0;
|
|
|
|
srand(time(NULL));
|
|
|
|
for (int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
salad[i][j] = '.'; //Füllen des gesamten Arrays mit '.'
|
|
}
|
|
}
|
|
for (int i=0; i < wordCount; i++)
|
|
{
|
|
|
|
wordLength = strlen(words[i]);
|
|
|
|
while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1)
|
|
{
|
|
int direction = rand() % 2;// 0 = Horizontal, 1 = Vertikal
|
|
if (direction == 0) //Horizontal
|
|
{
|
|
int spalte = rand() % searchFieldLen;
|
|
int reihe = rand() % searchFieldLen;
|
|
for (int j = 0; j < wordLength; j++)
|
|
{
|
|
if (salad[reihe][spalte+j] != '.') //Prüfen das Wort mit einem anderem Wort überlappen würde
|
|
{
|
|
space = 1;
|
|
break;
|
|
}
|
|
|
|
}
|
|
if ((spalte + wordLength >= searchFieldLen-1) || (space == 1)) // überprüfung, ob das Wort an der Stelle ins Array passt
|
|
{
|
|
attempts++;
|
|
}
|
|
|
|
|
|
else
|
|
{
|
|
for (int j = 0; j < wordLength; j++)
|
|
{
|
|
salad[reihe][spalte+j] = words[i][j]; //Einfügen er Chars des Wortes
|
|
}
|
|
added=1;
|
|
addedWords++;
|
|
}
|
|
}
|
|
else if (direction == 1) // Vertikal
|
|
{
|
|
int spalte = rand() % searchFieldLen;
|
|
int reihe = rand() % searchFieldLen;
|
|
for (int j = 0; j < wordLength; j++)
|
|
{
|
|
if (salad[reihe+j][spalte] != '.')
|
|
{
|
|
space = 1;
|
|
break;
|
|
}
|
|
}
|
|
if ((reihe + wordLength >= searchFieldLen-1) || (space == 1))
|
|
{
|
|
attempts++;
|
|
}
|
|
|
|
|
|
else
|
|
{
|
|
for (int j = 0; j < wordLength; j++)
|
|
{
|
|
salad[reihe+j][spalte] = words[i][j];
|
|
}
|
|
added=1;
|
|
addedWords++;
|
|
}
|
|
}
|
|
space = 0;
|
|
}
|
|
attempts = 0;
|
|
added = 0;
|
|
}
|
|
for (int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
if (salad[i][j] == '.')
|
|
{
|
|
salad[i][j] = rand()%('Z'-'A'+1)+'A';//Zufällige Befüllung der noch nicht befüllten Felder
|
|
}
|
|
}
|
|
}
|
|
return addedWords;
|
|
}
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
for(int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for(int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
printf("%c", salad[i][j]);//Ausgabe des Arrays auf die Konsole
|
|
}
|
|
}
|
|
}
|