Предложение, слова могут быть размещены горизонтально (слева направо), вертикально (сверху вниз), по диагонали, всегда слева направо, но сверху вниз или снизу вверх
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define WIDTH 16
#define HEIGHT 16
#define NWORDS 6
char wordsearch[HEIGHT][WIDTH];
/* horizontaly */
int canPlaceH(const char * word, int i, int j)
{
if ((strlen(word) + j) > WIDTH)
return 0;
do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
j += 1;
} while (*++word);
return 1;
}
void placeH(const char * word, int i, int j)
{
do {
wordsearch[i][j++] = *word;
} while (*++word);
}
/* verticaly */
int canPlaceV(const char * word, int i, int j)
{
if ((strlen(word) + i) > HEIGHT)
return 0;
do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
i += 1;
} while (*++word);
return 1;
}
void placeV(const char * word, int i, int j)
{
do {
wordsearch[i++][j] = *word;
} while (*++word);
}
/* diagonal up */
int canPlaceDU(const char * word, int i, int j)
{
int ln = strlen(word);
if (((ln + j) > WIDTH) || ((i - ln) < 0))
return 0;
do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
i -= 1;
j += 1;
} while (*++word);
return 1;
}
void placeDU(const char * word, int i, int j)
{
do {
wordsearch[i--][j++] = *word;
} while (*++word);
}
/* diagonal down */
int canPlaceDD(const char * word, int i, int j)
{
int ln = strlen(word);
if (((ln + j) > WIDTH) || ((i + ln) > HEIGHT))
return 0;
do {
if ((wordsearch[i][j] != 0) && (wordsearch[i][j] != *word))
return 0;
i += 1;
j += 1;
} while (*++word);
return 1;
}
void placeDD(const char * word, int i, int j)
{
do {
wordsearch[i++][j++] = *word;
} while (*++word);
}
void fillWordsearch(const char ** a, int sz)
{
/* first step add words from a */
const char * used[NWORDS - 1] = { NULL }; /* to not get two times the same word */
for (int w = 0; w != NWORDS; ++w) {
/* random choice of a word not yet used */
const char * word;
for (;;) {
word = a[rand() % sz];
int i;
/* check not yet used */
for (i = 0; i != w; ++i)
if (!strcmp(used[i], word))
break;
if (i == w) {
/* not yet used */
used[w] = word;
break;
}
}
/* random placement */
int i, j, d;
typedef int (*canFunc)(const char *, int, int);
typedef void (*placeFunc)(const char *, int, int);
const canFunc canPlace[] = { canPlaceH, canPlaceV, canPlaceDU, canPlaceDD };
const placeFunc place[] = { placeH, placeV, placeDU, placeDD };
do {
i = rand() % HEIGHT;
j = rand() % WIDTH;
d = rand() % 4;
} while (!(*canPlace[d])(word, i, j));
(*place[d])(word, i, j);
#ifdef DEBUG
printf("place %s on %d %d direction %d\n", word, i, j, d);
#endif
}
#ifdef DEBUG
for (int i = 0; i != HEIGHT; ++i) {
for (int j = 0; j != WIDTH; ++j)
putchar((wordsearch[i][j] == 0) ? '.' : wordsearch[i][j]);
putchar('\n');
}
putchar('\n');
#endif
/* second step fill not yet set characters with random lowercase letters */
int i,j;
for (i = 0; i < HEIGHT; i++)
for (j = 0; j != WIDTH; ++j)
if (wordsearch[i][j] == 0)
wordsearch[i][j] = 'a' + rand() % ('z' - 'a' + 1);
}
int main()
{
const char *animalArray[] = {
"lynx",
"kitten",
"cheetah",
"ape",
"doe",
"reindeer",
"whale",
"baboon",
"skunk",
"dugong",
"elephant",
"anteater",
"chameleon",
"lizaed",
"horse"
};
const char *colourArray[] = {
"red",
"green",
"blue",
"black",
"pink",
"yellow",
"brown",
"orange",
"purple",
"black",
"white",
"cyan",
"maroon",
"magenta",
"grey"
};
const char *videogameArray[] = {
"fortnite",
"fifa",
"hytale",
"soma",
"prey",
"destiny",
"titanfall",
"woldenstein",
"battlefield",
"fallout",
"tekken",
"skyrim",
"dishonored",
"uncharted",
"anthem"
};
const char *sportsArray[] = {
"basketball",
"football",
"cricket",
"wrestling",
"fencing",
"rowing",
"volleyball",
"baseball",
"hockey",
"racing",
"golf",
"bobsleigh",
"curling",
"snowboarding",
"bowling"
};
const char *countryArray[] = {
"england",
"ireland",
"china",
"wales",
"bangladesh",
"maldives",
"slovenia",
"uruguay",
"colombia",
"samoa",
"jamaica",
"malta",
"bulgaria",
"armenia",
"gamnbia"
};
printf("choose category 1=animal 2=colour 3=video-game 4=sport 5=country : ");
int i;
if ((scanf("%d", &i) != 1) || (i < 1) || (i > 5))
return -1;
srand(time(NULL));
switch (i) {
case 1:
fillWordsearch(animalArray, sizeof(animalArray)/sizeof(*animalArray));
break;
case 2:
fillWordsearch(colourArray, sizeof(colourArray)/sizeof(*colourArray));
break;
case 3:
fillWordsearch(videogameArray, sizeof(videogameArray)/sizeof(*videogameArray));
break;
case 4:
fillWordsearch(sportsArray, sizeof(sportsArray)/sizeof(*sportsArray));
break;
default:
fillWordsearch(countryArray, sizeof(countryArray)/sizeof(*countryArray));
break;
}
/* print result */
for (i = 0; i != HEIGHT; ++i) {
for (int j = 0; j != WIDTH; ++j)
putchar(wordsearch[i][j]);
putchar('\n');
}
return 0;
}
При компиляции с определенным DEBUG, который указывает, какое слово помещается и как, а также печатает поиск слова только с вставленными словами.
Компиляция и некоторые исполнения:
pi@raspberrypi:/tmp $ gcc -DDEBUG -g c.c
pi@raspberrypi:/tmp $ ./a.out
choose category 1=animal 2=colour 3=video-game 4=sport 5=country : 1
place skunk on 14 10 direction 0
place elephant on 0 15 direction 1
place lizaed on 6 11 direction 1
place cheetah on 13 2 direction 0
place chameleon on 4 3 direction 0
place horse on 12 3 direction 0
...............e
...............l
...............e
...............p
...chameleon...h
...............a
...........l...n
...........i...t
...........z....
...........a....
...........e....
...........d....
...horse........
..cheetah.......
..........skunk.
................
xjzcgwbnnxlmaaje
wgssltuqwsuozgjl
ldtmfzijoxizmkze
vgipavkvmhnixmpp
hzuchameleonalah
bvlpuvqggpdoztpa
vdkjmzrklirlmdrn
zfswqjqnzxtiqait
qvzlzjwlinvzvymj
bfigzgyiwlhatfyw
pqrodqzzejoezgmo
rnvbvvhtddfdnzmn
vbdhorsemrgfshjd
vkcheetahjkaczwd
vrkowptjofskunkw
jibybjdvqoyoyjyb
pi@raspberrypi:/tmp $ ./a.out
choose category 1=animal 2=colour 3=video-game 4=sport 5=country : 2
place brown on 6 0 direction 1
place magenta on 12 5 direction 2
place yellow on 8 4 direction 3
place grey on 13 12 direction 0
place pink on 6 8 direction 3
place black on 5 11 direction 2
................
...............k
..............c.
.............a..
............l...
...........b....
b.......p..a....
r........it.....
o...y....nn.....
w....e..e..k....
n.....lg........
......al........
.....m..o.......
.........w..grey
................
................
syxxaavuzbbvskbu
lcmcvehexwrvkylk
cyizyixuiyxfshcg
mtkaxfhelecdbamd
mqcuscfpzppyluhg
cvbmyathgflbikmx
bpantwfwpbwaxsnv
rmudosgcqitgwzod
okznyywcynnlwutu
wyrtqefoefbkuvta
nyljuklgxxwymwuo
uimrbialrsbjxsoz
mmuokmizoeuzdtxp
psfmasddawwvgrey
efvwwwiisysayyxx
dqusfoxrqrwtusry