#include "game.h" #include #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 */ int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[]) { for(size_t i = 0 ; i < strlen(wort); i++ ) { short xoffset = (richtung) ? i : 0; short yoffset = (richtung) ? 0 : i; if (!(salad[x+xoffset][y+yoffset]=='#' || salad[x+xoffset][y+yoffset]==wort[i])) { return(-1); } } return(1); } // 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) { unsigned int placedCount = 0; for(short i = 0; i < searchFieldLen; i++) { for(short j = 0; j < searchFieldLen; j++) { salad[i][j] = '#'; } } srand(time(NULL)); for(short i = wordCount;i>0;i--) { size_t platzbedarf = strlen(words[i-1]); if (platzbedarf > searchFieldLen) { printf("%s konnte nicht eingefuegt werden da es groesser als das Feld ist\n",words[i-1]); continue; } short x,y,waagrecht; int zuoft = enoughTries(searchFieldLen,0.99); int versuche = 0; do { versuche++; short max = searchFieldLen - platzbedarf; short position1 = rand() % (max +1); short position2 = rand() % (searchFieldLen); waagrecht = rand()%2; x = (waagrecht) ? position1:position2; y = (waagrecht) ? position2:position1; }while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1 && versuche<=zuoft); if(versuche > zuoft) { printf("%s passt nicht mehr ins Gitter\n",words[i-1]); continue; } for(size_t k = 0 ; k < strlen(words[i-1]); k++ ) { short xoffset = (waagrecht) ? k : 0; short yoffset = (waagrecht) ? 0 : k; salad[x+xoffset][y+yoffset]=words[i-1][k]; } placedCount++; } return placedCount; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { for (unsigned int y = 0; y < searchFieldLen; y++) { for (unsigned int x = 0; x < searchFieldLen; x++) { printf("%c ", salad[x][y]); // oder printf("%c", salad[x][y]); ohne Leerzeichen } printf("\n"); // neue Zeile nach jeder Zeile } } int enoughTries(double feldgroesse, double sicherheit) { double moeglichkeiten = feldgroesse * feldgroesse; // Anzahl der Möglichkeiten double c = -log(-log(sicherheit)); // Zusatzterm aus Zielwahrscheinlichkeit double versuche = moeglichkeiten * (log(moeglichkeiten) + c); // Coupon-Collector-Formel return (int)ceil(versuche); }