generated from freudenreichan/info2Praktikum-Wortsalat
150 lines
4.1 KiB
C
150 lines
4.1 KiB
C
#include "game.h"
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h> //für toupper
|
|
|
|
#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
|
|
|
|
void fillArrayEmpty(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
|
void generateCoordinates(unsigned int searchFieldLen, int *platzierung, int *xKoord, int *yKoord);
|
|
int placeWords(unsigned int searchFieldLen, const char *wort, int platzierung, int xKoord, int yKoord, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
|
void fillRandLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
|
|
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
|
{
|
|
fillArrayEmpty(salad, searchFieldLen);
|
|
|
|
srand((unsigned int)time(NULL));
|
|
|
|
int numberWords = 0;
|
|
for (unsigned int i = 0; i < wordCount; i++)
|
|
{
|
|
|
|
const char *wort = 0;
|
|
wort = words[i];
|
|
|
|
for (int k = 0; k < MAX_RAND_TRIES_PER_WORD; k++)
|
|
{
|
|
int platzierung = 0;
|
|
int xKoord = 0;
|
|
int yKoord = 0;
|
|
int success = 0;
|
|
|
|
generateCoordinates(searchFieldLen, &platzierung, &xKoord, &yKoord);
|
|
success = placeWords(searchFieldLen, wort, platzierung, xKoord, yKoord, salad);
|
|
|
|
if (success != 0)
|
|
{
|
|
numberWords++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
fillRandLetters(salad, searchFieldLen);
|
|
|
|
return numberWords;
|
|
}
|
|
|
|
void fillArrayEmpty(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++)
|
|
{
|
|
salad[i][j] = EMPTY_CHAR;
|
|
}
|
|
}
|
|
}
|
|
|
|
void generateCoordinates(unsigned int searchFieldLen, int *platzierung, int *xKoord, int *yKoord)
|
|
{
|
|
*platzierung = (unsigned int)rand() % 2; // 0=Horizont.; 1=Vertik.
|
|
*xKoord = (unsigned int)rand() % searchFieldLen;
|
|
*yKoord = (unsigned int)rand() % searchFieldLen;
|
|
}
|
|
|
|
int placeWords(unsigned int searchFieldLen, const char *wort, int platzierung, int xKoord, int yKoord, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
|
|
{
|
|
|
|
if (platzierung == 0) // Horizontal
|
|
{
|
|
size_t len = strnlen(wort, MAX_WORD_LEN);
|
|
|
|
if ((size_t)xKoord + len > searchFieldLen) //Addition da unsigned int
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
for (size_t j = 0; j < len; j++)
|
|
{
|
|
char c = salad[yKoord][xKoord + j];
|
|
char w = (char)toupper((unsigned char)wort[j]); //Umwandlung in Großbuchstaben
|
|
if (c != EMPTY_CHAR && c != w)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
for (size_t j = 0; j < len; j++)
|
|
{
|
|
salad[yKoord][xKoord + j] = (char)toupper((unsigned char)wort[j]); //Umwandlung in Großbuchstaben
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
else if (platzierung == 1) // Vertikal (analog zu Horizontal)
|
|
{
|
|
size_t len = strnlen(wort, MAX_WORD_LEN);
|
|
|
|
if ((size_t)yKoord + len > searchFieldLen)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
for (size_t j = 0; j < len; j++)
|
|
{
|
|
char c = salad[yKoord + j][xKoord];
|
|
char w = (char)toupper((unsigned char)wort[j]);
|
|
if (c != EMPTY_CHAR && c != w)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
for (size_t j = 0; j < len; j++)
|
|
{
|
|
salad[yKoord + j][xKoord] = (char)toupper((unsigned char)wort[j]);
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void fillRandLetters(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++)
|
|
{
|
|
if (salad[i][j] == EMPTY_CHAR)
|
|
{
|
|
salad[i][j] = (unsigned int)rand() % 26 + 'A'; // Assci A=65='A', Z=90
|
|
}
|
|
}
|
|
}
|
|
}
|