0

我目前正在尝试创建一个程序,它将吐出像这样的棋盘(它在实际程序中看起来更好,只是编辑不喜欢我使用“-”符号所以我把它们放在引号中):

-----------------
| | | | |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 方法的方式有关,但我仍然对如何编写该方法感到困惑,这是我最好的尝试(这是行不通的)。有没有人有什么建议?谢谢!

4

3 回答 3

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();
于 2013-04-10T17:31:23.413 回答
1

为什么要使用 new String(r)?您的板数组已经是一个字符串数组,只需使用:

board[x][y] = r;

另外,您在 main 中的 showBoard 方法之后添加了该片段,将它们切换

addPiece(1,2,"R");
showBoard();
于 2013-04-10T17:30:16.663 回答
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.
    }
}
于 2013-04-10T17:32:22.600 回答