Tito
Tito

Reputation: 478

How can I get value from one class to another without extends it?

I am newbie in programming and may be this will be stupid question but here it is:

This is my class board:

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

I want to get the value of this:

board[toX][toY];

OK after that here is the other my class for chess pieces:

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

}

And now this is Pawn class which extends 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;

    }

}

Now in this method isValidTrace() I want to get the value of board[toX][toY] from class Board and how can I do this without any extends here ?

Upvotes: 1

Views: 52

Answers (3)

WIll
WIll

Reputation: 45

You can create a method to return the array and call it in the class you want the data.

public PlayingPiece getBoard()
{
     return board;
}

Upvotes: 0

BoDidely
BoDidely

Reputation: 504

Another solution (assuming you won't need multiple Board instances) is making board static.

public static PlayingPiece[][] board;

Then you can access it from your Pawn class using Board.board

Upvotes: 1

ruhungry
ruhungry

Reputation: 4706

I do not know if I understand your question but..

If you have got 2 classes A and B instead of using inheritance you can use aggregation. For example, you want to use some method from object B in class A you go with:

class A {
    private B b;
}

and then inside methods of class A you go with b.nameOfTheMethod()

Upvotes: 0

Related Questions