gpt4 book ai didi

java - 如何在不扩展它的情况下从一个类获取到另一个类的值(value)?

转载 作者:行者123 更新时间:2023-11-30 08:04:44 26 4
gpt4 key购买 nike

我是编程新手,可能这将是一个愚蠢的问题,但它是:

这是我的类(class)板:

public class Board {

public static final int COLOR_WHITE = 1;
public static final int COLOR_BLACK = 2;

PlayingPiece[][] board;
private boolean isFirstMove;
private int color;

public Board() {
this.setBoard(new PlayingPiece[8][8]);
this.isFirstMove = true;
this.initializePieces();

}

// Initialize the chess pieces
public void initializePieces() {

for (int i = 0; i < 8; i++) {
board[1][i] = new Pawn(1, i, COLOR_WHITE);
}

for (int i = 0; i < 8; i++) {
board[6][i] = new Pawn(6, i, COLOR_BLACK);
}

board[0][0] = new Rook(0, 0, COLOR_WHITE);
board[0][7] = new Rook(0, 7, COLOR_WHITE);
board[7][0] = new Rook(7, 0, COLOR_BLACK);
board[7][7] = new Rook(7, 7, COLOR_BLACK);

board[0][1] = new Knight(0, 1, COLOR_WHITE);
board[0][6] = new Knight(0, 6, COLOR_WHITE);
board[7][1] = new Knight(7, 1, COLOR_BLACK);
board[7][6] = new Knight(7, 6, COLOR_BLACK);

board[0][2] = new Officer(0, 2, COLOR_WHITE);
board[0][5] = new Officer(0, 5, COLOR_WHITE);
board[7][2] = new Officer(7, 2, COLOR_BLACK);
board[7][5] = new Officer(7, 5, COLOR_BLACK);

board[0][3] = new Queen(3, 0, COLOR_WHITE);
board[0][4] = new King(4, 0, COLOR_WHITE);
board[7][3] = new Queen(7, 3, COLOR_BLACK);
board[7][4] = new King(7, 4, COLOR_BLACK);

this.printBoard();

}

public boolean play(int color, int fromX, int fromY, int toX, int toY) {


boolean isTrue = false;
// Check if this is the first turn and only white can move
if (isFirstMove && color == COLOR_WHITE) {
isTrue = true;

} else if (isFirstMove && color == COLOR_BLACK) {
return false;
}
// check if player plays 2 times in a raw and if you move the piece from
// current possition
if (color == this.color || (toX == fromX && toY == fromY)) {
return false;
}

isTrue = true;

if (isTrue == true) {

this.isFirstMove = false;
// Check if player plays with his own color
if (((board[fromX][fromY]).getColor() != color)) {
return false;
}


// Check the isLegal movement of every chess piece
if ((board[fromX][fromY]).move(toX, toY)) {
board[toX][toY] = board[fromX][fromY];
board[fromX][fromY] = null;
}

this.printBoard();

}
return isTrue;
}

public PlayingPiece[][] getBoard() {
return board;
}

public void setBoard(PlayingPiece[][] board) {
this.board = board;
}

我想得到这个的值:

board[toX][toY];

好的,之后这是我的另一门国际象棋棋子类(class):

public class PlayingPiece {

public static final int COLOR_WHITE = 1;
public static final int COLOR_BLACK = 2;
public static final char BLACK_PAWN = '\u265F';
public static final char BLACK_ROOK = '\u265C';
public static final char BLACK_KNIGHT = '\u265E';
public static final char BLACK_BISHOP = '\u265D';
public static final char BLACK_QUEEN = '\u265B';
public static final char BLACK_KING = '\u265A';
public static final char WHITE_PAWN = '\u2659';
public static final char WHITE_ROOK = '\u2656';
public static final char WHITE_KNIGHT = '\u2658';
public static final char WHITE_BISHOP = '\u2657';
public static final char WHITE_QUEEN = '\u2655';
public static final char WHITE_KING = '\u2654';
public static final char NO_PIECE = ' ';

private int x, y;
private boolean isAlive;
private int color;
private char symbol;


protected PlayingPiece (int newX, int newY, int newColor) {
this.setX(newX);
this.setY(newY);
this.color = newColor;
this.isAlive = true;

}

protected PlayingPiece(int newX, int newY) {
this.setX(newX);
this.setY(newY);
}


protected PlayingPiece() {

}

public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}

public int getY() {
return y;
}

public void setX(int x) {
this.x = x;
}

protected boolean moveIsLegal (int newX, int newY) {
boolean isLegal = false;

if ((0 <= newX && newX <= 7) && (0 <= newY && newY <= 7)){

isLegal = true;
}
return isLegal;
}

public boolean move (int newX, int newY) {
if (moveIsLegal(newX, newY)) {
setX(newX);
setY(newY);
return true;
}
return false;
}


public int getColor() {
return color;
}

public boolean isAlive() {
return isAlive;
}

public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}

public char getSymbol() {
return symbol;
}

public void setSymbol(char symbol) {
this.symbol = symbol;
}

}

现在这是扩展 PlayingPieces 的 Pawn 类:

    public class Pawn extends PlayingPiece {

private boolean hasBeenMoved;

protected Pawn(int newX, int newY, int color) {
super(newX, newY, color);
this.hasBeenMoved = false;

if (color == COLOR_BLACK) {
this.setSymbol(BLACK_PAWN);
} else {
this.setSymbol(WHITE_PAWN);
}

}

@Override
public boolean move(int newX, int newY) {
if (super.move(newX, newY)) {
this.hasBeenMoved = true;
return true;
}
return false;
}

@Override
protected boolean moveIsLegal(int newX, int newY) {

boolean isLegal = false;

int newPositionX = newX - this.getX();

if (super.moveIsLegal(newX, newY)) {
if ((hasBeenMoved == false)
&& (((Math.abs(newPositionX) <= 2) && getY() == newY))) {
isLegal = true;
} else if ((hasBeenMoved == true)
&& (((Math.abs(newPositionX) <= 1) && getY() == newY)) && isValidTrace(newX, newY)) {

isLegal = true;
}
}
return isLegal;
}

public boolean isValidTrace(int newX, int newY) {


PlayingPiece[][] array = new PlayingPiece[8][8];

if (array[newX][newY].equals(new PlayingPiece())) {
return false;
}
return true;

}

}

现在在这个方法中 isValidTrace() 我想从 Board 类获取 board[toX][toY] 的值,如何在没有任何扩展的情况下做到这一点在这里?

最佳答案

另一个解决方案(假设您不需要多个 Board 实例)是使 board 静态。

public static PlayingPiece[][] board;

然后您可以使用 Board.board 从 Pawn 类访问它

关于java - 如何在不扩展它的情况下从一个类获取到另一个类的值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31294408/

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