From d61687f8df506a47254a94f1dcf7c37c0a489f18 Mon Sep 17 00:00:00 2001 From: sengpielto106561 Date: Sun, 25 Oct 2026 15:13:52 +0100 Subject: [PATCH] implemented game.c --- Startcode/game.c | 56 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/Startcode/game.c b/Startcode/game.c index 1ba6d5a..3ddaadc 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -30,19 +30,67 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi while(counter < wordCount){ //load current word char currentWord[MAX_WORD_LEN]; - strncpy(currentWord) - + 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; - if (salad[x][y] == '0'){ + 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 == 1){ + //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]; + } + break; } - try++; + //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]; + } } } return placedWords;