generated from freudenreichan/info2Praktikum-Wortsalat
Test
This commit is contained in:
parent
1fb5db8a4f
commit
111d1ce238
@ -13,11 +13,118 @@
|
|||||||
// 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 placedWords = 0;
|
||||||
|
unsigned int tries = 0;
|
||||||
|
unsigned int check_direction = 0, check_overlap = 0;
|
||||||
|
int zeile = 0, spalte = 0, len = 0;
|
||||||
|
|
||||||
|
//1. Schritt: Alle Felder mit 0 befüllen
|
||||||
|
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
|
||||||
|
{
|
||||||
|
for(int n = 0; n < searchFieldLen; n++) //n Spalte
|
||||||
|
{
|
||||||
|
salad[m][n] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Schritt: Prüfen ob Wort in Array geschrieben darf
|
||||||
|
for(int num_words = 0; num_words < wordCount && num_words < searchFieldLen; num_words++) //eingelesen Wörter zählen
|
||||||
|
{
|
||||||
|
len = strlen(words[num_words]); //Größe des Wortes ermitteln
|
||||||
|
|
||||||
|
if (len < searchFieldLen)
|
||||||
|
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
|
||||||
|
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
|
||||||
|
|
||||||
|
// zufälliger Startpunkt, Startkoordinaten
|
||||||
|
if (direction == 0) // 0 = VERTIKAL -> Spalte egal
|
||||||
|
{
|
||||||
|
zeile = rand() % (searchFieldLen - len);
|
||||||
|
spalte = rand() % (searchFieldLen);
|
||||||
|
check_direction = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (direction == 1) // 1 = HORIZONTAL -> Zeile egal
|
||||||
|
{
|
||||||
|
zeile = rand() % (searchFieldLen);
|
||||||
|
spalte = rand() % (searchFieldLen - len);
|
||||||
|
check_direction = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Prüfen auf Überlappung ----------> Logikfehler
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
tries++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//3. Schritt: Wort in Wortsalat schreiben
|
||||||
|
if(check_direction == 1 && check_overlap == 1 && tries < MAX_RAND_TRIES_PER_WORD)
|
||||||
|
{
|
||||||
|
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];
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (direction == 1) // 1 = HORIZONTAL
|
||||||
|
{
|
||||||
|
salad[zeile][spalte + i_set] = words[num_words][i_set];
|
||||||
|
placedWords++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fügt zufällige Buchstaben ein
|
||||||
|
for(int l = 0; l < searchFieldLen; l++){
|
||||||
|
for(int m = 0; m < searchFieldLen; m++ ){
|
||||||
|
if(isalpha(salad[l][m]) == 0 ){
|
||||||
|
// zufällige Buchstaben erzeugen
|
||||||
|
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
int laenge = strlen(alphabet);
|
||||||
|
int zufallszahl = rand()% laenge;
|
||||||
|
char zufallsbuchstabe = alphabet[zufallszahl];
|
||||||
|
// zufällige Buchstaben einfügen
|
||||||
|
salad[l][m] = zufallsbuchstabe;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return placedWords; //platzierte Wörter zurückgeben
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
{
|
{
|
||||||
|
for(int p = 0; p < searchFieldLen; p++)
|
||||||
|
{
|
||||||
|
for(int q = 0; q < searchFieldLen; q++)
|
||||||
|
{
|
||||||
|
printf("%c", salad[p][q]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -36,11 +36,19 @@ int main(int argc, char *argv[])
|
|||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
// TODO:
|
// TODO: (if Schleife implementieren, die das prüft)
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// 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
|
||||||
|
|
||||||
|
if (placedWords == wordCount){
|
||||||
|
showWordSalad(wordSalad, placedWords);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
printf("Error. The words couldn't be placed.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
1
Start_Windows/testwords_delims.txt
Normal file
1
Start_Windows/testwords_delims.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hund,Katze; Maus
|
||||||
0
Start_Windows/testwords_empty.txt
Normal file
0
Start_Windows/testwords_empty.txt
Normal file
3
Start_Windows/testwords_simple.txt
Normal file
3
Start_Windows/testwords_simple.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Apfel
|
||||||
|
Banane
|
||||||
|
Kiwi
|
||||||
Loading…
x
Reference in New Issue
Block a user