gpt4 book ai didi

java - 将数组作为参数传递 - 棋盘

转载 作者:行者123 更新时间:2023-11-29 08:25:55 28 4
gpt4 key购买 nike

我看不出有什么问题,我正在尝试将 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);


}
}
}

公共(public)课板{

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) 的整个实例,而不仅仅是它的数组组件。所以,这是正确的:你得到的错误是你没有传递数组。

关于java - 将数组作为参数传递 - 棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53193690/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com