使用C制作单词搜索游戏

时间:2015-12-01 21:09:15

标签: c logic wordsearch

我一直在尝试制作一个10到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;
}

非常感谢你的帮助!

0 个答案:

没有答案