如何在字符串的二维数组

时间:2016-10-01 18:19:26

标签: java arrays 2d puzzle shift

所以我正在制作一个8puzzle幻灯片游戏,我遇到了一些麻烦。所以我要说现在的状态S是:

1 5 6

3 7 B

2 8 4

其中B代表空格(这就是我在String类型中进行2D数组的原因)。所以我试图调用移动方法,最终将向上,向下,向左或向右移动B空间。我已经记录了B的位置,所以在这种情况下它会是[1,2]。我使用这个B位置看看我是否可以进行有效的上移(如果B [0] = 0则不能进行),有效向下移动(如果B [0] = 2则不能进行下移),有效左移(如果B [1] = 0则不能进行)或有效向右移动(如果B [1] = 2则不能进行)。所以现在如果我确实有一个有效的举动让我们说,我将如何实现该移动功能?如果所有内容都是字符串类型,我不知道如何将S状态下的B位置替换为上面的位置。

public class EightPuzzle {

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

/*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(Arrays.deepToString(gameBoard));

    return gameBoard;   
}

public String[][] randomizeState(){
    return null;
}

public void move(String dir){
    if(dir.equalsIgnoreCase("up")){
        if(bLocation[0].equals("0")){
            //cannot move up
        }
        else{
            int[] temp;

        }
    }
    if(dir.equalsIgnoreCase("down")){
        if(bLocation[0].equals("2")){
            //cannot move down
        }
        else{

        }

    }
    if(dir.equalsIgnoreCase("left")){
        if(bLocation[1].equals("0")){
            //cannot move left
        }
        else{

        }

    }
    if(dir.equalsIgnoreCase("right")){
        if(bLocation[1].equals("2")){
            //cannot move right
        }
        else{

        }
    }
}

public void bLocation(String board){
    setState(board);
    for(int i=0; i<gameBoard.length; i++){
        for(int j=0; j<gameBoard[i].length; j++){
            if(gameBoard[i][j].equals("b"))
            {
                bLocation[0] = Integer.toString(i);
                bLocation[1] = Integer.toString(j);
            }
        }
    }   
}

public static void main (String[]args){
    EightPuzzle b1=new EightPuzzle();
    b1.setState("156 37b 284");
    b1.bLocation("156 37b 284");

}

}

1 个答案:

答案 0 :(得分:0)

向上移动意味着将B与其上方的图块交换。

为了简化代码,请创建一个交换两个位置的方法moveB

private void moveB(int deltaRow, int deltaCol) {
    int newRow = bLocation[0] + deltaRow;
    int newCol = bLocation[1] + deltaCol;

    String temp = gameboard[newRow][newCol];
    gameBoard[newRow][newCol] = gameBoard[bLocation[0]][bLocation[1]];
    gameBoard[bLocation[0]][bLocation[1]] = temp;

    bLocation[0] = newRow;
    bLocation[1] = newCol;
}

向上移动:moveB(-1, 0);

向下移动:moveB(1, 0);

向左移动:moveB(0, -1);

向右移动:moveB(0, 1);