#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) { //Initialisiert den seed für rand() srand(time(NULL)); //Initialisiere spielfeld auf '0' // TODO: memset() //memset(salad, '0', MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN); for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){ for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){ salad[i][j] = '0'; } } int placedWords = 0; //Iterate over words for(int counter = 0; counter < wordCount; counter++){ size_t len = strlen(words[counter]); //try to place a word for(int try = 0; try < 10; try++){ int x = rand() % MAX_SEARCH_FIELD_LEN; int y = rand() % MAX_SEARCH_FIELD_LEN; int dir = rand() % 2; // cant place vertically if(dir == 1 && MAX_SEARCH_FIELD_LEN < (y + (int) len -1)){ dir = 0; } //cant place horizontally if((x + (int) len -1) > MAX_SEARCH_FIELD_LEN){ continue; } //place word vertically if(dir == 1){ //see if all neccessary spaces are empty bool cont = false; for (int i = 0; i < (int)len; i++){ if(salad[x][y+i] != '0'){ cont = true; break; } } //if not all spaces are empty try again if(cont) continue; //insert currentWord into playing field for (int i = 0; i < (int)len; i++){ salad[x][y+i] = words[counter][i]; } placedWords++; break; }else{ //place currentWord horizontally //see if all neccessary spaces are emtry bool cont = false; for (int i = 0; i < (int)len; i++){ if(salad[x+1][y] != '0'){ cont = true; break; } } //if not all spaces are empty try again if(cont) continue; //place currentWord for (int i = 0; i < (int)len; i++){ salad[x+1][y] = words[counter][i]; } placedWords++; break; } } } for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){ for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){ if(salad[i][j] == '0'){ char c = 'A' + (rand() % 26); salad[i][j] = c; } } } return placedWords; }