猜谜游戏检查拼写

Boggle game check spelling

我做了一个拼写游戏,现在我想在其中附加一个字典文件,这个游戏会检查拼写,return拼写是否正确。

#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("%c\n",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("%d\n ", num);
  }
  return num;
}

这是我粘贴的游戏的完整代码。并请指导我如何做,我只是在学习。

要“附加”词典文件,您有多种选择:

您可以添加一个带有单词数组的 c 文件,如下所示:

const char *words[] = {
   "banana",
   "apple",
   "baseball"
};

然后你需要在你的主文件中像这样声明这个符号:

extern const char *words[];

当然,您需要将主文件和词典文件一起编译才能使其正常工作。

另一种选择是将字典写成原始单词列表,例如:

banana
apple
baseball

并以某种方式从主文件中读取文件。你可以看看getline函数,这将是一个好的开始。

使用前一种解决方案,您将能够使用索引遍历字典,例如在 for 循环中。

对于后者,您只能逐行阅读,因此每次需要查找单词时都会阅读整个文件。 (你当然可以读一遍存到你程序的开头,不过你还不如用第一种方案。)

有帮助吗?