我制作了一个益智游戏,现在我想在游戏中附加一个字典文件,用于检查拼写并返回拼写是否正确。
#include<stdio.h>
#include<string.h>
#include <time.h>
#include <stdlib.h>
int printRandoms(int lower, int upper, int count);
int main()
{
printf("n Hello !" Welcome to Boggle game. " n");
printf(" n Start Making as many Words as you can. n");
printf("n Board number>> ");
char arr_1[4][4]= {{'D','G','H','I'},{'K','L','P','S'},{'Y','E','U','T'},{'E','O','R','N'}};
char arr_2[4][4]= {{'T','A','P','O'},{'E','N','E','R'},{'D','S','T','A'},{'I','G','H','C'}};
char arr_3[4][4]= {{'E','I','L','E'},{'Z','A','B','N'},{'S','V','O','D'},{'T','E','R','A'}};
char arr_4[4][4]= {{'H','D','E','I'},{'N','A','R','F'},{'S','O','P','U'},{'W','P','Y','L'}};
char arr_5[4][4]= {{'F','B','L','P'},{'R','I','E','A'},{'G','M','N','D'},{'H','T','S','U'}};
char arr_6[4][4]= {{'A','R','K','E'},{'L','O','T','N'},{'S','V','I','D'},{'P','E','B','A'}};
char arr_7[4][4]= {{'M','A','P','O'},{'E','T','E','R'},{'D','E','N','I'},{'L','D','H','C'}};
char arr_8[4][4]= {{'J','U','O','K'},{'A','R','K','E'},{'S','T','N','R'},{'P','E','I','T'}};
char arr_9[4][4]= {{'G','I','L','B'},{'A','D','E','R'},{'N','V','W','S'},{'B','E','I','J'}};
char arr_10[4][4]={{'E','R','T','E'},{'N','I','A','N'},{'S','P','F','V'},{'O','L','E','A'}};
srand(time(NULL)); // Initialization, should only be called once.
int number = printRandoms(1,10,1);
printf("%cn",number);
switch (number)
{
case 1:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_1[i][j]);
}
printf("|n------------------n");
}
break;
case 2:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_2[i][j]);
}
printf("|n------------------n");
}
break;
case 3:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_3[i][j]);
}
printf("|n------------------n");
}
break;
case 4:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_4[i][j]);
}
printf("|n------------------n");
}
break;
case 5:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_5[i][j]);
}
printf("|n------------------n");
}
break;
case 6:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_6[i][j]);
}
printf("|n------------------n");
}
break;
case 7:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_7[i][j]);
}
printf("|n------------------n");
}
break;
case 8:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_8[i][j]);
}
printf("|n------------------n");
}
break;
case 9:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_9[i][j]);
}
printf("|n------------------n");
}
break;
case 10:
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("| %c ", arr_10[i][j]);
}
printf("|n------------------n");
}
break;
default:
printf("invalid ");
break;
}
return 0;
}
int printRandoms(int lower, int upper, int count) {
int i;
int num;
for (i = 0; i < count; i++) {
num = (rand() %
(upper - lower + 1)) + lower;
printf("%dn ", num);
}
return num;
}
这是我粘贴的完整游戏代码。请指导我如何做到这一点,我只是在学习。
To"附上";一个字典文件,你有几个选项:
你可以添加一个带有单词数组的c文件,如下所示:
const char *words[] = {
"banana",
"apple",
"baseball"
};
然后你需要在你的主文件中声明这个符号,如下所示:
extern const char *words[];
当然,您需要同时编译主文件和字典文件才能使其正常工作。
另一种选择是把你的字典写成一个原始的单词列表,比如:
banana
apple
baseball
并以某种方式从主文件中读取文件。您可以看看getline函数,这将是一个良好的开端。
使用前一种解决方案,您将能够遍历带有索引的字典,例如在for循环中。
对于后者,您只能逐行读取,因此每次需要查找单词时都会读取整个文件。(你当然可以读一次,并将其存储在程序的开头,但你也可以使用第一个解决方案。(
这有帮助吗?