2013-04-10 96 views
0

我目前正在嘗試創建一個程序,它會吐出一個像這樣的棋盤(它在實際的程序中看起來更好,只是爲了編輯器不喜歡我使用「 - 」符號,所以我把它們放在引號):使用2D arraylist創建一個棋盤和棋子

----------------- 
| | | | |K| | | | 
----------------- 
| |P| | | |P| | | 
----------------- 
| | | | | | | | | 
----------------- 
| | | | | | | | | 
----------------- 
| | | | | | | | | 
----------------- 
| | | | | | | | | 
----------------- 
| | | | | |N| | | 
----------------- 
| | | | |K| | | | 
----------------- 

我使用兩種方法,一個showBoard方法和addPiece方法。我目前堅持使用addPiece方法,並且我試圖讓它使用三個輸入:行int,列int和字符串名稱(例如K只是K)。但是,我無法使用addPiece方法將片段放到我想要的位置,或者甚至完全沒有問題。這是我到目前爲止有:

public class ChessBoard { 

public static String[][] board = new String[8][8]; 
public static int row = 0; 
public static int col = 0; 

public static void addPiece(int x, int y, String r){ 

    board[x][y] = new String(r); 
} 

public static void showBoard(){ 
    for (row = 0; row < board.length; row++) 
    { 
     System.out.println(""); 
     System.out.println("---------------"); 

     for(col = 0; col < board[row].length; col++) 
     { 
      System.out.print("| "); 

     } 
    } 
    System.out.println(""); 
    System.out.println("---------------"); 
} 

public static void main(String[] args) { 
System.out.println(board.length); 
showBoard(); 
addPiece(1,2,"R"); 
} 
} 

我知道它是與我寫我addpiece法的方式,但我仍然爲編寫方法應該怎麼樣的困惑,那就是我最好的嘗試(不起作用)。有沒有人有什麼建議?謝謝!

+0

如果您將帖子的部分標記爲代碼,編輯器將接受它們,而不管它們包含的任何格式化符號。當你showBoard()第一次顯示板時,你需要輸入 – 2013-04-10 17:27:50

+2

。你添加你的作品。請注意,你之後沒有再次顯示板子? – 2013-04-10 17:28:01

+1

此外,你可以簡單地寫'board [x] [y] = r;' – 2013-04-10 17:29:31

回答

1

你爲什麼使用新的String(r)?您板陣列已經是一個字符串數組,只需使用:

board[x][y] = r; 

而且要添加在主要的showBoard方法後片,開關周圍

addPiece(1,2,"R"); 
showBoard(); 
2

你永遠不打印件值

for(col = 0; col < board[row].length; col++) 
{ 
    if (board[row][col] != null) { 
     System.out.print("|" + board[row][col]); 
    } 
    else 
     System.out.print("| "); 

} 

,你也將需要添加pience您展示板前:

addPiece(1,2,"R"); //before 
showBoard(); 
+0

這工作!非常感謝!看看你的代碼,看起來非常明顯,我需要在showboard方法中打印這些片段值,而這之前我沒有想到。再次感謝。 – penguinteacher 2013-04-10 17:39:13

+0

不客氣:) – 2013-04-10 19:13:43

1

請注意,addPiece正在改變電路板的狀態。如果你想看到這種變化,你需要重新顯示新的電路板狀態。

public class ChessBoard { 

    public static String[][] board = new String[8][8]; 

    public static void addPiece(int x, int y, String r){ 

     board[x][y] = r;//no need for new String(), board is already made of Strings. 
    } 

    public static void showBoard(){ 
     //it's generally better practice to initialize loop counters in the loop themselves 
     for (int row = 0; row < board.length; row++) 
     { 
      System.out.println(""); 
      System.out.println("---------------"); 

      for(int col = 0; col < board[row].length; col++) 
      { 

       System.out.print("|"); //you're only printing spaces in the spots 
       if(board[column][row] == null){ 
        System.ot.print(" "); 
       }else{ 
        System.out.print(board[column][row]); 
       } 
      } 
     } 
     System.out.println(""); 
     System.out.println("---------------"); 
    } 

    public static void main(String[] args) { 
     System.out.println(board.length); 
     showBoard();  //board does not have R in it yet. 
     addPiece(1,2,"R"); //board now has R in it. 
     showBoard();  //display the new board with R in it. 
    } 
}