Search code examples
c++arraysgrid2dwordsearch

C++ Wordsearch Puzzle Grid 2D Array


I'm having an issue trying to read in a textfile (below) that contains letters for a wordsearch. I want to read the textfile in as an array then be able to match the words from my dictionary.txt to the wordsearch_grid.txt. Any ideas?

wordsearch_grid:
9
E M M A R G O R P 
C L U A U N L L D 
O T A O F I L O I 
M E U N J G E O K 
P W H K G G H P Q 
I C O M P U T E R 
L L V R Z B A O X 
E H O M L E Q G U 
T N I R P D C O E

dictionary:
COMPILE
COMPUTER
DEBUGGING
HELLO
KITCHEN
GRAPHICS
LOOP
SPAN
PROGRAMME
WORLD

The code I have so far is as follows:

#include "WordSearch.h"
#include "fstream"
#include <iostream>
#include <string>
#include "vector"


using namespace std;

vector<string> list;
vector<string> grid;
string line;
string n;

WordSearch::WordSearch(const char * const filename) 
{


}

WordSearch::~WordSearch() 
{

}

void WordSearch::ReadSimplePuzzle() {

    ifstream inFile;
    inFile.open("wordsearch_grid.txt");

    if (inFile.fail())
    {
        cerr << "Error Wordsearch Grid File" << endl;
        exit(1);
    }
    else
    {

        while (getline (inFile, n))
        {
            cout << n << endl;          
        }
        //grid[4][3];
        inFile.close();
        //cout << grid << endl;
        cout << "\n" << endl;
    }

}

void WordSearch::ReadSimpleDictionary() 
{
    ifstream inFile;
    inFile.open("dictionary.txt");


    if (inFile.fail())
    {
        cerr << "Error Dictionary File" << endl;
        exit(1);

    }
    else
    {
        int count = 0;
        while (getline(inFile, line))
        {
            list.push_back(line);
            cout << line << endl;
        }
        inFile.close();
    }


}

void WordSearch::SolvePuzzleSimple() 

{


}

So far it can read in the files and display them but I want to be able to manipulate the grid so that it I can match the first and last letter of say "COMPILE" to match up with 2 letters in the grid and output to output.txt "COMPILE was found at [1][2]


Solution

  • Here is the inline logic which you can encapsulate in your class how you choose :

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <ctime>
    using namespace std;
    
    ifstream inFile("wordsearch_grid.txt");
    ifstream dict("dictionary.txt");
    ofstream out("output.txt");
    
    int main(){
        string word;
        char c;
        char grid[9][9] = {};
        int row = 0;
        int column = 0;
        vector<string> wordsFound;
        clock_t start;
        double duration;
        vector<string> words;
        vector<vector<int> > locations;
        //store words from dictionary into vector
        while (getline(dict, word))
        { 
            words.push_back(word);     
        }
        start = clock();
        //store grid in a c-array
        while (inFile.get(c))
        {
            if (c != ' ' && c != '\n')
            {
                grid[row][column] = c;
                if (column == 8)
                {
                    column = 0;
                    row++;
                }else
                {
                    column++;
                }
            }
        }
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                cout << grid[i][j] << " ";
            }
            cout << endl;
        }
        duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
        cout << "Time it took to populate grid (seconds) : " << duration << endl;
        start = clock();
        //for each character in grid
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                //cout << grid[i][j] << " ";
                //for each word
                for (int k = 0; k < words.size(); k++)
                {
                    //check if grid letter equals the first letter of word
                    if (grid[i][j] == words[k][0])
                    {
                        //check horizontal vertical and diagonal
                        for (int l = 1; l <= words[k].size(); l++)
                        {
                            if (
                                //break if no word was found
                                grid[i-l][j] != words[k][l] && 
                                grid[i+l][j] != words[k][l] && 
                                grid[i][j+l] != words[k][l] && 
                                grid[i][j-l] != words[k][l] &&
                                grid[i+l][j+l] != words[k][l] &&
                                grid[i-l][j-l] != words[k][l] && 
                                grid[i+l][j-l] != words[k][l] &&
                                grid[i-l][j+l] != words[k][l] )
                            {
                                break;
                            }
                            else if (l == words[k].size()-1)
                            {
                                //else write word found to file
                                //out << words[k] << " was found at [" <<
                                //j+1 << "][" << i+1 << "]" << endl;
                                //add word location to locations
                                vector<int> location;
                                location.push_back(j+1);
                                location.push_back(i+1);
                                locations.push_back(location);
                                //add word to wordsFound
                                wordsFound.push_back(words[k]);
                            }
                        }
                    }
                }
            }
            //cout << endl;
        }
        duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
        cout << "Time it took to finish wordsearch puzzle (seconds) : " << duration << endl;
    
        out << "number of words found: " << wordsFound.size() << endl;
    
        for (int i = 0; i < wordsFound.size(); i++){
            out << wordsFound[i] << " was found at [" << locations[i][0] << "][" << locations[i][1] << "]" << endl;
        }
        out << "number of words not found: " << words.size() - wordsFound.size() << endl;
    
        for (int i = 0; i < words.size(); i++) {
            for (int j = 0; j < wordsFound.size(); j++) {
                //loop to check if word in dictionary wasn't found and append to output.txt
                if (words[i] == wordsFound[j]){
                    break;
                }
                else if (j == wordsFound.size()-1){
                    out << words[i] << " was not found!" << endl;
                }
            }
        }
        return 0;
    }