将数组作为参数棋盘传递

时间:2018-11-07 16:27:02

标签: java arrays methods chess

我看不到有什么问题,我正在尝试将gameBoard数组(这不是数组吗?-参见构造函数)传递给findPiece方法,但它说 不是数组,我应该在这里传递什么以获得未更新的电路板?抱歉,我是编程新手,但我非常感谢任何提示!

public class Game {



    private Board gameBoard;



    public Game() {
        gameBoard = new Board();
    }


    public void play(Board board) {

        EasyIn2 reader = new EasyIn2();

        gameBoard = new Board();     //initializes the board so dont need to do so in main

        boolean done = false;

        while(!done) {                     //keeps looping when no one has won yet
            gameBoard.printBoard();

            System.out.println(WHITEPLAYS_MSG);

            String pos1 = reader.getString();         //gets user input ... move from... to....   temporary variables
            int xFrom=pos1.charAt(0) - 'a';                           //to transform the letter
            int yFrom=pos1.charAt(1) - '1';                           // to transform the number

            String pos2 = reader.getString();
            int xTo=pos2.charAt(0) - 'a';                           //to transform the letter
            int yTo=pos2.charAt(1) - '1';                           // to transform the number

            gameBoard.findPiece(gameBoard,xFrom,yFrom);


}
}
}

公共类委员会{

private static final int DEFAULT_SIZE = 8;             //images for pieces to be displayed on board
private static final char FREE = '.';
private static final char WHITEROOK = '♖';
private static final char BLACKROOK = '♜';
private static final char WHITEBISHOP = '♗';
private static final char BLACKBISHOP = '♝';



private static final char WHITEKING = '♔';
private static final char BLACKKING = '♚';
private static final char WHITEQUEEN = '♕';
private static final char BLACKQUEEN = '♛';
private static final char WHITEKNIGHT = '♘';
private static final char BLACKKNIGHT = '♞';
private static final char WHITEPAWN = '♙';
private static final char BLACKPAWN = '♟';

private int boardsize;
public char[][] board;


public Board() {
    this.boardsize = DEFAULT_SIZE;

    board = new char[boardsize][boardsize];

    // Clear all playable fields
    for (int x = 0; x < boardsize; x++)
        for (int y = 0; y < boardsize; y++)
            board[x][y] = FREE;


    board[0][7] = BLACKROOK;
    board[2][7] = BLACKBISHOP;
    board[5][7] = BLACKBISHOP;
    board[7][7] = BLACKROOK;
    board[0][0] = WHITEROOK;
    board[2][0] = WHITEBISHOP;
    board[5][0] = WHITEBISHOP;
    board[7][0] = WHITEROOK;


}

public boolean findPiece(char[][] boardIn, int xFrom, int yFrom) {     //checks that the player has selected a piece

    for (int i = 0; i < boardIn.length; i++) {
        for (int j = 0; j < boardIn.length; j++) {
            if (boardIn[i][j] == boardIn[xFrom][yFrom]) {      //checks the user input co-ordinate  is on the board
                break;

                if (boardIn[xFrom][yFrom] != FREE) {
                    Piece piece=new Piece();          //checks the piece is real, ie not a free space
                    piece.getPieceType(xFrom, yFrom);
                    return true;

                } else {
                    return false;
                }
            }
        }

2 个答案:

答案 0 :(得分:3)

您应该传递gameBoard.board:实际上,您正在传递该类的整个实例(gameBoard),而不仅是它的数组组件。所以,没错:您得到的错误是您没有传递数组。

答案 1 :(得分:1)

findPiece期望将char [] []作为第一个参数,而不是整个类Board。 您需要使用第一个参数作为gameBoard.board;调用findPiece方法。