Search code examples
javaarraysmethodschess

Passing an array as a parameter- chess board


I can't see what is wrong, I am trying to pass the gameBoard array (is this not an array?- see constructor) into the findPiece method, but it says it isn't an array, what should I be passing here to get un updated board? Sorry, I am new to programming but I really appreciate any hints!

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


}
}
}

public class Board {

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

Solution

  • You should pass gameBoard.board: actually, you are passing the entire instance of that class (gameBoard), not just the array component of it. So, it's right: the error you got said that you are not passing an array.