#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 */ // 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)); // wipe field int placedWords = 0; for (int i = 0; i < searchFieldLen; i++) { for (int j = 0; j < searchFieldLen ; j++) salad[i][j] = EMPTY_CHAR; } // place Words for (int wordNumber = 0; wordNumber < wordCount; wordNumber++) { int wordlength = strlen(words[wordNumber]); int placed = 0; for (int try = 0; (try < MAX_RAND_TRIES_PER_WORD) && !placed; try++) { int horizontal = rand() % 2; // 0 vertikal --> 1 horizontal int x = rand() % searchFieldLen; int y = rand() % searchFieldLen; if (horizontal) { if (x + wordlength > searchFieldLen) // word does not fit { continue; } int fits = 1; for (int i = 0; i < wordlength; i++) // position already occupied by other word { if (salad[y][x + i] != EMPTY_CHAR) { fits = 0; break; } } if (fits) { for (int i = 0; i < wordlength; i++) { salad[y][x + i] = words[wordNumber][i]; } placed = 1; placedWords++; } } else { if ((y + wordlength) > searchFieldLen) { continue; } int fits = 1; for (int i = 0; i < wordlength; i++) // position already occupied by other word { if (salad[y + i][x] != EMPTY_CHAR) { fits = 0; break; } } if (fits) { for (int i = 0; i < wordlength; i++) { salad[y + i][x] = words[wordNumber][i]; } placed = 1; placedWords++; } } } } // fill rest with random characters for (int i = 0; i < searchFieldLen; i++) { for (int j = 0; j < searchFieldLen; j++) { if (salad[i][j] == EMPTY_CHAR) { salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; // 90 ist Z in ASCII und A ist 65 } } } return placedWords; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { for (int i = 0; i < searchFieldLen; i++) { for (int j = 0; j < searchFieldLen; j++) { printf("%c", salad[i][j]); } } }