111 lines
3.2 KiB
C
111 lines
3.2 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)
|
|
{
|
|
srand(time(NULL));
|
|
|
|
int x = 0;
|
|
int y = 0;
|
|
int placedWords = 0;
|
|
int i = 0;
|
|
int nichtPlatziert =0;
|
|
typedef enum {vertikal, horizontal} Platzierungsrichtung;
|
|
|
|
for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen
|
|
{
|
|
for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
|
{
|
|
salad[i][j] = '0';
|
|
}
|
|
}
|
|
|
|
while(placedWords < wordCount)
|
|
{
|
|
x = rand() % 20 +1;
|
|
y = rand() % 20 +1;
|
|
|
|
Platzierungsrichtung zufaellige_richtung = rand() % 2;
|
|
|
|
if(zufaellige_richtung == 0 && salad[y][x] == '0')
|
|
{
|
|
|
|
if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-y)) // -1, da Nullzeichen sowieso weg muss
|
|
{
|
|
for(int j = 0; j < sizeof(words[i]-1); j++)
|
|
{
|
|
if(salad[(y+j)][x] == '0')
|
|
{
|
|
strncpy(&salad[(y+j)][x], &words[i][j], 1);
|
|
}
|
|
else if(&salad[(y+j)][x] == &words[i][j])
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
nichtPlatziert = 1;
|
|
}
|
|
}
|
|
if(nichtPlatziert == 0)
|
|
{
|
|
placedWords++;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
}
|
|
else if( zufaellige_richtung == 1 && salad[y][x] == '0')
|
|
{
|
|
if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-x)) // -1, da Nullzeichen sowieso weg muss
|
|
{
|
|
for(int j = 0; j < sizeof(words[i]-1); j++)
|
|
{
|
|
if(salad[y][(x+j)] == '0')
|
|
{
|
|
strncpy(&salad[y][(x+j)], &words[i][j], 1);
|
|
|
|
}
|
|
else if(&salad[y][(x+j)] == &words[i][j])
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
nichtPlatziert = 1;
|
|
}
|
|
}
|
|
if(nichtPlatziert == 0)
|
|
{
|
|
placedWords++;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen
|
|
{
|
|
for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
|
{
|
|
if(salad[i][j] == '0')
|
|
{
|
|
salad[i][j] = rand() % (90-65+1)+ 65;
|
|
}
|
|
}
|
|
}
|
|
|
|
return placedWords;
|
|
}
|