0

I am making a checkers game and have the following classes. Abstract class piece:

public abstract class Piece {

protected Position position;
private CheckersColor color;

public Piece(CheckersColor color, Position position) {
    this.position = position;
    this.color = color;

}

with some more methods.

The sub class Checker:

public class Checker extends Piece {

public Checker(CheckersColor color, Position position) {
    super(color, position);
}

with also some more methods, and another sub class king:

public class King extends Piece {

public King(CheckersColor color, Position position) {
    super(color, position);
}

public King(Piece checker) {
    super(checker.getColor(), checker.position);
}

The checkers are stored in an array of type Piece

private Piece[] whiteCheckers;

and inserted by :

whiteCheckers[counter] = new Checker(Constants.COLOR_WHITE, tempPosition);

So far everything works, but when I try to change one of the checkers into a king I get an error:

Exception in thread "main" java.lang.ArrayStoreException: King

The code that causes the error:

whiteCheckers[i] = new King(piece);

I have tried a few different ways to store it, with casting, removing the checker and adding the king in the same spot and more with no luck.

That's really weird to me because the checkers were stored with no problem and king causes the error while they are both sub classes of Piece.

Thanks a lot!

EDIT:

public class Board {
private int size;
private Piece[] blackCheckers;
private Piece[] whiteCheckers;
private Piece[][] board;

public Board(int size) {
        ....(Some other code)

        for (int k = 0; k < whiteCheckers.length; k++) {
            board[whiteCheckers[k].position.getRow()][whiteCheckers[k].position.getColumn()] = whiteCheckers[k];
            board[blackCheckers[k].position.getRow()][blackCheckers[k].position.getColumn()] = blackCheckers[k];

        }

    } else {
        throw new RuntimeException("The size is either smaller than 4 or is not even");
    }
}
    ....(Some more methods)

public void verifyCrown(Piece piece) {
    if ((piece.getColor().equals(Constants.COLOR_WHITE) && piece.position.getRow() == 0)
            || piece.getColor().equals(Constants.COLOR_BLACK) && piece.position.getRow() == size - 1) {

        if (piece.getColor().equals(Constants.COLOR_WHITE)) {
            for (int i = 0; i < whiteCheckers.length; i++) {
                if (whiteCheckers[i].equals(piece)) {
                    whiteCheckers[i] = new King(piece);

                }
            }
        }

        if (piece.getColor().equals(Constants.COLOR_BLACK)) {
            for (int i = 0; i < blackCheckers.length; i++) {
                if (blackCheckers[i].equals(piece)) {
                    blackCheckers[i] = new King(piece);

                }
            }
        }
    ....
    }
}

}

5
  • arrays are weird in java, i suggest you to use List<Piece> Commented Jan 5, 2017 at 21:18
  • by the way, show a code, where you assign a value to whiteCheckers array Commented Jan 5, 2017 at 21:20
  • Please show where you instantiate the array of pieces. Commented Jan 5, 2017 at 21:21
  • Thanks for answering! It is an assignment so we have to use arrays, I have added some code I hope it is easier to understand now. Commented Jan 5, 2017 at 21:36
  • Please provide a minimal reproducible example. Commented Jan 5, 2017 at 21:54

1 Answer 1

1

Can you please show us how the array is initialized? The cause could be that you initialize the array as follows:

Piece[] whiteCheckers = new Checker[n];

instead of

Piece[] whiteCheckers = new Piece[n];

which would not allow you to add objects of type King to it.

But again without seeing how the array was initialized, this may not be the case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot!!! That was the mistake, I was so sure that I made it piece so I didn't even check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.