Word Search Using 2d arrays in java - getting past the second letter -
so, have been tasked creating word search program using 2d array grid.
so far code can find 2 letter words in grid, go beyond 2 letters in length , code skips them. i'm dead tired @ 2am obvious im missing, if isnt, me out?
current searching code:
/** * searches word in lettergrid using traditional wordsearch * rules. * * @param word * word */ public boolean wordsearch(string word) { // check each letter in grid (int row = 0; row < this.noofrows; row++) { (int col = 0; col < this.rowlength; col++) { if (grid[row][col] == word.charat(0) && word.length() > 1) { return gridcheck(row, col, word, 1); } else if (grid[row][col] == word.charat(0)) { return true; } } } return false; } public boolean gridcheck(int row, int col, string word, int chartofind) { if (chartofind == word.length() - 1) { return true; } else if (chartofind < word.length() - 1) { // letter being checked? -contingency check- // if letter being checked not touching edge [most likely] if (row > 0 && row < this.noofrows && col > 0 && col < this.rowlength) { // codes see checkplaces.txt // if (grid[row - 1][col - 1] == word.charat(chartofind)) { gridcheck(row - 1, col - 1, word, word.charat(chartofind + 1)); } // b else if (grid[row - 1][col] == word.charat(chartofind)) { gridcheck(row - 1, col, word, word.charat(chartofind + 1)); } // c else if (grid[row - 1][col + 1] == word.charat(chartofind)) { gridcheck(row - 1, col + 1, word, word.charat(chartofind + 1)); } // d else if (grid[row][col - 1] == word.charat(chartofind)) { gridcheck(row, col - 1, word, word.charat(chartofind + 1)); } // e else if (grid[row][col + 1] == word.charat(chartofind)) { gridcheck(row, col + 1, word, word.charat(chartofind + 1)); } // f else if (grid[row + 1][col - 1] == word.charat(chartofind)) { gridcheck(row + 1, col - 1, word, word.charat(chartofind + 1)); } // g else if (grid[row + 1][col] == word.charat(chartofind)) { gridcheck(row + 1, col, word, word.charat(chartofind + 1)); } // h else if (grid[row + 1][col + 1] == word.charat(chartofind)) { gridcheck(row + 1, col + 1, word, word.charat(chartofind + 1)); } } // letter touching top left corner if (row == 0 && col == 0) { // e if (grid[row][col + 1] == word.charat(chartofind)) { gridcheck(row, col + 1, word, word.charat(chartofind + 1)); } // h else if (grid[row + 1][col + 1] == word.charat(chartofind)) { gridcheck(row + 1, col + 1, word, word.charat(chartofind + 1)); } // g else if (grid[row + 1][col] == word.charat(chartofind)) { gridcheck(row + 1, col, word, word.charat(chartofind + 1)); } } // letter touching top right corner if (row == 0 && col == this.rowlength) { // d if (grid[row][col - 1] == word.charat(chartofind)) { gridcheck(row, col - 1, word, word.charat(chartofind + 1)); } // f else if (grid[row + 1][col - 1] == word.charat(chartofind)) { gridcheck(row + 1, col - 1, word, word.charat(chartofind + 1)); } // g else if (grid[row + 1][col] == word.charat(chartofind)) { gridcheck(row + 1, col, word, word.charat(chartofind + 1)); } } // letter touching bottom left corner if (row == this.noofrows && col == 0) { // b if (grid[row - 1][col] == word.charat(chartofind)) { gridcheck(row - 1, col, word, word.charat(chartofind + 1)); } // c else if (grid[row - 1][col + 1] == word.charat(chartofind)) { gridcheck(row - 1, col + 1, word, word.charat(chartofind + 1)); } // e else if (grid[row][col + 1] == word.charat(chartofind)) { gridcheck(row, col + 1, word, word.charat(chartofind + 1)); } } // letter touching bottom right corner if (row == this.noofrows && col == this.rowlength) { // if (grid[row - 1][col - 1] == word.charat(chartofind)) { gridcheck(row - 1, col - 1, word, word.charat(chartofind + 1)); } // b else if (grid[row - 1][col] == word.charat(chartofind)) { gridcheck(row - 1, col, word, word.charat(chartofind + 1)); } // d if (grid[row][col - 1] == word.charat(chartofind)) { gridcheck(row, col - 1, word, word.charat(chartofind + 1)); } } // letter on top row of grid if (row == 0 && col > 0 && col < this.rowlength) { // d if (grid[row][col - 1] == word.charat(chartofind)) { gridcheck(row, col - 1, word, word.charat(chartofind + 1)); } // e else if (grid[row][col + 1] == word.charat(chartofind)) { gridcheck(row, col + 1, word, word.charat(chartofind + 1)); } // f else if (grid[row + 1][col - 1] == word.charat(chartofind)) { gridcheck(row + 1, col - 1, word, word.charat(chartofind + 1)); } // g else if (grid[row + 1][col] == word.charat(chartofind)) { gridcheck(row + 1, col, word, word.charat(chartofind + 1)); } // h else if (grid[row + 1][col + 1] == word.charat(chartofind)) { gridcheck(row + 1, col + 1, word, word.charat(chartofind + 1)); } } // letter on bottom row of grid if (row == this.noofrows && col > 0 && col < this.rowlength) { // codes see checkplaces.txt // if (grid[row - 1][col - 1] == word.charat(chartofind)) { gridcheck(row - 1, col - 1, word, word.charat(chartofind + 1)); } // b else if (grid[row - 1][col] == word.charat(chartofind)) { gridcheck(row - 1, col, word, word.charat(chartofind + 1)); } // c else if (grid[row - 1][col + 1] == word.charat(chartofind)) { gridcheck(row - 1, col + 1, word, word.charat(chartofind + 1)); } // d else if (grid[row][col - 1] == word.charat(chartofind)) { gridcheck(row, col - 1, word, word.charat(chartofind + 1)); } // e else if (grid[row][col + 1] == word.charat(chartofind)) { gridcheck(row, col + 1, word, word.charat(chartofind + 1)); } } // letter on leftmost column of grid if (col == 0 && row > 0 && row < this.noofrows) { // b if (grid[row - 1][col] == word.charat(chartofind)) { gridcheck(row - 1, col, word, word.charat(chartofind + 1)); } // c else if (grid[row - 1][col + 1] == word.charat(chartofind)) { gridcheck(row - 1, col + 1, word, word.charat(chartofind + 1)); } // e else if (grid[row][col + 1] == word.charat(chartofind)) { gridcheck(row, col + 1, word, word.charat(chartofind + 1)); } // g else if (grid[row + 1][col] == word.charat(chartofind)) { gridcheck(row + 1, col, word, word.charat(chartofind + 1)); } // h else if (grid[row + 1][col + 1] == word.charat(chartofind)) { gridcheck(row + 1, col + 1, word, word.charat(chartofind + 1)); } } // letter on rightmost column of grid if (col == this.rowlength && row > 0 && row < this.noofrows) { // if (grid[row - 1][col - 1] == word.charat(chartofind)) { gridcheck(row - 1, col - 1, word, word.charat(chartofind + 1)); } // b else if (grid[row - 1][col] == word.charat(chartofind)) { gridcheck(row - 1, col, word, word.charat(chartofind + 1)); } // d else if (grid[row][col - 1] == word.charat(chartofind)) { gridcheck(row, col - 1, word, word.charat(chartofind + 1)); } } } // if word not found return false; }
the constructor grid object [incase need it] follows -
/** * constructs new lettergrid object * * @param lettergridfile * file use */ public lettergrid(string lettergridfile) { try { // init scanner scanner filein = new scanner(new file(lettergridfile)); // init arraylist arraylist<string> nextline = new arraylist<string>(10); // read data while (filein.hasnextline()) { nextline.add(filein.nextline()); noofrows++; } filein.close(); rowlength = nextline.size(); grid = new char[noofrows][rowlength]; // add data grid (int rowcount = 0; rowcount < noofrows; rowcount++) { grid[rowcount] = (nextline.get(rowcount).tochararray()); } } // in case file name mistyped or nonexistent catch (ioexception exp) { system.out.println("oops, went wrong."); system.out.println("--> file not found"); } }
and finally, reference using search:
places check x = current letter check around row col code b c -1 -1 -1 0 b d x e -1 1 c 0 -1 d f g h 0 1 e 1 -1 f 1 0 g 1 1 h
thankyou =)
-apok
well first thing see think code count word "computer" in following:
xxxxxxcxxxxxx xxxxxoxxxxxxx xxxxxmxxxxxxx xxxxxxpuxxxxx xxxxxretxxxxx
where x's arbitrary letters.
i believe need implement preserves direction of search between calls.
for example, if find first letter of word "computer" @ point in grid , find 'o' north-west, code should continue search in nw direction until finds character not in word (or of course if hits "wall")
Comments
Post a Comment