Search code examples
cmultidimensional-arraywordsearchboggle

Check if word exists in the matrix of random characters in C


I'm currently working in creating a word search/boggle game in C language. On which a player will input a word and the program will check if the inputted word is existing in the 2D matrix of random characters. I'm having difficulty in C language. Can someone help if I'm doing right? Or I need to change my codes. Help is much appreciated.

So here is my entire code. When I try to guess it always printing Not Found even though the word is the 2D matrix of random char. So, I need help with what is wrong? And what is the correct code to check if the word is existing?

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

#define SIZE 6 
#define MAX 10

typedef enum { true, false } bool;

// Draw the board with random characters
void board(char matrix[SIZE][SIZE]) {
    int i, j;
    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++) {
            matrix[i][j] = 'A' + rand() % ('Z' - 'A' + 1);
        }
    }
}

// Prints the board
void printBoard(char matrix[SIZE][SIZE]) {
    int i, j;
    srand((unsigned int)time(NULL));
    board(matrix);

    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++) {
            printf(" %c ", matrix[i][j]);
        }
        printf("\n");       
    }
}

bool adjacentSearch(char matrix[SIZE][SIZE], char *find, int i, int j, int index) {
    if (index == strlen(find))
        return true;

    if (i < 0 || j < 0 || i > SIZE - 1 || j > strlen(matrix[0]) - 1) {
        return true;
    }

    if (matrix[i][j] != find[index]) {
        return false;
    }

    matrix[i][j] = '*';

    bool searchFurther = adjacentSearch(matrix, find, i + 1, j, index + 1) ||
                         adjacentSearch(matrix, find, i - 1, j, index + 1) ||
                         adjacentSearch(matrix, find, i, j - 1, index + 1) ||
                         adjacentSearch(matrix, find, i, j + 1, index + 1);

    matrix[i][j] = find[index];
    return searchFurther;
}

bool exist(char matrix[SIZE][SIZE], char *find, int r, int c) {
    int len = strlen(find);

    if (len > r * c)
        return false;

    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < strlen(matrix[0]); j++) {
            if (matrix[i][j] == find[0]) {
                if (adjacentSearch(matrix, find, i, j, 0)) {
                    return true;
                }
            }
        }
    }
    return false;
}

// Driver 
int main() {
    char matrix[SIZE][SIZE];
    char word[MAX];

    printBoard(matrix);

    printf("\nThink of a word> ");
    fgets(word, MAX, stdin);
    //printf("word: %s", word);

    if (exist(matrix, word, SIZE, SIZE)) {
        printf("Found\n");
    } else {
        printf("Not Found\n");
    }
    return 0;
}

Solution

  • There are multiple problems in the code:

    • the definition of true and false is incorrect: you should write:

      typedef enum { false, true } bool;
      
    • you do not strip the trailing newline left in the buffer by fgets(). The program cannot find the word because the newline is not present in the matrix. Remove this character with:

      word[strcspn(word, "\n")] = '\0';
      
    • the tests j > strlen(matrix[0]) - 1 are incorrect: you should just test if j >= SIZE to verify that the coordinates are inside the matrix.

    • adjacentSearch() should return false when i or j are outside the matrix.

    Here is a corrected version:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define SIZE 6
    #define MAX 40
    
    typedef enum { false, true } bool;
    
    // Draw the board with random characters
    void board(char matrix[SIZE][SIZE]) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                matrix[i][j] = 'A' + rand() % ('Z' - 'A' + 1);
            }
        }
    }
    
    // Prints the board
    void printBoard(char matrix[SIZE][SIZE]) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                printf(" %c ", matrix[i][j]);
            }
            printf("\n");
        }
    }
    
    bool adjacentSearch(char matrix[SIZE][SIZE], const char *find, int i, int j, int index) {
        if (find[index] == '\0')
            return true;
    
        if (i < 0 || j < 0 || i >= SIZE || j >= SIZE || matrix[i][j] != find[index])
            return false;
    
        matrix[i][j] = '*';
        bool found = (adjacentSearch(matrix, find, i + 1, j, index + 1) ||
                      adjacentSearch(matrix, find, i - 1, j, index + 1) ||
                      adjacentSearch(matrix, find, i, j - 1, index + 1) ||
                      adjacentSearch(matrix, find, i, j + 1, index + 1));
        matrix[i][j] = find[index];
        return found;
    }
    
    bool exist(char matrix[SIZE][SIZE], const char *find) {
        int len = strlen(find);
        if (len > SIZE * SIZE)
            return false;
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (adjacentSearch(matrix, find, i, j, 0)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    // Driver
    int main() {
        char matrix[SIZE][SIZE], word[MAX];
    
        srand((unsigned int)time(NULL));
        board(matrix);
        printBoard(matrix);
    
        for (;;) {
            printf("\nThink of a word> ");
            if (!fgets(word, sizeof word, stdin))
                break;
            word[strcspn(word, "\n")] = '\0';
            if (exist(matrix, word)) {
                printf("Found\n");
            } else {
                printf("Not Found\n");
            }
        }
        return 0;
    }