implemented game.c
This commit is contained in:
1 parent
d368eea2f3
commit
d61687f8df
1 file changed
+51
-3
+51
-3
@@ -30,19 +30,67 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
while(counter < wordCount){
|
while(counter < wordCount){
|
||||||
//load current word
|
//load current word
|
||||||
char currentWord[MAX_WORD_LEN];
|
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
|
//try to place a word
|
||||||
int try = 0;
|
int try = 0;
|
||||||
while(try < 10){
|
while(try < 10){
|
||||||
int x = rand() % MAX_SEARCH_FIELD_LEN;
|
int x = rand() % MAX_SEARCH_FIELD_LEN;
|
||||||
int y = 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try++;
|
//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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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;
|
return placedWords;
|
||||||
|
|||||||
Reference in new issue
Block a user