2013-04-10 92 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. 
    } 
}