Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * assignment1.cpp
- *
- * <add comments to describe the purpose of this application>
- *
- */
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstring>
- #include <algorithm>
- #include <cctype>
- #include <vector>
- using namespace std;
- enum direction
- {
- LEFT_DOWN,
- LEFT,
- LEFT_UP,
- DOWN,
- UP,
- RIGHT_DOWN,
- RIGHT,
- RIGHT_UP
- };
- const int MAX = 50;
- struct wordGame
- {
- int version;
- int numberRows;
- int numberColumns;
- char puzzle[MAX][MAX];
- };
- struct wordFind
- {
- string word;
- bool found;
- int row;
- int column;
- direction where;
- int foundCount;
- };
- // function prototypes go here
- void findWord(wordGame& game, wordFind& theFind);
- bool readPuzzle(wordGame& game, string inputFileName);
- void displayPuzzle(wordGame& game);
- wordGame gameInfo;
- wordFind wordInfo;
- string puzzleFile;
- string wordsFile;
- ifstream words;
- int main()
- {
- cin >> puzzleFile;
- if (!readPuzzle(gameInfo, puzzleFile)) {
- //cout an error message here
- return 0;
- }
- else {
- displayPuzzle(gameInfo);
- }
- cin >> wordsFile;
- words.open(wordsFile);
- if (!words.is_open())
- {
- cout << "The puzzle file \"" << wordsFile << "\" could not be opened or is invalid";
- }
- findWord(gameInfo, wordInfo);
- words.close();
- return 0;
- }
- bool readPuzzle(wordGame& game, string inputFileName)
- {
- ifstream inFile;
- inFile.open(inputFileName);
- if (inFile.is_open())
- {
- inFile >> game.numberRows;
- inFile >> game.numberColumns;
- if ((game.numberColumns < 1 || game.numberColumns > 50))
- {
- cout << "Your column size is out of range. Please keep it between 1 and 50.";
- return false;
- }
- else if ((game.numberRows < 1 || game.numberRows > 50))
- {
- cout << "Your row size is out of range. Please keep it between 1 and 50.";
- return false;
- }
- else
- {
- game.version = 2; //BONUS POINTS \o/
- for (int i = 0; i < game.numberRows; i++)
- {
- for (int k = 0; k < game.numberColumns; k++)
- {
- char tempChar;
- inFile >> tempChar; //stores the values of input into the 2D array
- game.puzzle[i][k] = toupper(tempChar); //converts all characters being put into the array to uppercase
- }
- }
- }
- return true;
- }
- else
- {
- cout << "The puzzle file \"" << inputFileName << "\" could not be opened or is invalid";
- }
- inFile.close();
- return false;
- }
- void findWord(wordGame& game, wordFind& theFind)
- {
- vector <string> wordsList;
- while (!words.eof())
- {
- string temp;
- words >> temp;
- wordsList.push_back(temp);
- }
- int x[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
- int y[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
- //LEFT_DOWN, LEFT, LEFT_UP, DOWN, UP, RIGHT_DOWN, RIGHT, RIGHT_UP respectively
- string dir[8] = { "left/down", "left", "left/up", "down", "up", "right/down", "right", "right/up"};
- for(int w = 0; w < wordsList.size();w++)
- {
- theFind.word = wordsList[w];
- for (int k = 0; k < game.numberRows; k++)
- {
- for (int b = 0; b < game.numberColumns; b++)
- {
- if (game.puzzle[k][b] == theFind.word[0]) //checks if first letter matches with current letter
- {
- for (int i = 0; i < 8; i++) //8 directions to get through
- {
- int u, rd = k + x[i], cd = b + y[i]; // Initialize starting point for current direction
- for (u = 1; u < theFind.word.length(); u++) // First character has already been checked; need to match remaining characters
- {
- if (rd >= game.numberRows-1 || rd < 0 || cd >= game.numberColumns-1 || cd < 0) // If it reaches out of bounds, break
- break;
- if (game.puzzle[rd][cd] != theFind.word[u]) // If characters do not match, break
- break;
- rd += x[i], cd += y[i]; // Continues moving in that direction
- }
- if (u == theFind.word.length()) //assign all acquired values
- {
- theFind.column = b;
- theFind.row = k;
- theFind.where = static_cast<direction>(i); //converts i to its corresponding direction
- theFind.found = true;
- theFind.word = theFind.word;
- theFind.foundCount++;
- }
- }
- }
- }
- }
- if (theFind.found)
- {
- if (theFind.foundCount > 1)
- {
- cout << "The word " << theFind.word << " was found " << theFind.foundCount << " times." << endl;
- }
- else
- {
- cout << "The word " << theFind.word << " was found at (" << theFind.row + 1 << ", " << theFind.column + 1 << ") - " << dir[static_cast<int>(theFind.where)] << endl;
- }
- }
- else
- {
- cout << "The word " << theFind.word << " was not found" << endl;
- }
- theFind.foundCount = 0;
- theFind.found = false;
- }
- }
- void displayPuzzle(wordGame& game)
- {
- cout << "The puzzle from file \"" << puzzleFile << "\"" << endl;
- for (int i = 0; i < game.numberRows; i++)
- {
- for (int k = 0; k < game.numberColumns; k++)
- {
- cout << game.puzzle[i][k]; //prints values stored inside array
- }
- cout << endl; // moves to next line as soon as a single row has completed in order to properly arrange
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement