Home > front end >  Boggle Game in C language
Boggle Game in C language

Time:12-20

I have made a boggle game now I want to make or simply want to attach a dictionary file to it where this game check spelling and return whether the spelling is correct or wrong.

Kindly guide me about that.

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

This is the complete code of game I pasted. And kindly guide me how to do that I am simply learning.

CodePudding user response:

To "attach" a dictionnary file, you have several options: You can add a c file with a words array like this:

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

Then you need to declare this symbol in your main file like this:

extern const char *words[];

Of course you need to compile both your main file and your dictionary file together to get it working.

Another option is to write your dictionary as a raw list of words, like that:

banana
apple
baseball

And to read the file in some way from your main file. You can take a look at getline function, it would be a good start.

With the former solution, you will be able to iterate through the dictionary with indexes, e.g. in a for loop.

With the latter, You'll only be able to read it line by line, thus reading the whole file each time you need to find a word. (You can of course read it once and store it at the beginning of your program, but you might as well use the first solution.)

Does it helps?

  • Related