Advertisement
Guest User

Wordsearch Project1

a guest
Sep 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. /*
  2.  * assignment1.cpp
  3.  *
  4.  * <add comments to describe the purpose of this application>
  5.  *
  6.  */
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include <fstream>
  11. #include <cstring>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. enum direction
  19. {
  20.     LEFT_DOWN,
  21.     LEFT,
  22.     LEFT_UP,
  23.     DOWN,
  24.     UP,
  25.     RIGHT_DOWN,
  26.     RIGHT,
  27.     RIGHT_UP
  28. };
  29.  
  30. const int MAX = 50;
  31.  
  32. struct wordGame
  33. {
  34.     int version;
  35.     int numberRows;
  36.     int numberColumns;
  37.     char puzzle[MAX][MAX];
  38. };
  39.  
  40. struct wordFind
  41. {
  42.     string word;
  43.     bool found;
  44.     int row;
  45.     int column;
  46.     direction where;
  47.     int foundCount;
  48. };
  49.  
  50. // function prototypes go here
  51. void findWord(wordGame& game, wordFind& theFind);
  52.  
  53. bool readPuzzle(wordGame& game, string inputFileName);
  54.  
  55. void displayPuzzle(wordGame& game);
  56.  
  57. wordGame gameInfo;
  58. wordFind wordInfo;
  59. string puzzleFile;
  60. string wordsFile;
  61. ifstream words;
  62.  
  63. int main()
  64. {
  65.     cin >> puzzleFile;
  66.    
  67.     if (!readPuzzle(gameInfo, puzzleFile)) {
  68.         //cout an error message here
  69.         return 0;
  70.     }
  71.     else {
  72.         displayPuzzle(gameInfo);
  73.     }
  74.  
  75.     cin >> wordsFile;
  76.     words.open(wordsFile);
  77.     if (!words.is_open())
  78.     {
  79.         cout << "The puzzle file \"" << wordsFile << "\" could not be opened or is invalid";
  80.     }
  81.     findWord(gameInfo, wordInfo);
  82.     words.close();
  83.     return 0;
  84. }
  85. bool readPuzzle(wordGame& game, string inputFileName)
  86. {
  87.     ifstream inFile;
  88.     inFile.open(inputFileName);
  89.     if (inFile.is_open())
  90.     {
  91.         inFile >> game.numberRows;
  92.         inFile >> game.numberColumns;
  93.         if ((game.numberColumns < 1 || game.numberColumns > 50))
  94.         {
  95.             cout << "Your column size is out of range. Please keep it between 1 and 50.";
  96.             return false;
  97.         }
  98.         else if ((game.numberRows < 1 || game.numberRows > 50))
  99.         {
  100.             cout << "Your row size is out of range. Please keep it between 1 and 50.";
  101.             return false;
  102.         }
  103.         else
  104.         {
  105.             game.version = 2; //BONUS POINTS \o/
  106.  
  107.             for (int i = 0; i < game.numberRows; i++)
  108.             {
  109.                 for (int k = 0; k < game.numberColumns; k++)
  110.                 {
  111.                     char tempChar;
  112.                     inFile >> tempChar; //stores the values of input into the 2D array
  113.                     game.puzzle[i][k] = toupper(tempChar); //converts all characters being put into the array to uppercase
  114.                 }
  115.             }
  116.         }
  117.         return true;
  118.     }
  119.     else
  120.     {
  121.         cout << "The puzzle file \"" << inputFileName << "\" could not be opened or is invalid";
  122.     }
  123.     inFile.close();
  124.     return false;
  125. }
  126. void findWord(wordGame& game, wordFind& theFind)
  127. {
  128.     vector <string> wordsList;
  129.     while (!words.eof())
  130.     {
  131.         string temp;
  132.         words >> temp;
  133.         wordsList.push_back(temp);
  134.     }
  135.     int x[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
  136.     int y[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
  137.     //LEFT_DOWN, LEFT, LEFT_UP, DOWN, UP, RIGHT_DOWN, RIGHT, RIGHT_UP respectively
  138.     string dir[8] = { "left/down", "left", "left/up", "down", "up", "right/down", "right", "right/up"};
  139.     for(int w = 0; w < wordsList.size();w++)
  140.     {
  141.         theFind.word = wordsList[w];
  142.         for (int k = 0; k < game.numberRows; k++)
  143.         {
  144.             for (int b = 0; b < game.numberColumns; b++)
  145.             {
  146.                 if (game.puzzle[k][b] == theFind.word[0]) //checks if first letter matches with current letter
  147.                 {
  148.                     for (int i = 0; i < 8; i++) //8 directions to get through
  149.                     {
  150.                         int u, rd = k + x[i], cd = b + y[i]; // Initialize starting point for current direction
  151.                         for (u = 1; u < theFind.word.length(); u++) // First character has already been checked; need to match remaining characters
  152.                         {
  153.                             if (rd >= game.numberRows-1 || rd < 0 || cd >= game.numberColumns-1 || cd < 0) // If it reaches out of bounds, break
  154.                                 break;
  155.                            
  156.                             if (game.puzzle[rd][cd] != theFind.word[u]) // If characters do not match, break
  157.                                 break;
  158.  
  159.                             rd += x[i], cd += y[i]; // Continues moving in that direction
  160.                         }
  161.                         if (u == theFind.word.length()) //assign all acquired values
  162.                         {
  163.                             theFind.column = b;
  164.                             theFind.row = k;
  165.                             theFind.where = static_cast<direction>(i); //converts i to its corresponding direction
  166.                             theFind.found = true;
  167.                             theFind.word = theFind.word;
  168.                             theFind.foundCount++;
  169.                         }
  170.                     }
  171.                 }
  172.             }
  173.         }
  174.         if (theFind.found)
  175.         {
  176.             if (theFind.foundCount > 1)
  177.             {
  178.                 cout << "The word " << theFind.word << " was found " << theFind.foundCount << " times." << endl;
  179.             }
  180.             else
  181.             {
  182.                 cout << "The word " << theFind.word << " was found at (" << theFind.row + 1 << ", " << theFind.column + 1 << ") - " << dir[static_cast<int>(theFind.where)] << endl;
  183.             }
  184.         }
  185.         else
  186.         {
  187.             cout << "The word " << theFind.word << " was not found" << endl;
  188.         }
  189.         theFind.foundCount = 0;
  190.         theFind.found = false;
  191.     }
  192. }
  193. void displayPuzzle(wordGame& game)
  194. {
  195.     cout << "The puzzle from file \"" << puzzleFile << "\"" << endl;
  196.     for (int i = 0; i < game.numberRows; i++)
  197.     {
  198.         for (int k = 0; k < game.numberColumns; k++)
  199.         {
  200.             cout << game.puzzle[i][k]; //prints values stored inside array
  201.         }
  202.         cout << endl; // moves to next line as soon as a single row has completed in order to properly arrange
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement