Передача массива в качестве параметра - шахматная доска - PullRequest
0 голосов
/ 07 ноября 2018

Я не вижу, что не так, я пытаюсь передать массив 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 голосов
/ 07 ноября 2018

findPiece ожидает символ [] [] в качестве первого параметра, а не всей платы класса. Вам нужно вызвать метод findPiece с первым параметром как gameBoard.board;

0 голосов
/ 07 ноября 2018

Вы должны передать gameBoard.board: на самом деле, вы передаете весь экземпляр этого класса (gameBoard), а не только его компонент массива. Итак, это правильно: вы получили сообщение о том, что вы не передаете массив.

...