如何读取多个基于文本的拼字游戏板,并将它们放入二维数组中?

How to read in multiple text-based scrabble boards, and place them into a 2D array?

我目前正在尝试制作一个拼字游戏求解器,但我在读取代表棋盘的文本文件时遇到了一些问题。例如,我有一个这样的文件:

15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2.  l  a  u  d  a  t  o  r .. .. .2
.. .. .. .. 2. .. ..  h .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 ..  o .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2  t .2 .. .. .. .2 .. ..
3. .. .. .2 .. .. ..  i .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
le*mdoe
7
3. .. .. 2. .. .. 3.
.. .3 .. .. .. .3 ..
.. ..  a  d .2 .. ..
2. ..  u  h .. .. 2.
.. ..  l  o .2 .. ..
..  m  a  t .. .3 ..
 r  e  S  i  d .. 3.
toloeri
21
4. .. .. .2 .. .. .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
.. 2. .. .. .3 .. .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
.. .. 2. .. .. .4 .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.2 .. .. 3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. .3 .. .. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.. .. .4 .. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .. .. .2 .. .. 2.  q  u  e  p .. .. .. 2. .. .. .2 .. .. ..
3. .. .. .. .. ..  l  i .. .. .. .. .. 2. .. .. .. .. .. .. 3.
..  g  l  i  t ..  a .. .3 .. .. .. .3 .. .. .. .3 .. .. 2. ..
.. ..  a .. .. .2  u ..  n .2 .. .2 .. .. .. .2 .. .. 2. .. ..
 d  a  w  d .. ..  d  h  o  t  i .. .. .. .2 .. .. 3. .. .. .2
.. ..  f  i  g  j  a  m  s .2 .. .2 .. .. .. .2 .. .. 2. .. ..
..  t  u  n  e ..  t ..  y .. .. .. .3 .. .. .. .3 .. .. 2. ..
 m  e  l  o  d  e  o  N .. .. .. .. .. 2. .. .. .. .. .. .. 3.
.. .. .. .2 .. ..  r .. .. .. .2 .. .. .. 2. .. .. .2 .. .. ..
..  v  e  z  i  r  s .. .. .2 .. .2 .. .. .. 2. .. .. .4 .. ..
.. .3 ..  o 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. .. .. .3 ..
.2  b  a  r  f .. .2 .. .. .. 3. .. .. .. .2 .. .. 3. .. .. .2
.. ..  a  r  o  w .. .. .. 2. .. 2. .. .. .. .4 .. .. 2. .. ..
.. 2. ..  o  x  y .. .. 2. .. .. .. 2. .. .. .. .3 .. .. 2. ..
 k  e  p  s ..  n .. 3. .. .. .2 .. .. 3. .. .. .. .2 .. .. 4.
ntnbtoi
15
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
3. .. .. .2 ..  c  a  t .. .. .. .2 .. .. 3.
.. .. .2 .. .. .. .2 .. .2 .. .. .. .2 .. ..
.. .3 .. .. .. .3 .. .. .. .3 .. .. .. .3 ..
.. .. .. .. 2. .. .. .. .. .. 2. .. .. .. ..
.2 .. .. 2. .. .. .. .2 .. .. .. 2. .. .. .2
.. .. 2. .. .. .. .2 .. .2 .. .. .. 2. .. ..
.. 2. .. .. .. .3 .. .. .. .3 .. .. .. 2. ..
3. .. .. .2 .. .. .. 3. .. .. .. .2 .. .. 3.
dgos*ie

棋盘上方的第一行是rows/columns的数字。然后,很明显,董事会。然后是放置字符的手。

我正在尝试实现的是一种将每个板彼此分开并独立解决的方法。我不确定如何使用扫描仪,例如,在读取 row/column 数字后停止,创建相应的 2D 数组,然后读取板并在手前停止,然后读取手并放置它们进入 Hand 对象,然后继续下一个棋盘。我不确定如何划分,如果这有意义的话。

这是我的尝试,仅此而已。如果我删除第一行并且只有一块板,我只能让它工作。在顶部被注释掉的行是我尝试只读取第一行,并创建相应的数组,但每当我这样做时,对于我的 input ArrayList,第一个 sysout 是一个空白列表,并且只有然后它开始读取板本身,这对我来说没有任何意义。

public static void populateBoard(File file) throws FileNotFoundException {
        Scanner scan = new Scanner(file);
//        String NUMBER_ROWS = scan.next();
//        System.out.println(NUMBER_ROWS);

//        board = new String[NUMBER_ROWS][NUMBER_ROWS];
        board = new String[15][15];
        int actualLine = 0;

        while(actualLine < 15) {
            ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
            // Removes blank characters which occur because of the letters on the board having a space instead
            // of a dot to their left.
            input.removeIf(string -> string.equals(""));
            System.out.println(input.toString());

            for(int i = 0; i < input.size(); i++) {
                board[actualLine][i] = input.get(i);
            }
            actualLine++;
        }

    }

我觉得我没有解释清楚,所以如果有什么我可以澄清的,请问,我会解释的。

你已经完成了大部分,你只需要将它全部包装在另一个循环中,这个循环可以找到电路板的大小并一次创建每个电路板。

我们首先创建一个板的 ArrayList,我们可以将每个板存储在 ArrayList<String[][]> boards = new ArrayList<>();

假设文件的布局总是正确的,那么接下来我们需要创建一个循环,一次找到一个板,并根据它们的大小 currentBoardSize = Integer.parseInt(line); 我们可以一次创建一个 currentBoard = new String[currentBoardSize][currentBoardSize]; 然后我们处理下一批行以匹配 currentBoardSize 并将每一行存储到我们刚刚使用您的行拆分代码创建的二维数组中 ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));;

最后,我们将完整的棋盘存储到 boards 列表 boards.add(currentBoard); 中,然后我们让 while 循环重新开始并找到下一个棋盘大小并创建一个新的二维数组直到文件中没有更多的板了。

一个完整的示例可能如下所示:

//list of boards, store this outside of the method so that it can be accessed from other places like you main method etc
public static ArrayList<String[][]> boards = new ArrayList<>();

//Load the file into the boards array, or change "void" to "ArrayList<String[][]>" if you want to return the array directly from the method
public static void populateBoard(File file) throws FileNotFoundException {
    Scanner scan = new Scanner(file);
    int currentBoardSize = 0;
    String[][] currentBoard;
    
    //Get the very first board size so that the while loop works correctly
    String line = scan.nextLine();
    
    while(line != null) {       
        //Check it the first line has no spaces and is a single number indicating a new board
        if(line.contains(" ") == false) {   
            try {   
                //Get the board size
                currentBoardSize = Integer.parseInt(line);
            }
            catch (NumberFormatException ex) {
                System.out.println("Error, unable to read board size: ");
                ex.printStackTrace();
                break;
            }
            currentBoard = new String[currentBoardSize][currentBoardSize];
            System.out.println("Created new board that is "+currentBoardSize + "x"+currentBoardSize);
        
            //populate the borad using the next number of lines based on the board size
            for (int row = 0; row < currentBoardSize; row++) {
                ArrayList<String> input = new ArrayList<>(Arrays.asList(scan.nextLine().split(" ")));
                // Removes blank characters which occur because of the letters on the board having a space instead
                // of a dot to their left.
                input.removeIf(string -> string.equals(""));
                System.out.println(input.toString());

                for (int column = 0; column < input.size(); column++) {
                    currentBoard[row][column] = input.get(column);
                }
            }
        
            //Lastly do whatever you want with the word below each board
            String wordToSolve = scan.nextLine();
            System.out.println("Word to solve: "+wordToSolve );

            //Add this board to the list of boards
            boards.add(currentBoard);
        
            //Get the next board size, or break the while loop
            try {
                line = scan.nextLine();
            }
            catch (NoSuchElementException ex) {
                break;
            }
        }
        else {
            System.out.println("Error unexpected board size found: "+line);
            break;
        }
    }
}

并且可以像这样从列表中轻松获得输出:

///boards.get(board#) grabs the board, and [row#][col#] gets the row and column (remember they are 0 based, the first one is 0 not 1)
String value = boards.get(0)[2][6];
System.out.println("Value: "+value);

上面的给定输出是 .2,正如预期的那样