diff --git a/Startcode/game.c b/Startcode/game.c index 77be689..cca6f86 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -7,103 +7,115 @@ #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 */ +int placeWord(int len, int column, int row, + char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + int searchFieldLen, const char* word, int axis); // 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) { - if (searchFieldLen == 0 ||searchFieldLen > MAX_SEARCH_FIELD_LEN) - {return 0;} + if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN) + return 0; - int placedWOrds = 0; - srand((unsigned int)time(NULL)); + int placedWords = 0; - //Spielfeld leeren: - for (int r = 0; r < searchFieldLen; r++ ) - { - for (int c = 0; c < searchFieldLen; c++) { - salad[r][c] = EMPTY_CHAR; - } - } + srand(time(NULL)); - //Wörter platzieren - for (int w = 0; w < wordCount; w++) - { - int len = strlen(words[w]); //Wortlänge festgelegt - - for (int atempts = 0; atempts < MAX_RAND_TRIES_PER_WORD; atempts++) - { - //Muss in for-Schleife sein damit immer neu erzeugt wird - int axis = rand() % 2; - int row = rand() % searchFieldLen; - int column = rand() % searchFieldLen; - int error = 0; + // Spielfeld leeren: + for (int x = 0; x < searchFieldLen; x++) + for (int y = 0; y < searchFieldLen; y++) + salad[x][y] = EMPTY_CHAR; + // Wörter platzieren + for (int w = 0; w < wordCount; w++) + { + int wordlenght = strlen(words[w]); // Wortlänge festgelegt + if (wordlenght == 0 || wordlenght > searchFieldLen) + continue; - if (axis == 0) //Vertical~~~~~~~~~~~ - { - //Available Space Check & Wordcollision Avoidance////// - for (int i = 0; i < len; i++) + for (int attempts = 0; + attempts < MAX_RAND_TRIES_PER_WORD; + attempts++) + { + // Muss in for-Schleife sein damit immer neu erzeugt wird + int axis = rand() % 2; + int row = rand() % searchFieldLen; + int column = rand() % searchFieldLen; + + int success = placeWord( + wordlenght, column, row, salad, + searchFieldLen, words[w], axis); + + if (success) { - if (len + row > searchFieldLen || salad[row+i][column] != EMPTY_CHAR){ - error = 1; - break; - } + placedWords++; + break; } - //Write Word - if (error == 0){ - for (int i = 0; i < len; i++) - { - salad[row+i][column] = words[w][i]; - } - placedWOrds++; - break; - }else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );} + printf("Platzierung zum %d. Mal fehlgeschlagen!\n", + attempts + 1); + } + } - - - - }else if (axis == 1){ //Horizontal~~~~~~~~~~~~~~~ - - //Available Space Check & Wordcollision Avoidance - for (int i = 0; i < len; i++) + //Fill empty spaces + for (int r = 0; r < searchFieldLen; r++) + { + for (int c = 0; c < searchFieldLen; c++) + { + if (salad[r][c] == EMPTY_CHAR) { - if (len + column > searchFieldLen || salad[row][column+i] != EMPTY_CHAR){ - error = 1; - break; - } + salad[r][c] = 'A' + rand() % 26; } + } + } + printf("%d placed" ,placedWords); + return placedWords; +} - //Write Word - if (error == 0){ - for (int i = 0; i < len; i++) - { - salad[row][column+i] = words[w][i]; - } - placedWOrds++; - break; - }else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );} - } - } +int placeWord(int len, int column, int row, + char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], + int searchFieldLen, const char* word, int axis) +{ + // Available space check & word collision avoidance + for (int i = 0; i < len; i++) + { + if (axis == 0) // Horizontal + { + if (column + len > searchFieldLen || + salad[row][column + i] != EMPTY_CHAR) + { + return 0; + } + } + else // Vertical + { + if (row + len > searchFieldLen || + salad[row + i][column] != EMPTY_CHAR) + { + return 0; + } + } + } - } + //Write Word + for (int i = 0; i < len; i++) + { + if (axis == 0) // Horizontal + { + salad[row][column + i] = word[i]; + } + else // Vertical + { + salad[row + i][column] = word[i]; + } + } - //Fill empty spaces - for (int r = 0; r < searchFieldLen; r++) - { - for (int c = 0; c < searchFieldLen; c++) - { - if (salad[r][c] == EMPTY_CHAR) - { - salad[r][c] = 'A' + rand() % 26; - } - } - } - return placedWOrds; + return 1; } \ No newline at end of file