-1

我已经制作了一个拼写游戏,现在我想在其中附加一个字典文件,这个游戏会检查拼写并返回拼写是否正确。

#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;
}

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

4

1 回答 1

0

要“附加”字典文件,您有几个选项:

您可以使用这样的单词数组添加 ac 文件:

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

然后你需要在你的主文件中声明这个符号,如下所示:

extern const char *words[];

当然,您需要同时编译主文件和字典文件才能使其正常工作。

另一种选择是将您的字典编写为原始单词列表,如下所示:

banana
apple
baseball

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

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

使用后者,您只能逐行读取,因此每次需要查找单词时都会读取整个文件。(您当然可以读取一次并将其存储在程序的开头,但您也可以使用第一个解决方案。)

它有帮助吗?

于 2021-12-20T09:02:38.680 回答