generated from freudenreichan/info2Praktikum-Wortsalat
game update
This commit is contained in:
parent
a108133a59
commit
31fbbb3153
@ -18,74 +18,101 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
int placedWords = 0;
|
int placedWords = 0;
|
||||||
unsigned int tries = 0;
|
unsigned int attemps = 0;
|
||||||
unsigned int check_direction = 0, check_overlap = 0;
|
unsigned int check_direction = 0, check_overlap = 0;
|
||||||
int zeile = 0, spalte = 0, len = 0;
|
int zeile = 0, spalte = 0, len = 0;
|
||||||
|
|
||||||
//1. Schritt: Alle Felder mit 0 befüllen
|
//Alle Felder mit ? befüllen
|
||||||
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
|
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
|
||||||
{
|
{
|
||||||
for(int n = 0; n < searchFieldLen; n++) //n Spalte
|
for(int n = 0; n < searchFieldLen; n++) //n Spalte
|
||||||
{
|
{
|
||||||
salad[m][n] = '.';
|
salad[m][n] = '?';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Schritt: Prüfen ob Wort in Array geschrieben darf
|
//Prüfen ob Wort in Array geschrieben darf
|
||||||
for(int num_words = 0; num_words < wordCount; num_words++) //eingelesen Wörter zählen
|
for(int num_words = 0; num_words < wordCount; num_words++) //eingelesen Wörter zählen
|
||||||
{
|
{
|
||||||
len = strlen(words[num_words]); //Größe des Wortes ermitteln
|
len = strlen(words[num_words]); //Größe des Wortes ermitteln
|
||||||
|
|
||||||
if (len < searchFieldLen)
|
while(attemps < MAX_RAND_TRIES_PER_WORD)
|
||||||
return -1; //ERROR, falls Wort größer als Größe des Feldes (unnötig, da bei worteingabe bereits überprüft)
|
|
||||||
|
|
||||||
while(tries < MAX_RAND_TRIES_PER_WORD)
|
|
||||||
{
|
{
|
||||||
// zufällige Richtung auswählen
|
// zufällige Richtung auswählen
|
||||||
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
|
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
|
||||||
|
|
||||||
// zufälliger Startpunkt, Startkoordinaten
|
// zufälliger Startpunkt, Startkoordinaten
|
||||||
if (direction == 0) // 0 = VERTIKAL -> Spalte egal
|
if (direction == 0) // 0 = VERTIKAL
|
||||||
{
|
{
|
||||||
zeile = rand() % (searchFieldLen - len);
|
zeile = rand() % searchFieldLen;
|
||||||
spalte = rand() % (searchFieldLen);
|
spalte = rand() % searchFieldLen;
|
||||||
check_direction = 1;
|
check_direction = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (direction == 1) // 1 = HORIZONTAL -> Zeile egal
|
if (direction == 1) // 1 = HORIZONTAL
|
||||||
{
|
{
|
||||||
zeile = rand() % (searchFieldLen);
|
zeile = rand() % searchFieldLen;
|
||||||
spalte = rand() % (searchFieldLen - len);
|
spalte = rand() % searchFieldLen;
|
||||||
check_direction = 1;
|
check_direction = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Prüfen auf Überlappung ----------> Logikfehler
|
for(int i = 0; i < len; i++)
|
||||||
for(int i_overlap = 0; i_overlap < len; i_overlap++)
|
|
||||||
{
|
{
|
||||||
if (direction == 0) // 0 = VERTIKAL
|
if (direction == 0) // 0 = VERTIKAL
|
||||||
{
|
{
|
||||||
if(salad[zeile + i_overlap][spalte] != '.')
|
//Prüfe ob Feld bereits belegt
|
||||||
|
if (salad[zeile+i][spalte] != '?')
|
||||||
{
|
{
|
||||||
check_overlap = 1; //wenn check_overlap = 1 -> belegt
|
check_overlap = 1; // 1 = Überlappung
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if (direction == 1) // 1 = HORIZONTAL
|
//Prüfe auf Groeße
|
||||||
{
|
if (spalte + len >= searchFieldLen-1 || check_overlap == 1)
|
||||||
if(salad[zeile][spalte + i_overlap] == '.')
|
|
||||||
{
|
{
|
||||||
check_overlap = 1; //wenn check_overlap = 1 -> belegt
|
attemps++;
|
||||||
break;
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(int k; k < len; k++)
|
||||||
|
{
|
||||||
|
salad[zeile][spalte+k] = words[i][k];
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(check_overlap == 0)
|
|
||||||
|
if (direction == 1) // 1 = HORZIZONTAL
|
||||||
{
|
{
|
||||||
tries++;
|
//Prüfe ob Feld bereits belegt
|
||||||
break;
|
if (salad[zeile][spalte+i] != '?')
|
||||||
|
{
|
||||||
|
check_overlap = 1; // 1 = Überlappung
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Prüfe auf Groeße
|
||||||
|
if (zeile + len >= searchFieldLen-1 || check_overlap == 1)
|
||||||
|
{
|
||||||
|
attemps++;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(int k; k < len; k++)
|
||||||
|
{
|
||||||
|
salad[zeile+k][spalte] = words[i][k];
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//3. Schritt: Wort in Wortsalat schreiben
|
/*
|
||||||
|
|
||||||
|
//Wort in Wortsalat schreiben
|
||||||
if(check_direction == 1 && check_overlap == 0 && tries < MAX_RAND_TRIES_PER_WORD)
|
if(check_direction == 1 && check_overlap == 0 && tries < MAX_RAND_TRIES_PER_WORD)
|
||||||
{
|
{
|
||||||
for(int i_set = 0; i_set < len; i_set++)
|
for(int i_set = 0; i_set < len; i_set++)
|
||||||
@ -121,6 +148,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
}
|
||||||
return placedWords; //platzierte Wörter zurückgeben
|
return placedWords; //platzierte Wörter zurückgeben
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user