#include "game.h" #include #include #include #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 typedef enum Richtung { VERTIKAL = 0; HORIZONTAL = 1; }; //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 unsigned int counter = 0; while(counter < wordCount){ //load current word char currentWord[MAX_WORD_LEN]; if(strncpy(currentWord, words[counter], MAX_WORD_LEN) == NULL){ //strncpy failed continue; } size_t len = strlen(currentWord); //try to place a word int try = 0; while(try < 10){ int x = rand() % MAX_SEARCH_FIELD_LEN; int y = rand() % MAX_SEARCH_FIELD_LEN; int dir = rand() % 2; if ((MAX_SEARCH_FIELD_LEN - x) < len){ if((MAX_SEARCH_FIELD_LEN - )y < len){ // cant place word here (too far in the bottom right corner) try++; continue; } } //place word vertically if(dir == HORIZONTAL){ //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; try++; 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] = currentWord[i]; } placedWords++; break; } //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; try++; 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] = currentWord[i]; } placedWords++; } } 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; }