2016-10-01 108 views
0

因此,我正在研究一個8puzzle幻燈片遊戲,並且遇到了一些麻煩。因此,可以說,我的當前狀態S是:如何移動字符串的二維數組中的元素

3 7乙

其中B表示空白空間(這就是爲什麼我正在做2D數組中的字符串類型)。所以我試圖調用移動方法,最終將B空間向上,向下,向左或向右移動。我已經記錄了B的位置,所以在這種情況下它會是[1,2]。我使用這個B位置來查看我是否可以進行有效的移動(如果B [0] = 0,則無法生成),有效移動(如果B [0] = 2,則無法生成),有效移動(如果B [1] = 0,則不能生成)或有效向右移動(如果B [1] = 2,則不能生成)。所以現在如果我確實有一個可以讓我們說出來的有效舉措,那麼我將如何去實現這個移動功能呢?我不知道如何在S狀態下用上面的狀態替換B的位置,如果一切都在String類型的話。

公共類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"); 

} 

}

+0

你可以使用'char'而不是'String'。 – 4castle

+0

你能分享一下你嘗試過的代碼嗎? –

+0

你問如何交換兩個變量的值?用'S [1] [2]'交換'S [0] [2]'? – 4castle

回答

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