Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background Let\'s play \"Find a word puzzle.\" In the puzzle, there are uppercas

ID: 3717937 • Letter: B

Question

Background Let's play "Find a word puzzle." In the puzzle, there are uppercase letters stored in a square. The square is divided into rows and columns and each letter is shown in a cell of the square. The player has a list of words to find in the puzzle. Words can be found in 8 directions: horizontally from left to right, honizontally from night to left, vertically from topto bottom, vertically from bottom to top, diagonally from upper left to lower right, diagonally from lower right to upper left, diagonally from upper right to lower left, and diagonally from lower left to upper right. Normally the player will mark the word found in puzzle using a highlight marker once a word is found. You are asked to write program to assist the player in finding words in a puzzle. For simplicity, your program will find words horizontally and vertically only, not diagonally Class Specifications The declaration below shows a Puzzle class which will be used for this assignment const int MAX ROWS = 25; const int MAX COLUMNS 25; class Puzzle private: char storage [MAX ROWS1 IMAX COLUMNS int sizei puzzle storage / number of characters per row or colunn public Puzzle(int new size class Puzzle Listed below are the specifications of the Puzzle functions that will be implemented for the assignment. Puzzle: :Puzzlefint new size // precondition: new size is in bound ibetkeen 8 and 18) /I postcondition: set size to new size and every element in the Puzzle to a space character

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

const int MAX_ROWS = 25;
const int MAX_COLUMNS = 25;

class Puzzle{
   private:
       char storage[MAX_ROWS][MAX_COLUMNS];
       int size;
  
   public:
       Puzzle(int new_size)
       {
           if(new_size >=8 && new_size <= 18)
           {
               size = new_size;
              
               for(int i=0;i<size;i++)
               {
                   for(int j=0;j<size;j++)
                       storage[i][j] = ' ';
               }
           }
       }
      
       void setElement(int i,int j,char c)
       {
           storage[i][j] = c;
       }
      
       bool leftToRightFound(int i,int j,char *word)
       {
           int c = 0;
           int f = 0;
           while(word[c] && j<size)
           {
               if(storage[i][j] != word[c])
               {
                   return false;
               }
               c++;
               j++;
           }
           if(word[c]=='')
               return true;
           return false;
       }
      
       bool RightToLeftFound(int i,int j,char *word)
       {
           int c = 0;
           int f = 0;
           while(word[c] && j>=0)
           {
               if(storage[i][j] != word[c])
               {
                   return false;
               }
               c++;
               j--;
           }
           if(word[c]=='')
               return true;
           return false;
       }
      
       bool topToBottomFound(int i,int j,char *word)
       {
           int c = 0;
           int f = 0;
           while(word[c] && i<size)
           {
               if(storage[i][j] != word[c])
               {
                   return false;
               }
               c++;
               i++;
           }
           if(word[c]=='')
               return true;
           return false;
       }
      
       bool bottomToTopFound(int i,int j,char *word)
       {
           int c = 0;
           int f = 0;
           while(word[c] && i>=0)
           {
               if(storage[i][j] != word[c])
               {
                   return false;
               }
               c++;
               i--;
           }
           if(word[c]=='')
               return true;
           return false;
       }
      
       char getElement(int i,int j)
       {
           return storage[i][j];
       }
      
       int getRows()
       {
           return size;
       }
      
       int getColumns()
       {
           return size;
       }
  
};

int main()
{
   ifstream myfile;
   string filename;
  
   cout<<"Please enter the name of data file : ";
   cin>>filename;
  
   myfile.open(filename.c_str());
   int x;
   char c;
   myfile>>x;
  
   Puzzle myPuzzle(x);
   for(int i=0;i<x;i++)
   {
       for(int j=0;j<x;j++)
       {
           myfile>>c;
           myPuzzle.setElement(i,j,c);
       }
   }
   myfile.close();
  
   char word[30];
   while(1)
   {
       char inp;
       cout<<"Please select one of the options mentioned below : ";
       cout<<" a) Enter word to be searched in puzzle ";
       cout<<" b) Exit ";
       cin>>inp;
      
       if(inp == 'b')
       {
           break;
       }
       cin>>word;
      
       cout<<word<<endl;
      
       int f = 0;
       for(int i=0;i<x;i++)
       {
           for(int j=0;j<x;j++)
           {
               if(myPuzzle.leftToRightFound(i,j,word) || myPuzzle.RightToLeftFound(i,j,word) || myPuzzle.topToBottomFound(i,j,word) || myPuzzle.bottomToTopFound(i,j,word) )
               {
                    cout<<"Word found at location "<<i<<"' '"<<j<<endl;
                   f = 1;
                   break;
               }  
           }
           if(f==1)
               break;
       }
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote