如何在没有延伸的情况下从一个类到另一个类获得价值?

时间:2015-07-08 13:36:57

标签: java

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

这是我的班级主席:

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

好了之后,这是我的另一个国际象棋课程:

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

}

现在这是Pawn类扩展了PlayingPieces:

    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[toX][toY]的价值,我怎么能在没有延伸的情况下这样做?

3 个答案:

答案 0 :(得分:1)

另一种解决方案(假设您不需要多个Board实例)正在使电路板静止。

public static PlayingPiece[][] board;

然后您可以使用Board.board

从您的Pawn类访问它

答案 1 :(得分:0)

我不知道我是否理解你的问题但是......

如果您有2个A和B类而不是继承,则可以使用聚合。例如,您想要使用A类中对象B的某些方法:

class A {
    private B b;
}

然后在A类的内部方法中使用b.nameOfTheMethod()

答案 2 :(得分:0)

您可以创建一个方法来返回数组,并在您想要数据的类中调用它。

public PlayingPiece getBoard()
{
     return board;
}