#include "game.h" #include #include #include #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 //TODO: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ //words und salad sind char arrays --> laengstes wort sind 100 zeichen // 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) { srand(time(NULL)); fillSalad(salad, searchFieldLen, words, wordCount); showWordSalad(salad, searchFieldLen); return 1; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { printf("\nWordSalad:\n"); for(int i=0; i < MAX_SEARCH_FIELD_LEN; i++) { for(int j=0; j< MAX_SEARCH_FIELD_LEN; j++) { printf("%c\t", salad[i][j]); } printf("\n"); } } // Decides wether to print horizontally or vertically // returns 1, when horizontal int printHorizontal(unsigned int wordCount) { int rand = rand() % (wordCount - (-wordCount) + 1) - wordCount; if(rand > 0) { return 1; } return 0; } // 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_SEARCH_FIELD_LEN) { return 1; } return 0; } // fills salad array with max. words from word array either horizontally or vertically void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { for(int i= 0; i< searchFieldLen; i++) //for every word in salad, it is determined wether hor or ver { if(printHorizontal(wordCount)) //horizontal { for(int j= 0; j < searchFieldLen; j++) //sucht freie Zeile in salad { if(salad[j][0] == NULL) { int numWord = whichWord(words, wordCount); //determines, which word will be written in salad if(numWord != -5){ fillWordinRow(salad, searchFieldLen, words, wordCount, numWord, j); // fills word horizontally in salad and deletes the word afterwards } else{ fillRandom(salad, searchFieldLen, words, wordCount, emptyPlaces(wordCount)); } } } } else{ //vertical, immer von oben nach unten for(int j = 0; j< searchFieldLen; j++) { if(salad[0][j] == NULL) { int numWord = whichWord(words, wordCount); if(numWord != -5){ fillWordinColumn(salad, searchFieldLen, words, wordCount, numWord, j); } else{ fillRandom(salad, searchFieldLen, words, wordCount, emptyPlaces(wordCount)); } } } } } } // Determines, which word from words is filled into salad int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount) { for(int i = 0; i< MAX_NUMBERS_OF_WORDS; i++){ int numWord = rand()% (wordCount + 1); if(words[numWord][0] != NULL) { return numWord; //return random row, which has not been used yet } return -5; } } //Fills word in salad and deletes row of words of the used word void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int row) { for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++) { for(int g = 0; g < MAX_WORD_LEN; g++) { salad[row][h] = words[numWord][g]; words[numWord][g] = NULL; } } } //Fills word in salad and deletes row of words of the used word, vertical from top to bottom void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int column) { for(int h = 0; h< MAX_SEARCH_FIELD_LEN; h++) { for(int g = 0; g < MAX_WORD_LEN; g++) { salad[h][column] = word[numWord][g]; words[numWord][g] = NULL; } } } // if emptyPlaces, fill these with random letters void fillRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int empty) { if(empty) { for(int j=0; j < MAX_SEARCH_FIELD_LEN; j++) { for(int h= 0; h < MAX_SEARCH_FIELD_LEN; h++) { if(salad[j][h] == NULL) { int num = rand()%(26 - 1 + 1) - 1; char letter = 64 + num; salad[j][h] = letter; } } } } }