implemented game.c

This commit is contained in:
sengpielto106561 committed 2026-10-25 15:13:52 +01:00
1 parent d368eea2f3
commit d61687f8df
1 file changed
+52 -4
+52 -4
View File
@@ -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;