#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], // salad ist Spielfeld 2D Array mit 20*20 // Buchstaben unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { srand(time(NULL)); enum Richtung { HORIZONTAL, VERTIKAL }; // wipe field // array auf emptychar for (int x = 0; x < searchFieldLen; x++) { for (int y = 0; y < searchFieldLen; y++) { salad[y][x] = EMPTY_CHAR; } } int placedWords = 0; // variable platzierte Wörter // place words // for schleife mit wortzahl = 0 bis wordCount for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) { // strlen mit Wortlänge int wortLen = strlen(words[wortNummer]); int platziert = 0; int versuch = 0; // rand % 2 vertikal oder horizontal // rand % searchFieldLen um Ort zu generieren // Länge überprüfen und überprüfen, ob wort sich überschneidet ist nicht // randomtries while (versuch < MAX_RAND_TRIES_PER_WORD && platziert == 0) { enum Richtung r = (rand() % 2) ? HORIZONTAL : VERTIKAL; int x = rand() % searchFieldLen; int y = rand() % searchFieldLen; int fits = 1; if (r == HORIZONTAL) { if ((y + wortLen) > (searchFieldLen)) { fits = 0; } if (fits) { for (int i = 0; i < wortLen; i++) { char var = salad[y + i][x]; if (var != EMPTY_CHAR) { fits = 0; } } } if (fits != 0) { for (int i = 0; i < wortLen; i++) { salad[y + i][x] = words[wortNummer][i]; } platziert = 1; placedWords++; } } else if (r == VERTIKAL) { if ((x + wortLen) > searchFieldLen) { fits = 0; } if (fits) { for (int i = 0; i < wortLen; i++) { char var = salad[y][x + i]; if (var != EMPTY_CHAR) { fits = 0; } } } if (fits != 0) { for (int i = 0; i < wortLen; i++) { salad[y][x + i] = words[wortNummer][i]; } platziert = 1; placedWords++; } } versuch++; } } for (int i = 0; i < searchFieldLen; i++) { for (int j = 0; j < searchFieldLen; j++) { if (salad[i][j] == EMPTY_CHAR) { salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; } } } return placedWords; // emptychar rand() % ('Z' - 'A' + 1) + 'A' } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { for (int i = 0; i < searchFieldLen; i++) { for (int j = 0; j < searchFieldLen; j++) { printf("%c", salad[i][j]); } } }