简体   繁体   中英

Using a 2D arraylist to create a chessboard and pieces

I'm currently trying to create a program that will spit out a chessboard like this one (it looks better in the actual program, just to editor doesn't like me using the "-" symbol so I put them in quotation marks):

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

I'm using two methods, a showBoard method and an addPiece method. I'm currently stuck with the addPiece method, and I'm trying to make it so the method takes three inputs: the row int, the column int, and the string name (just K for king, for example). However, I can't get the addPiece method to put the pieces where I want them to go, or even at all. Here's what I have so far:

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

I know it has something to do with the way I wrote my addpiece method, but I'm still kind of confused as to how writing the method should be, and that is my best attempt (which doesn't work). Does anyone have any suggestions? Thanks!

You never print the pieces values

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

}

And also you'll need to add the pience before you show the board:

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

Why are you using new String(r)? Your board array is already an array of Strings, just use:

board[x][y] = r;

Also you are adding the piece after the showBoard method in main, switch them around

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

Note that addPiece is changing the state of the board. If you want to see that change, you need to redisplay the new board state.

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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM