我目前正在尝试创建一个程序,它将吐出像这样的棋盘(它在实际程序中看起来更好,只是编辑不喜欢我使用“-”符号所以我把它们放在引号中):
-----------------
| | | | |K| | | |
-----------------
| |P| | | |P| | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | |N| | |
-----------------
| | | | |K| | | |
-----------------
我使用了两种方法,一个 showBoard 方法和一个 addPiece 方法。我目前坚持使用 addPiece 方法,并且我正在尝试使该方法接受三个输入:行 int、列 int 和字符串名称(例如,仅 K 表示 king)。但是,我无法使用 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 方法的方式有关,但我仍然对如何编写该方法感到困惑,这是我最好的尝试(这是行不通的)。有没有人有什么建议?谢谢!