1

我一直在尝试制作一个固定大小为 10 x 10 的单词搜索游戏。到目前为止,我已经成功地在单词搜索中生成了随机字母,以及4 个随机隐藏的单词。在游戏过程中被发现。但是,我在如何将4 个生成的随机词放入词搜索本身时遇到了一些麻烦,同时考虑到隐藏词必须在每一轮游戏中处于随机位置。单词必须是水平的、垂直的或对角的。另外,我应该如何在随机位置生成 4 个单词,同时确保这些单词不会相互重叠

到目前为止,这是我的代码:

char getRandomCharacter();
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

char getRandomCharacter(){
    int r = (rand() % 26) + 65;
    return (char)r;
}

int main(void){

    int randomNum;
    char wordSearch[11][11];
    const char *takenWords[4];
    const char *words[20]={"DOG", "CAT", "ELEPHANT", "CROCODILE", "HIPPOPOTAMUS", "TORTOISE", "TIGER", "FISH", "SEAGULL", "SEAL", "MONKEY", "KANGAROO", "ZEBRA", "GIRAFFE", "RABBIT", "HORSE", "PENGUIN", "BEAR", "SQUIRREL", "HAMSTER"};

    printf("\n\tA\t\tB\t\tC\t\tD\t\tE\t\tF\t\tG\t\tH\t\tI\t\tJ\n");
    for(int i=1; i<11; i++){
        printf("\n%d\t", i-1);

        for(int j=0; j<10; j++){
            char c=getRandomCharacter();
            wordSearch[i][j]=c;
            printf("%c\t\t", *(&(wordSearch[i][j])));
        }
        printf("\n\n");
    }

    srand(time(NULL));

    for(int i=0; i<4; i++){
        int flag=0;
        do{
            randomNum = (rand()%20);
            takenWords[i]=words[randomNum];
            flag=0;
            for(int j=0;j<i;j++){
                if(strcmp(words[randomNum],takenWords[j])==0)flag=1;
            }
        }while(flag);
        //wordSearch[(rand()%20)+1][(rand()%20)+1];
        printf("%s\n", words[randomNum]);
    }
    getchar();
    return 0;
}

非常感谢你的帮助!

4

0 回答 0