簡體   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