如何将String中的值转换为Matrix

时间:2016-10-01 16:22:22

标签: java matrix input puzzle

所以我正在研究这个8益智游戏,我需要设置一个初始状态。所以我要说输入“1b2 345 678”,我想做各种搜索方法并移动各个部分(特别是b(空白)部分)。所以我想在矩阵中表示给定的输入,如: 1 b 2 3 4 5 6 7 8

所以我写了下面的代码:

public class EightPuzzle {

    String[][] gameBoard = new String[3][3];
    String bLocation;
    String board;


    /*public void ReadFromTxt(String file) throws FileNotFoundException, IOException {
        String read; 
        FileReader f = new FileReader(file);
        int i = 0;
        int j;
        BufferedReader b = new BufferedReader(f);
        System.out.println("Loading puzzle from file...");
        while((read = b.readLine())!=null){
            if(read.length()==3){
                for(j=0;j<3;j++){
                    board[i][j] = (int)(read.charAt(j)-48);
                }
            }
            i++;
        }
        b.close();
        System.out.println("Puzzle loaded!");
    }*/

    public String[][] setState(String board){

        gameBoard[0][0] = board.substring(0,1);
        gameBoard[0][1] = board.substring(1,2);
        gameBoard[0][2] = board.substring(2,3);
        gameBoard[1][0] = board.substring(4,5);
        gameBoard[1][1] = board.substring(5,6);
        gameBoard[1][2] = board.substring(6,7);
        gameBoard[2][0] = board.substring(8,9);
        gameBoard[2][1] = board.substring(9,10);
        gameBoard[2][2] = board.substring(10,11);
        System.out.println(gameBoard);

        return gameBoard;

    }

    public static void main (String[]args){

        EightPuzzle b1=new EightPuzzle();
        b1.setState("b12 345 678");

    }

}

但是当我运行它时,它只显示我的gameBoard存储为:[[Ljava.lang.String; @ 15db9742我做错了什么?

0 个答案:

没有答案