Java:在二维数组中搜索单词 [英] Java: search words in two dimensional array

查看:32
本文介绍了Java:在二维数组中搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 Java 大约 4 个月了,它是我学习的第一门编程语言.对于学校,我们必须做一个项目,一个基于控制台的游戏.我选择了 Boggle.

I've been learning Java for about 4 months now, it's the first programming language I learn. For school we have to do a project, a console-based game. I chose for Boggle.

我有一个带骰子的 ArrayList,每个骰子都有一个随机的面朝上",然后 ArrayList 被打乱,二维数组被每一面朝上的值填充.此时数组充满了字符串,字符可能是更好的选择,但很容易改变.

I have an ArrayList with dices, each one gets a random 'side up', and then the ArrayList gets shuffled and a two-dimensional array gets filled with the values of each side up. At this moment the Array is filled with Strings, chars may be a better option but something that's fairly easy to change.

我面临的问题是我需要能够在数组中找到单词.Boggle 中的单词可以朝任何方向前进,每个唯一的块每个单词只能使用一次,但是路径可以交叉,您也可以对角搜索.我设法找到数组中是否存在第一个字母.如果不是,则可以中止搜索,如果存在,则需要开始搜索单词的第二个字符,必须在第一个字符的块周围.

The problem I'm facing is that I need to be able to find words in the array. Words in Boggle can go in any direction, each unique block can be used only once per word, but the path can cross, you can search diagonally too. I managed to find if the first letter is present in the array. If not than the search can be aborted, if it is present there needs to start a search that searches for the second character of the word, that has to be around the first character's block.

我做了一些数学计算,发现它总是例如i-1 和 j-1"作为周围块的左上角.我解决了这个问题,但似乎无法找到单词......此外,如果周围有 2 个e",我不知道如何搜索尝试每个e"的单词.到目前为止,这是我的代码:

I did some math and found that it's always for example "i-1 and j-1" as the upper left corner of the surrounding blocks. I worked this out but can't seem to be able to find words... Also, if there are 2 "e"'s surrounding, I have no clue how to search for the word trying each "e". Here is my code so far:

这是我此刻最重要的类,游戏板类

This is my most important class at this moment, the class Gameboard

public class Gameboard {

private List<Dice> dices = new ArrayList<Dice>();
private final int boardSize;
private String[][] board;
private boolean [][] blocksAvailable;
private Random random = new Random();

public GameBoard() {
    // Making the board with a given size (will be changeable later but is "4" for now)
    boardSize = 4;
    board = new String[boardSize][boardSize];
    blocksAvailable = new boolean[boardSize][boardSize];
    for(int i = 0; i < boardSize; i++) {
        for(int j = 0; j < boardSize; j++) {
            blocksAvailable[i][j] = true;
        }
    }
}

public String[][] getBoard() {
    return board;
}

public int getFullSize() {
    return boardSize*boardSize;
}

public void makeBoard() {
    //random "side up" for each dice
    for(int i = 0; i < dices.size(); i++) {
        dices.get(i).setSideUp();
    }

    // Shuffle all dices
    Collections.shuffle(dices);

    // Fill the board with the values of the dices
    int counter = 0;
    for(int i = 0; i < boardSize; i++) {
        for(int j = 0; j < boardSize; j++) {
            board[i][j] = dices.get(counter++).getSideUp();
        }
    }


}

public String showBoard() {
    //Show the board, each block divided by "|"
    String str = "";
    for(int i = 0; i < boardSize; i++) {
        for(int j = 0; j < boardSize; j++) {
            str += String.format("|%s|", board[i][j].toString());
            if(j == 3) {
                str += "\n";
            }
        }
    }
    return str;
}

public void addDices() {
    dices.add(new dice("R", "I", "F", "O", "B", "X"));
    dices.add(new dice("I", "F", "E", "H", "E", "Y"));
    dices.add(new dice("D", "E", "N", "O", "W", "S"));
    dices.add(new dice("U", "T", "O", "K", "N", "D"));
    dices.add(new dice("H", "M", "S", "R", "A", "O"));
    dices.add(new dice("L", "U", "P", "E", "T", "S"));
    dices.add(new dice("A", "C", "I", "T", "O", "A"));
    dices.add(new dice("Y", "L", "G", "K", "U", "E"));
    dices.add(new dice("Q", "B", "M", "J", "O", "A"));
    dices.add(new dice("E", "H", "I", "S", "P", "N"));
    dices.add(new dice("V", "E", "T", "I", "G", "N"));
    dices.add(new dice("B", "A", "L", "I", "Y", "T"));
    dices.add(new dice("E", "Z", "A", "V", "N", "D"));
    dices.add(new dice("R", "A", "L", "E", "S", "C"));
    dices.add(new dice("U", "W", "I", "L", "R", "G"));
    dices.add(new dice("P", "A", "C", "E", "M", "D"));
}

public boolean searchWord(String word) {
    String wordUp = woord.toUpperCase();
    String firstLetter = Character.toString(wordUp.charAt(0));


    for(int i = 0; i < boardSize;i++) {
        for(int j = 0; j < boardSize;j++) {
            if(firstLetter.equals(board[i][j]) == true) {

                int a = i;
                int b = j;
                String theLetter = "";
                // First letter found, continue search
                for(int h = 1; h < hetWoord.length(); h++) {
                    theLetter = Character.toString(wordUp.charAt(h));
                    int[] values = searchLetter(theLetter, a, b);
                    if(values[0] > -1) {
                        a = values[0];
                        b = values[1];
                    } else {
                        return false;
                    }
                }
                return true;

            }
        }
    }
    return false;
}

public int[] searchLetter(String letter, int i, int j) {
    int[] values = new int[2];

    try{if(board[i-1][j-1].equals(letter) && blocksAvailable[i-1][j-1] == true) {
        values[0] = i-1;
        values[1] = j-1;
        blocksAvailable[i-1][j-1] = false;
    }  else if(board[i-1][j].equals(letter) && blocksAvailable[i-1][j] == true) {
        values[0] = i-1;
        values[1] = j;
        blocksAvailable[i-1][j] = false;
    } else if(board[i-1][j+1].equals(letter) && blocksAvailable[i-1][j+1] == true) {
        values[0] = i-1;
        values[1] = j+1;
        blocksAvailable[i-1][j+1] = false;
    } else if(board[i][j-1].equals(letter) && blocksAvailable[i][j-1] == true) {
        values[0] = i;
        values[1] = j-1;
        blocksAvailable[i][j-1] = false;
    } else if(board[i][j+1].equals(letter) && blocksAvailable[i][j+1] == true) {
        values[0] = i;
        values[1] = j+1;
        blocksAvailable[i][j+1] = false;
    } else if(board[i+1][j-1].equals(letter) && blocksAvailable[i+1][j-1] == true) {
        values[0] = i+1;
        values[1] = j-1;
        blocksAvailable[i+1][j+1] = false;
    } else if(board[i+1][j].equals(letter) && blocksAvailable[i+1][j] == true) {
        values[0] = i+1;
        values[1] = j;
        blocksAvailable[i+1][j] = false;
    } else if(board[i+1][j+1].equals(letter) && blocksAvailable[i+1][j+1] == true) {
        values[0] = i+1;
        values[1] = j+1;
        blocksAvailable[i+1][j+1] = false;
    } else {
        values[0] = -1;  // If not found, negative values, easy to check in searchWord if letter was found
        values[1] = -1;
    }}catch (ArrayIndexOutOfBoundsException e) {

    }

    return values;
}
}

推荐答案

这可能会被删除,因为我不打算回答你的问题.但是请,非常请,使用:

This may get deleted, because I am not going to answer your question. But please, pretty please, use:

// instead of: board[i-1][j-1]

public String getBoardValue(int x, int y) {
  if (x<0 || x>=boardSize) return "";
  if (y<0 || y>=boardSize) return "";
  return board[x][y];
}

使用这种辅助方法,您将

Using such helper method you will

  • 确保没有抛出 IndexArrayOutOfBoundsException
  • 总是有一个非空的板值

这篇关于Java:在二维数组中搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆