Recherche de mots en Java


Je fais un jeu de recherche de mots simple en java. Le code pour trouver horizontalement fonctionne bien jusqu'à un certain point, comme lors de la recherche de mots de deux lettres ou d'autres mots. Il imprimera que le mot est trouvé deux fois, le premier résultat est correct tandis que le second est incorrect. Le code ci-dessous est pour la fonction de recherche de mon programme. Je pense que le problème se produit dans l'instruction if ci-dessous l'appel de la méthode findRight.

// to check if each row contains the user imputed word
public void search(String inWord){
    //loop through the arrays row length
    for(int i = 0; i < wordPuzzle.length; i++) {
        // loop through the column length in terms of array length
        for(int j = 0; j < wordPuzzle[i].length; j++) {
            if (wordPuzzle[i][j] == inWord.charAt(0)) {
                findRight(wordPuzzle[i]);
                    if(findRight(wordPuzzle[i])){
                        System.out.println(word+" found horizontally at row "+i+" and column "+j+"!");
                    }
                findDown();
                    //if(findRight(wordPuzzle[i])){
                    //    System.out.println(word+" found vertically at row "+i+" and column "+j+"!");
                    //}
                findDiagonal();
                    //if(){
                    //    System.out.println(word+" found diagonally at row "+i+" and column "+j+"!");
                    //}
                // needs to print if the word has been found in the directions
            }
        }
    }
}

public boolean findRight(char[] inArray) {
    String row = new String(inArray);
    boolean wordFlag = false;
    for(int i = 0; i < word.length(); i++){
        if(row.contains(word)) {
           for(int j = 0; j < row.length(); j++){
               if(row.charAt(j) == (word.charAt(0))){
                  String subRow = new String(inArray, j, word.length());
                  if(subRow.contains(word)) {
                     wordFlag = true;
                    } 
                }
            }
        }
    }
    return wordFlag;
} 
Author: user3113722, 2014-03-28

1 answers

Voici un exemple de recherche de mots sur la carte MxN en Java. Les mots peuvent être placés dans n'importe quel sens: Horizontalement (de gauche à droite, de droite à gauche), Verticalement(de haut en bas, de bas en haut), en Diagonale (n'importe quelle direction).

import java.util.InputMismatchException;

/**
 * Created on 1/21/17.
 */
public class WordSearchPuzzle {

    private int currentChar; // Index of current character searched in word array
    private char word[]; //Searched word

    public boolean wordExists(char board[][], String word) {

        if (word.isEmpty()) {
            throw new InputMismatchException("Searched word can not be empty!");
        }
        this.word = new char[word.length()];
        currentChar = 0;

        for (int a = 0; a < word.length(); a++) {
            word.getChars(0, word.length(), this.word, 0);
        }

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (this.word.length != 0 && board[i][j] == this.word[currentChar]) {
                    if (this.word.length == 1) {
                        return true;
                    }

                    if (letterExists(board, i, j, this.word[++currentChar], "N")) {
                        return true;
                    }
                    currentChar = 0;
                    if (letterExists(board, i, j, this.word[++currentChar], "NE")) {
                        return true;
                    }
                    currentChar = 0;
                    if (letterExists(board, i, j, this.word[++currentChar], "E")) {
                        return true;
                    }
                    currentChar = 0;
                    if (letterExists(board, i, j, this.word[++currentChar], "SE")) {
                        return true;
                    }
                    currentChar = 0;
                    if (letterExists(board, i, j, this.word[++currentChar], "S")) {
                        return true;
                    }
                    currentChar = 0;
                    if (letterExists(board, i, j, this.word[++currentChar], "SW")) {
                        return true;
                    }
                    currentChar = 0;
                    if (letterExists(board, i, j, this.word[++currentChar], "W")) {
                        return true;
                    }
                    currentChar = 0;
                    if (letterExists(board, i, j, this.word[++currentChar], "NW")) {
                        return true;
                    }
                    //otherwise continue to search from first letter of given word,
                    //starting with some another position in the board
                    currentChar = 0;
                    continue;
                }
            }
        }
        return false;
    }

    public boolean letterExists(char board[][], int i, int j, char letter, String direction) {

        if (i < 0 || i > board.length || j < 0 || j > board.length) {
            throw new IndexOutOfBoundsException("Unable to search for letter " + letter + " with coordinates (" + i + ", " + j + ")");
        }

        currentChar++;//advance search character to next letter in word

        if (i - 1 >= 0 && board[i - 1][j] == letter && direction.equals("N")) { //search N
            if (currentChar == word.length)
                return true;
            return letterExists(board, i - 1, j, word[currentChar], "N");
        } else if (i - 1 >= 0 && j + 1 < board[i].length && board[i - 1][j + 1] == letter && direction.equals("NE")) {//search NE
            if (currentChar == word.length)
                return true;
            return letterExists(board, i - 1, j + 1, word[currentChar], "NE");
        } else if (j + 1 < board[i].length && board[i][j + 1] == letter && direction.equals("E")) { //search E
            if (currentChar == word.length)
                return true;
            return letterExists(board, i, j + 1, word[currentChar], "E");
        } else if (i + 1 < board.length && j + 1 < board[i + 1].length && board[i + 1][j + 1] == letter && direction.equals("SE")) { //search SE
            if (currentChar == word.length)
                return true;
            return letterExists(board, i + 1, j + 1, word[currentChar], "SE");
        } else if (i + 1 < board.length && board[i + 1][j] == letter && direction.equals("S")) {//search S
            if (currentChar == word.length)
                return true;
            return letterExists(board, i + 1, j, word[currentChar], "S");
        } else if (i + 1 < board.length && j - 1 >= 0 && board[i + 1][j - 1] == letter && direction.equals("SW")) { //search SW
            if (currentChar == word.length)
                return true;
            return letterExists(board, i + 1, j - 1, word[currentChar], "SW");
        } else if (j - 1 >= 0 && board[i][j - 1] == letter && direction.equals("W")) { //search W
            if (currentChar == word.length)
                return true;
            return letterExists(board, i, j - 1, word[currentChar], "W");
        } else if (j - 1 >= 0 && i - 1 >= 0 && board[i - 1][j - 1] == letter && direction.equals("NW")) { //search NW
            if (currentChar == word.length)
                return true;
            return letterExists(board, i - 1, j - 1, word[currentChar], "NW");
        }
        return false;
    }

    public static void main(String[] args) {
        char row1[] = {'a', 'm', 'e', 'r' };
        char row2[] = {'m', 'i', 'z', 'g' };
        char row3[] = {'k', 'l', 'b', 'f' };
        char row4[] = {'s', 't', 'o', 'c' };
        char row5[] = {'b', 'a', 'y', 'a' };

        char board[][] = new char[5][4];
        board[0] = row1;
        board[1] = row2;
        board[2] = row3;
        board[3] = row4;
        board[4] = row5;

        WordSearchPuzzle puzzle = new WordSearchPuzzle();
        if (puzzle.wordExists(board, "ezb")) {
            System.out.println("Word exists!");
        } else {
            System.out.println("Word doesn't exist!");
        }
    }
}
 0
Author: azec_pdx, 2017-01-22 03:56:18