generated from freudenreichan/info2Praktikum-Wortsalat
game.c, createWordSalad finished, not tested
This commit is contained in:
parent
45fb1f651b
commit
b2118b4c24
@ -13,74 +13,101 @@
|
|||||||
// 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));
|
||||||
wordSalad: char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] -> bereits definiert
|
|
||||||
searchFieldLen: SALAD_SIZE = 20
|
|
||||||
words: words_array
|
|
||||||
wordcount: Anzahl der eingelesen Wörter
|
|
||||||
|
|
||||||
*/
|
//1. Schritt: Alle Felder mit 0 befüllen
|
||||||
|
|
||||||
/*
|
|
||||||
// 1. Schritt: Alle Felder mit zufälligen Zahlen befüllen
|
|
||||||
srand(time(NULL)); //Seed zufällig ändern
|
|
||||||
int size_word;
|
int size_word;
|
||||||
int placed_words;
|
int placed_words = 0;
|
||||||
|
unsigned int succesfully_placed = 0;
|
||||||
|
unsigned int tries = 0;
|
||||||
|
unsigned int check_direction = 0, check_overlap = 0;
|
||||||
|
int zeile, spalte;
|
||||||
|
|
||||||
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] = 'A' + rand() % 26; //ASCII-Form
|
salad[m][n] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
// 2. Schritt: Prüfen ob Wort in Array geschrieben darf
|
||||||
// 2. Schritt: Wörter in das Array überschreiben
|
for(int num_words = 0; num_words < wordCount && num_words < searchFieldLen; num_words++) //eingelesen Wörter zählen
|
||||||
|
|
||||||
for(int i = 0; i < wordCount && i < searchFieldLen; i++) //eingelesen Wörter zählen + Länge des Feldes nicht überschreiten
|
|
||||||
{
|
{
|
||||||
size_t len = strlen(words[i]); //Größe des Wortes ermitteln
|
size_t len = strlen(words[num_words]); //Größe des Wortes ermitteln
|
||||||
|
|
||||||
if (len < searchFieldLen)
|
if (len < searchFieldLen)
|
||||||
return -1; //ERROR, falls Wort größer als Größe des Feldes, unnötig, da bei worteingabe bereitsüberprüft
|
return -1; //ERROR, falls Wort größer als Größe des Feldes (unnötig, da bei worteingabe bereits überprüft)
|
||||||
|
|
||||||
unsigned int succesfully_placed = 0;
|
|
||||||
unsigned int tries = 0;
|
|
||||||
|
|
||||||
while(succesfully_placed == 0 && tries < MAX_RAND_TRIES_PER_WORD)
|
while(succesfully_placed == 0 && tries < MAX_RAND_TRIES_PER_WORD)
|
||||||
{
|
{
|
||||||
// zufälliger Startpunkt
|
|
||||||
int row = rand() % searchFieldLen; // Reihe
|
|
||||||
int col = rand() % searchFieldLen; // Spalte
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
// Prüfen der Länge in Richtung, sodass Feld nicht überschritten
|
// zufälliger Startpunkt, Startkoordinaten
|
||||||
int fitsin = 1;
|
for(int i = 0; i < wordCount; i++)
|
||||||
|
|
||||||
for(int t = 0; t < len; t++)
|
|
||||||
{
|
{
|
||||||
if(direction = 0)
|
if (direction == 0) // 0 = VERTIKAL -> Spalte egal
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prüfen, ob bereits buchstaben in dem Feld sich befinden, bzw. der Buchstabe mit dem anderen übereinstimmt
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(xxx)
|
|
||||||
{
|
{
|
||||||
succesfully_placed = 1; // erfolgreich eingefügt
|
for(int j = 0; j < len; j++)
|
||||||
|
{
|
||||||
|
zeile = rand() % (searchFieldLen - len);
|
||||||
|
spalte = rand() % (searchFieldLen);
|
||||||
|
salad[zeile][spalte] = words[j];
|
||||||
|
check_direction = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (direction == 1) // 1 = HORIZONTAL -> Zeile egal
|
||||||
|
{
|
||||||
|
for(int k = 0; k < len; k++)
|
||||||
|
{
|
||||||
|
int zeile = rand() % (searchFieldLen);
|
||||||
|
int spalte = rand() % (searchFieldLen - len);
|
||||||
|
salad[zeile][spalte] = words[k];
|
||||||
|
check_direction = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Prüfen auf Überlappung
|
||||||
|
for(int i_overlap = 0; i_overlap < len; i_overlap++)
|
||||||
|
{
|
||||||
|
if (direction == 0) // 0 = VERTIKAL
|
||||||
|
{
|
||||||
|
if (words[num_words][i_overlap] == salad[zeile][spalte + i_overlap]);
|
||||||
|
check_overlap = 1;
|
||||||
|
}
|
||||||
|
else if (direction == 1) // 1 = HORIZONTAL
|
||||||
|
{
|
||||||
|
if (words[num_words][i_overlap] == salad[zeile + i_overlap][spalte]);
|
||||||
|
check_overlap = 1;
|
||||||
|
}
|
||||||
|
if(check_overlap == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//3. Schritt: Wort in Wortsalat schreiben
|
||||||
|
if(check_direction == 1 && check_overlap == 1)
|
||||||
|
{
|
||||||
|
for(int i_set = 0; i_set < len; i_set++)
|
||||||
|
{
|
||||||
|
if (direction == 0) // 0 = VERTIKAL
|
||||||
|
{
|
||||||
|
salad[zeile + i_set][spalte] = words[num_words][i_set];
|
||||||
|
placed_words++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (direction == 1) // 1 = HORIZONTAL
|
||||||
|
{
|
||||||
|
salad[zeile][spalte + i_set] = words[num_words][i_set];
|
||||||
|
placed_words++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return placed_words; //platzierte Wörter zurückgeben
|
||||||
return placedWords;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
|
|||||||
@ -41,6 +41,8 @@ int main(int argc, char *argv[])
|
|||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
|
||||||
|
// wordcount mit placedwords vergleichen
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user