Java 将数组作为参数传递-棋盘
我看不出有什么不对,我正在尝试传递gameBoard数组,这不是数组吗?-请参阅FindPice方法中的构造函数,但它会这样说 这不是一个数组,我应该在这里传递什么来获得未更新的板?对不起,我是编程新手,但我真的很感激任何提示Java 将数组作为参数传递-棋盘,java,arrays,methods,chess,Java,Arrays,Methods,Chess,我看不出有什么不对,我正在尝试传递gameBoard数组,这不是数组吗?-请参阅FindPice方法中的构造函数,但它会这样说 这不是一个数组,我应该在这里传递什么来获得未更新的板?对不起,我是编程新手,但我真的很感激任何提示 public class Game { private Board gameBoard; public Game() { gameBoard = new Board(); } public void play
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;
}
}
}
你应该通过gameBoard.board:实际上,你正在通过该类gameBoard的整个实例,而不仅仅是它的数组组件。因此,这是对的:你被告知没有通过数组的错误。你应该通过gameBoard.board:实际上,你正在通过该类gameBoard的整个实例,而不仅仅是数组组件因此,它是正确的:您收到的错误说您没有传递数组。FindPice需要一个char[][]作为第一个参数,而不是整个类板。
您需要调用findPiece方法,第一个参数为gameBoard.board;findPiece需要一个char[][]作为第一个参数,而不是整个类板。 您需要使用第一个参数gameBoard.board调用findPiece方法