#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) { srand(time(NULL)); int startpoint_x, startpoint_y, direction, possible, tries; for(int i = 0; i < searchFieldLen; i++){ for(int j = 0; j < searchFieldLen; j++){ salad[i][j] = EMPTY_CHAR; } } for(int i = 0; i < wordCount; i++){ if(strlen(words[i]) <= searchFieldLen){ possible = 0; tries = 0; while(possible == 0 && tries < MAX_RAND_TRIES_PER_WORD){ direction = rand() % 4; possible = 1; switch(direction){ case 0: //X-Positiv startpoint_x = rand() % (searchFieldLen - strlen(words[i]) + 1); startpoint_y = rand() % searchFieldLen; for(int j = 0; j < strlen(words[i]); j++){ if(salad[startpoint_x + j][startpoint_y] != EMPTY_CHAR){ possible = 0; break; } } if(possible == 0){ tries += 1; break; } for(int j = 0; j < strlen(words[i]); j++){ salad[startpoint_x + j][startpoint_y] = words[i][j]; } break; case 1: //X-Negativ startpoint_x = strlen(words[i]) - 1 + rand() % (searchFieldLen - strlen(words[i]) + 1); startpoint_y = rand() % searchFieldLen; for(int j = 0; j < strlen(words[i]); j++){ if(salad[startpoint_x - j][startpoint_y] != EMPTY_CHAR){ possible = 0; break; } } if(possible == 0){ tries += 1; break; } for(int j = 0; j < strlen(words[i]); j++){ salad[startpoint_x - j][startpoint_y] = words[i][j]; } break; case 2: //Y-Positiv startpoint_x = rand() % searchFieldLen; startpoint_y = rand() % (searchFieldLen - strlen(words[i]) + 1); for(int j = 0; j < strlen(words[i]); j++){ if(salad[startpoint_x][startpoint_y + j] != EMPTY_CHAR){ possible = 0; break; } } if(possible == 0){ tries += 1; break; } for(int j = 0; j < strlen(words[i]); j++){ salad[startpoint_x ][startpoint_y + j] = words[i][j]; } break; case 3: //Y-Negativ startpoint_x = rand() % searchFieldLen; startpoint_y = strlen(words[i]) - 1 + rand() % (searchFieldLen - strlen(words[i]) + 1); for(int j = 0; j < strlen(words[i]); j++){ if(salad[startpoint_x][startpoint_y - j] != EMPTY_CHAR){ possible = 0; break; } } if(possible == 0){ tries += 1; break; } for(int j = 0; j < strlen(words[i]); j++){ salad[startpoint_x][startpoint_y - j] = words[i][j]; } break; } } } } for(int i = 0; i < searchFieldLen; i++){ for(int j = 0; j < searchFieldLen; j++){ if(salad[i][j] == EMPTY_CHAR){ salad[i][j] = rand() % 26 + 'A'; } } } return 0; }