78 lines
2.2 KiB
C
78 lines
2.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 */
|
|
|
|
|
|
int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[])
|
|
{
|
|
for(size_t i = 0 ; i < strlen(wort); i++ )
|
|
{
|
|
short xoffset = (richtung) ? i : 0;
|
|
short yoffset = (richtung) ? 0 : i;
|
|
if (!(salad[x+xoffset][y+yoffset]=='#' || salad[x+xoffset][y+yoffset]==wort[i]))
|
|
{
|
|
return(-1);
|
|
}
|
|
}
|
|
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)
|
|
{
|
|
for(short i = searchFieldLen; i>0; i--)
|
|
{
|
|
for(short j = searchFieldLen; j>0; j--)
|
|
{
|
|
salad[i][j]= "#";
|
|
}
|
|
}
|
|
|
|
for(short i = wordCount;i>0;i--)
|
|
{
|
|
size_t platzbedarf = strlen(words[i-1]);
|
|
if (platzbedarf >= searchFieldLen)
|
|
{
|
|
printf("%s konnte nicht eingefuegt werden da es groeßer als das Feld ist",words[wordCount-1]);
|
|
continue;
|
|
}
|
|
srand(time(NULL));
|
|
short x,y,waagrecht;
|
|
do
|
|
{
|
|
short max = searchFieldLen - platzbedarf;
|
|
short position1 = rand() % (max +1);
|
|
short position2 = rand() % (searchFieldLen);
|
|
waagrecht = rand()%2;
|
|
x = (waagrecht) ? position1:position2;
|
|
y = (waagrecht) ? position2:position1;
|
|
|
|
|
|
}while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
// 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 y = 0; y < searchFieldLen; y++) {
|
|
for (unsigned int x = 0; x < searchFieldLen; x++) {
|
|
printf("%c ", salad[x][y]); // oder printf("%c", salad[x][y]); ohne Leerzeichen
|
|
}
|
|
printf("\n"); // neue Zeile nach jeder Zeile
|
|
}
|
|
}
|
|
|