#include "game.h" #include #include #include #include #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 #define MAX_NUMBER_OF_WORDS 100 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 placedWords = 0; srand(time(NULL)); placedWords = fillSalad(salad, searchFieldLen, words, wordCount); showWordSalad(salad); printf("\nPlacedWords: %d\n", placedWords); return placedWords; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]) { printf("\nWordSalad:\n"); for(unsigned int i=0; i < MAX_SEARCH_FIELD_LEN; i++) { for(unsigned int j=0; j< MAX_SEARCH_FIELD_LEN; j++) { printf("%c ", salad[i][j]); } printf("\n"); } } // Decides wether to print horizontally or vertically // returns 1, when horizontal int printHorizontal() { return rand() % 2; } // checks wether after all words are filled, empty spaces are left // returns 1, when empty spaces are left int emptyPlaces(unsigned int wordCount) { if(wordCount < MAX_NUMBER_OF_WORDS) { return 1; } return 0; } int fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { int addedWords = 0; // empties salad for(unsigned int i = 0; i < MAX_SEARCH_FIELD_LEN; i++) { for(unsigned int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) { salad[i][j] = '\0'; } } // inserts words for(unsigned int w = 0; w < wordCount; w++) { //geht words array linear durch, damit jedes Wort ueberprueft wird int tries = 0; int placed = 0; while(tries < MAX_RAND_TRIES_PER_WORD && !placed) { //solange die max. Anzahl an Versuchen pro Wort nicht ueberschritten wird und das Wort nicht plaziert wurde: int horizontal = printHorizontal(); //horozontal oder vertikal? if(horizontal) { int row = rand() % searchFieldLen; //sucht random row aus unsigned int wordLen = strlen(words[w]); if(wordLen <= searchFieldLen) { //wenn wort von der Laenge her in Zeile passt int startCol = rand() % (searchFieldLen - wordLen + 1); //bestimmt einen zufaelligen Startpunkt in der row zhwischen 0 und max. Index, damit das WOrt noch passt int canPlace = 1; for(unsigned int i = 0; i < wordLen; i++) { //schaut, ob der Platz in jeder Zelle in salad frei ist oder ob Ueberlappung zweier Woerter moeglich ist if(salad[row][startCol + i] != '\0') { if(salad[row][startCol + i] != words[w][i]) { canPlace = 0; break; } } } if(canPlace) { //Wort kann plaziert werden -> fuellt Wort in random row ab random Zelle mit Wort und addiert erfolgreich plazierte Woerter for(unsigned int i = 0; i < wordLen; i++) { salad[row][startCol + i] = words[w][i]; } placed = 1; addedWords++; } } } else { // vertically int col = rand() % searchFieldLen; unsigned int wordLen = strlen(words[w]); if(wordLen <= searchFieldLen) { int startRow = rand() % (searchFieldLen - wordLen + 1); int canPlace = 1; for(unsigned int i = 0; i < wordLen; i++) { if(salad[startRow + i][col] != '\0') { if(salad[startRow + i][col] != words[w][i]) { canPlace = 0; break; } } } if(canPlace) { for(unsigned int i = 0; i < wordLen; i++) { salad[startRow + i][col] = words[w][i]; } placed = 1; addedWords++; } } } tries++; } } fillRandom(salad); return addedWords; } // if emptyPlaces, fill these with random letters void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]) { for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) { for (int h = 0; h < MAX_SEARCH_FIELD_LEN; h++) { if (salad[j][h] == '\0') { char letter = 'A' + rand() % 26; // 0–25 → 'A'–'Z' salad[j][h] = letter; } } } }