#include #include #include "WordMatrix.h" // default constructor - will always create the same matrix // (usefull for testing) WordMatrix::WordMatrix() { matrix[0] = "LLABTEKSAB"; matrix[1] = "AUGNILCYCW"; matrix[2] = "CHGSOCCERH"; matrix[3] = "SRDEYRBCBA"; matrix[4] = "YNIBOOOCSN"; matrix[5] = "BEGCWQLOID"; matrix[6] = "FUKLKUOBNB"; matrix[7] = "RLICIEPFNA"; matrix[8] = "UNOGOTTMEL"; matrix[9] = "GATGRHPETL"; toSearchFor[0] = "BASKETBALL"; toSearchFor[1] = "LUGE"; toSearchFor[2] = "SOCCER"; toSearchFor[3] = "HANDBALL"; toSearchFor[4] = "CROQUET"; toSearchFor[5] = "BOCCE"; toSearchFor[6] = "HOCKEY"; toSearchFor[7] = "RUGBY"; toSearchFor[8] = "BOWLING"; toSearchFor[9] = "CYCLING"; toSearchFor[10] = "CRICKET"; toSearchFor[11] = "POLO"; toSearchFor[12] = "TENNIS"; toSearchFor[13] = "GOLF"; numWords = 14; size = 10; } // constructor - hides a given set of words // not currently implemented WordMatrix::WordMatrix(int size, string toHide[], int numToHide) { cerr << "we're sorry - we cannot create this word matrix at this time" << endl; } // // inspectors // return number of words in the word matrix int WordMatrix::GetNumWords(){ return numWords; } // return string that is in the given row // returns blank string if given bad row number string WordMatrix::GetNthRow(int rowNum) { // for humans - rows are 1->size rather than 0->size-1 string res = ""; // catch bad row number if ((rowNum < 1) || (rowNum > size)) { cerr << "Bad row number " << endl; } else { res = matrix[rowNum-1]; // if user asks for row 1 give them row 0 } return res; } // return string that is in the given row // returns blank string if given bad row number string WordMatrix::GetNthCol(int colNum) { // for humans - columns are 1->size rather than 0->size-1 string res = ""; // catch bad column number if ((colNum < 1) || (colNum > size)) { cerr << "Bad column number " << endl; } else { for (int cnt = 0; cnt < size; cnt++ ) { res += matrix[cnt][colNum-1]; // add char from approp column from each row of matrix } } return res; } // return string that starts in the given row,col and goes down and to the right // returns blank string if given bad starting position // should really get either a row or column of 1 (or both) but not enforcing that string WordMatrix::GetDiagRight(int startRow, int startCol) { // for humans - rows and columns are 1->size rather than 0->size-1 string res = ""; // catch bad row or column number if ((startRow < 1) || (startRow > size) || (startCol < 1) || (startCol > size)) { cerr << "Bad row or column number " << endl; } else { int col = startCol; // move down the matrix pulling chars one at a time and concatenating them to result string for (int row = startRow; (row <= size) && (col <= size) ; row++ ) { res += matrix[row-1][col-1]; // add char from approp column from each row of matrix col++; } } return res; } // return string that starts in the given row,col and goes down and to the left // returns blank string if given bad starting position // should really get either a row of 1 or a column of highest column (or both) but not enforcing that string WordMatrix::GetDiagLeft(int startRow, int startCol) { // for humans - rows and columns are 1->size rather than 0->size-1 string res = ""; // catch bad row or column number if ((startRow < 1) || (startRow > size) || (startCol < 1) || (startCol > size)) { cerr << "Bad row or column number " << endl; } else { int col = startCol; // move down the matrix pulling chars one at a time and concatenating them to result string for (int row = startRow; (row <= size) && (col > 0) ; row++ ) { res += matrix[row-1][col-1]; // add char from approp column from each row of matrix col--; } } return res; } // return word in search list - given position string WordMatrix::GetNthSearchWord(int wordNum) { // for humans - words are 1->numWords rather than 0->numWords-1 string res = ""; // catch bad word number if ((wordNum < 1) || (wordNum > numWords)) { cerr << "Bad search word number " << endl; } else { res = toSearchFor[wordNum-1]; // if user asks for word 1 give them word 0 } return res; } int WordMatrix::GetSize(){ // return size of the Word Matrix return size; } // allows outputting all info about word matrix ostream& operator << (ostream & strOut, WordMatrix & myMatrix) { string currRow; int matSize = myMatrix.GetSize(); strOut << "Word Matrix: " << endl; // get each row of the matrix in turn and display it char by char for (int cnt = 0; cnt < matSize; cnt++) { currRow = myMatrix.GetNthRow(cnt+1); // class expects rows starting with 1 for (int n = 0; n < matSize; n++) { strOut << currRow[n] << " "; } // go to next line for next row strOut << endl; } strOut << "Search Words: " << endl; for (int k = 0; k < myMatrix.GetNumWords(); k++) { strOut << myMatrix.GetNthSearchWord(k+1) << " "; // class expects word numbers starting with 1 } strOut << endl; return strOut; }