繁体   English   中英

如何在2D字符串数组中移动元素

[英]How to shift elements in a 2D array of Strings

所以我在做一个8puzzle幻灯片游戏,遇到了一些麻烦。 因此,可以说我当前的状态S为:

1 5 6

3 7 B

2 8 4

其中B代表空格(这就是为什么我要在String类型中执行2D数组)。 因此,我试图调用move方法,这些方法最终将使B空间向上,向下,向左或向右移动。 我已经记录了B的位置,因此在这种情况下为[1,2]。 我使用此B位置来查看是否可以进行有效的上移(如果B [0] = 0则不能进行有效移动)(有效的下移(如果B [0] = 2则不能进行有效)移动)向左(如果B [1] = 0则无法创建)或向右有效移动(如果B [1] = 2则无法创建)。 因此,现在如果我有一个有效的举动供大家说,我将如何实现该举动功能? 如果一切都是String类型,我不知道如何用上面的状态准确地替换处于S状态的B的位置。

公共课程ThreePuzzle {

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");

}

}

向上移动意味着将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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM