Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
364dad3ac8 | ||
|
|
d21c6beda0 |
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 1000
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
//TODO: Spiellogik implementieren:
|
||||||
@@ -13,5 +13,77 @@
|
|||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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 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 i,j;
|
||||||
|
int Woertergesetzt = 0;
|
||||||
|
for (i=0 ; i<MAX_SEARCH_FIELD_LEN;i++)
|
||||||
|
{
|
||||||
|
for (j=0 ; j<MAX_SEARCH_FIELD_LEN;j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (unsigned int k = 0 ; k < wordCount ; k++)
|
||||||
|
{
|
||||||
|
int richtigplatziert = 0;
|
||||||
|
unsigned int laenge = strlen(words[k]);
|
||||||
|
for (int f = 0 ; f < MAX_RAND_TRIES_PER_WORD && !richtigplatziert ; f++)
|
||||||
|
{
|
||||||
|
unsigned int ausrichtung, zeile, spalte;
|
||||||
|
ausrichtung = rand() % 2;
|
||||||
|
zeile = rand() % (searchFieldLen);
|
||||||
|
spalte = rand() % (searchFieldLen);
|
||||||
|
|
||||||
|
if (ausrichtung == 0) // horizontal
|
||||||
|
{
|
||||||
|
if (spalte + laenge <= searchFieldLen)
|
||||||
|
{
|
||||||
|
int moeglichePosition = 1;
|
||||||
|
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
||||||
|
{
|
||||||
|
if (salad[zeile][spalte + i1] != EMPTY_CHAR)
|
||||||
|
moeglichePosition = 0;
|
||||||
|
}
|
||||||
|
if (moeglichePosition)
|
||||||
|
{
|
||||||
|
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
||||||
|
salad[zeile][spalte + i1] = words[k][i1];
|
||||||
|
richtigplatziert = 1;
|
||||||
|
Woertergesetzt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // vertikal
|
||||||
|
{
|
||||||
|
if (zeile + laenge <= searchFieldLen)
|
||||||
|
{
|
||||||
|
int moeglichePosition = 1;
|
||||||
|
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
||||||
|
{
|
||||||
|
if (salad[zeile + i1][spalte] != EMPTY_CHAR)
|
||||||
|
moeglichePosition = 0;
|
||||||
|
}
|
||||||
|
if (moeglichePosition)
|
||||||
|
{
|
||||||
|
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
||||||
|
salad[zeile + i1][spalte] = words[k][i1];
|
||||||
|
richtigplatziert = 1;
|
||||||
|
Woertergesetzt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// restliche Felder mit zufaelligen Buchstaben fuellen
|
||||||
|
for (i = 0; i < (int)searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < (int)searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
if (salad[i][j] == EMPTY_CHAR)
|
||||||
|
salad[i][j] = 'A' + rand() % 26;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Woertergesetzt;
|
||||||
}
|
}
|
||||||
@@ -8,33 +8,5 @@
|
|||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
unsigned int wordCount=0;// die beiden Zähler also hier fertige wörter werden hier gespeichert
|
|
||||||
unsigned int letterCount=0;// zählt die gespeicherten zeichen der aktuellen wörter
|
|
||||||
int character;
|
|
||||||
|
|
||||||
//ungültige parameter müssen abgefragt werden
|
|
||||||
if (file == NULL || words == NULL || maxWordCount == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// datei zeichen für zeichen lesen & fgetc (file get character) E0F (end of file)
|
|
||||||
while (wordCount < maxWordCount && (character = fgetc(file)) != EOF)
|
|
||||||
{
|
|
||||||
//leerräume tc trennen die wörter
|
|
||||||
if (isspace(character) || character == ',' || character == ';') {
|
|
||||||
if (letterCount > 0) {
|
|
||||||
words[wordCount] [letterCount] = '\0';
|
|
||||||
wordCount++;
|
|
||||||
letterCount=0;
|
|
||||||
}
|
|
||||||
} else if (letterCount < MAX_WORD_LEN -1 ) {
|
|
||||||
words[wordCount] [letterCount] = (char)toupper (character);// to uppercase
|
|
||||||
letterCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// letzes wort speicher, auch ohne trennzeichen am datei ende
|
|
||||||
if (letterCount > 0 && wordCount < maxWordCount) {
|
|
||||||
words[wordCount] [letterCount] = '\0';
|
|
||||||
wordCount++;
|
|
||||||
}
|
|
||||||
return (int)wordCount;
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user