bool check_word(const std::vector<std::vector<char> > &grid, int width, int height, int x, int y, int direction, const char *word) { // direction is 0-7, 0 being right, and 0+ turning in a clockwise direction. static const int dirx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; static const int diry[8] = {0, 1, 1, 1, 0, -1, -1, -1}; int len = std::strlen(word); if(x < 0 || y < 0 || x >= width || y >= height || // check if starting point is outside of grid x+dirx[direction]*len < 0 || y+diry[direction]*len < 0 || // check if ending point is outside of grid x+dirx[direction]*len >= width || y+diry[direction]*len >= height) return false; // Check if the letters in the grid match the word. for(int p = 0; p < len; ++p) if(grid[x+dirx[direction]*p][y+diry[direction]*p] != word[p]) return false; // Everything is okay. return true; }
[edited by - smart_idiot on February 10, 2003 4:15:31 AM]