#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 */ unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Number); unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_LEN], char salad[][MAX_SEARCH_FIELD_LEN], unsigned int position); void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge); void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]); void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField); // 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) { unsigned int maxLength = 0; unsigned int sizeOfField = 0; unsigned int returnValue = 0; // search for longest word maxLength = findmaxLength(words, wordCount); // Size of Field! if (searchFieldLen < maxLength) sizeOfField = maxLength; else sizeOfField = searchFieldLen; initializeField(sizeOfField, salad); // random srand(time(NULL)); // for all words put them in for (unsigned int i = 0; i < wordCount; i++){ if (findAPlace(sizeOfField,words,salad,i) == 1) { returnValue += 1; } } fillTheRest(salad, sizeOfField); return returnValue; } unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Number) { unsigned int max = 0; for (unsigned int i = 0; i < Number; i++) { for (unsigned int j = 0; j < MAX_WORD_LEN; j++) { if(wordlist[i][j] == '\0') { if (max < j) max = j; break; } } } return max; } // unsigned int free(unsigned int richtung, unsigned int laenge, unsigned int x, unsigned int y, char spielfeld [][MAX_SEARCH_FIELD_LEN], unsigned int groesse) // { // } void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]) { for (unsigned int j = 0; j < groesse; j++){ for (unsigned int i = 0; i < groesse; i++) { field[j][i] = EMPTY_CHAR; } } } unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_LEN], char salad[][MAX_SEARCH_FIELD_LEN], unsigned int position) { unsigned int direction, length = 0; unsigned int maxLineColumn = 0; unsigned int xNumber, yNumber = 0; direction = rand() % 2; // choose 0 = X oder 1 = Y - direction length = strlen(words[position]); maxLineColumn = (sizeOfField + 1) - length; for (unsigned int j = 0; j < MAX_RAND_TRIES_PER_WORD; j++) { if (direction == 0){ yNumber = rand() % maxLineColumn; xNumber = rand() % sizeOfField; } // Maximale Werte je nach X - Y - Richtung else { xNumber = rand() % maxLineColumn; yNumber = rand() % sizeOfField; } if(direction == 1){ unsigned int a = xNumber; while (a < (xNumber + length) && salad[a][yNumber] == 0) a++; if (a == xNumber+length) { putInTheWord(words, position, salad, xNumber, yNumber, direction, length); return 1; } } else{ unsigned int a = yNumber; while (a < (yNumber + length) && salad[xNumber][a] == 0) a++; if (a == yNumber+length) { putInTheWord(words, position, salad, xNumber, yNumber, direction, length); return 1; } } // if (j == (MAX_RAND_TRIES_PER_WORD -1)){ // if (search() == 1) // putInTheWord(words, position, salad, xNumber, yNumber, direction, length); // return 1; // } } return 0; } void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge) { if (richtung == 0){ for (unsigned int i=0; i < laenge; i++) tabelle[X][(i+Y)] = words[wort][i]; } else { for (unsigned int i=0; i < laenge; i++) tabelle[X+i][(Y)] = words[wort][i]; } } void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField) { for (unsigned int i = 0; i < sizeOfField; i++) { for (unsigned int j = 0; j < sizeOfField; j++) { if (field[i][j] == 0) { char random = 'A' + rand() % 26; field[i][j] = random; } printf("%1c ", field[i][j]); } printf("\n"); } }