From ea276d358e7bb9fd88fcd7710856d9a3eb7fb510 Mon Sep 17 00:00:00 2001 From: Tobias Date: Mon, 26 Oct 2026 15:28:53 +0100 Subject: [PATCH] =?UTF-8?q?game.c=20angefangen=20Startpunkte=20f=C3=BCr=20?= =?UTF-8?q?worte=20raussuchen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Startcode/game.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/Startcode/game.c b/Startcode/game.c index 5210c2c..868f39a 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -12,8 +12,117 @@ // Creates the word salad by placing words randomly and filling empty spaces -//test jonathan 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 < MAX_SEARCH_FIELD_LEN; i++){ + for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){ + salad[i][j] = '0'; + } + } + + for(int i = 0; i < wordCount; i++){ + possible = 0; + tries = 0; + while(possible == 0 && tries <= MAX_RAND_TRIES_PER_WORD){ + direction = rand() % 4; + + 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] != '0'){ + possible = 0; + break; + } + else { + possible = 1; + } + } + + 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 = searchFieldLen - 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] != '0'){ + possible = 0; + break; + } + else { + possible = 1; + } + } + + 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] != '0'){ + possible = 0; + break; + } + else { + possible = 1; + } + } + + 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 = searchFieldLen - rand() % (searchFieldLen - strlen(words [i])) + 1; + for(int j = 0; j < strlen(words[i]); j++){ + if(salad[startpoint_x][startpoint_y - j] != '0'){ + possible = 0; + break; + } + else { + possible = 1; + } + } + + 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; + } + } + + } + }