#include "game.h" #include #include #include #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 //DONE: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ // enum to make it more clear what de current orientation is typedef enum { HORIZONTAL, VERTIKAL } orientation; // new struct for handing over the entire wordPosition // alignment is HORIZONTAL or VERTICAL // rowOrColumn is the int of the colum or row the word is suposed to be placed in // startingCell is the int of where in the row/colum the first leter of the word suposed to be placed typedef struct { orientation alignment; unsigned int rowOrColumn; unsigned int startingCell; } wordPosition; void initializeWordsalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { int i = 0; int j = 0; for (i = 0; i < searchFieldLen; i++) { for (j = 0; j < searchFieldLen; j++) { salad[i][j] = '='; } } } // Choses a random Position for the current Word wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordLength) { wordPosition position = {0, 0, 0}; //srand(time(NULL)); position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL; position.rowOrColumn = rand()%(searchFieldLen); printf("Wordlength: %d\n", wordLength); position.startingCell = rand()%(searchFieldLen - wordLength); printf("Starting Cell: %d\n", position.startingCell); return position; } // Checks if the current Position is free // returns 1 if the Position is free // returns 0 if the Position is occupied int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPosition position,unsigned int wordLength, unsigned int serchFieldLen) { int i = 0; int letterFound = 0; int positionfree = 0; if (position.alignment == HORIZONTAL) { // checking to see if position is free // by scanning each column in the given row for (i = position.startingCell; (i < serchFieldLen) && !(letterFound); i++) { if (((salad[position.rowOrColumn][i] >= 'A') && (salad[position.rowOrColumn][i] <= 'Z')) || ((salad[position.rowOrColumn][i] >= 'a') && (salad[position.rowOrColumn][i] <= 'z'))) { letterFound = 1; } } } else if (position.alignment == VERTIKAL) { // checking to see if position is free // by scanning each row in the given column for (i = position.startingCell; (i < serchFieldLen) && !(letterFound); i++) { if (((salad[i][position.rowOrColumn] >= 'A') && (salad[i][position.rowOrColumn] <= 'z')) || ((salad[i][position.rowOrColumn] >= 'a') && (salad[i][position.rowOrColumn] <= 'z'))) { letterFound = 1; } } } positionfree = !letterFound; return positionfree; } // Places the current word into the // selected position // returns 1 if sucessfull int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPosition position, const char currentWord[], unsigned int currentWordLen, unsigned int serchFieldLen) { int i = 0; int j = 0; if (position.alignment == HORIZONTAL) { for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++) { salad[position.rowOrColumn][i] = currentWord[j]; printf("%c",currentWord[j]); j++; } salad[position.rowOrColumn][i] = '\0'; } else if (position.alignment == VERTIKAL) { for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++) { salad[i][position.rowOrColumn] = currentWord[j]; printf("%c",currentWord[j]); j++; } salad[i][position.rowOrColumn] = '\0'; } printf("\n"); return 1; } // Places random letters into the // empty fields of the wordSalad void placeRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { int i = 0; int j = 0; for (i = 0; i < searchFieldLen; i++) { for (j = 0; j < searchFieldLen; j++) { if ((salad[i][j] < 'A') || (salad[i][j] > 'z') || ((salad[i][j] < 'a') && (salad[i][j] > 'Z'))) { salad[i][j] = rand()%('Z' - 'A' + 1) + 'A'; } } } } // Creates the word salad by placing words randomly and filling empty spaces // returnes the number of sucessfully placed words 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 i = 0; int j = 0; int k = 0; int positionFound = 0; int placedWords = 0; char nullWord[MAX_WORD_LEN]; char currentWord[MAX_WORD_LEN]; wordPosition currentWordPosition = {0,0,0}; for (k = 0; k < MAX_WORD_LEN; k++) { nullWord[k] = '\0'; } srand(time(NULL)); initializeWordsalad(salad, searchFieldLen); showWordSalad(salad, searchFieldLen); for (i = 1; i <= wordCount; i++) { j = 0; strcpy(currentWord, nullWord); strcpy(currentWord, words[i-1]); do { currentWordPosition = choserandomPosition(searchFieldLen, strlen(currentWord)); positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(currentWord), searchFieldLen); j++; } while ((j < MAX_RAND_TRIES_PER_WORD) && !(positionFound)); if (positionFound) { printf("%d, %d, %d \n", currentWordPosition.alignment, currentWordPosition.rowOrColumn, currentWordPosition.startingCell); printf("%s\n", currentWord); placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen); } } showWordSalad(salad, searchFieldLen); placeRandomLetters(salad, searchFieldLen); return placedWords; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { int i = 0; int j = 0; for (i = 0; i < searchFieldLen; i++) { for (j = 0; j < searchFieldLen; j++) { printf("%c", salad[i][j]); } printf("\n"); } }