C:如何在字符网格中插入字符串

时间:2017-12-21 13:30:40

标签: c puzzle

我正在尝试制作一个搜索词拼图,程序随机选择4个单词供用户查找。到目前为止,我已经创建了具有随机字符的网格,并列出了在网格下方打印的随机单词列表。我曾尝试使用strcpy在网格中插入单词,但未能成功。任何帮助,将不胜感激。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

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

int main(void){

int randomNum = 0;
char grid[11][11];
const char *chosenWords[4];
const char *words[20]={"DOG", "CAT", "PENCIL", "BOOK", "PAPER", "CLOUD", "RAIN", "SUN", "MOON", "MIRROR", "BUBBLE", "GOLD", "SILVER", "APPLE", "KIWI", "RED", "BLUE", "GREEN", "YELLOW"};

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

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

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



getchar();
return 0;
}

0 个答案:

没有答案