'Method undefined for the Type Piece problem
I've been working on a project of coding chess in Java. I have an abstract class named Piece which has one abstract method and a couple of normal methods. I also have a class for each chess piece, and they all extend Piece.
There is a class the represents the board called Board as well. In this class, I have a method that checks if the piece at a given position belongs to the other player or not. In this method I call a method from the Piece class that gets the color called getColor(). when I call this method on a piece it says "The method getColor() is undefined for the type Piece" even though it is a method that is created inside the Piece class. I really don't know what to do to solve this issue.
Here's the Piece class code:
abstract class Piece {
private Color color;
/**
* @param color the color of the piece.
*/
public Piece(Color color) {
this.color = color;
}
/**
* This method gives a list of all legal moves a piece at a given position in a
* given board can do.
*
* @param position the position of the piece in the board.
* @param board the board in which the piece is in.
* @return a list of all legal moves.
*/
abstract List<Position> getPossibleMoves(Position position, Board board);
/**
* @return the color of the piece.
*/
public Color getColor() {
return color;
}
Here's the Board class code:
public class Board {
private Square[][] square;
/**
* Initialises an 8 by 8 squares 2d table.
*/
public Board() {
this.square = new Square[8][8];
for (int row = 0; row < square.length; row++) {
for (int column = 0; column < square.length; column++) {
square[row][column] = new Square<>(null);
}
}
}
/**
* This method sets the given piece in the given position in the board.
* Throw an exception if the position is not in the board.
*
* @param piece The piece to be set.
* @param position The position to set the piece in.
*/
public <Piece> void setPiece(Piece piece, Position position) {
if (piece instanceof Piece) {
if (contains(position)) {
square[position.getRow()][position.getColumn()].setPiece(piece);
} else {
throw new IllegalArgumentException("The given position isn't part of the board :" + position);
}
}
}
/**
* This method returns the piece that is in the given position in the board.
* Throws an exception if the position is not in the board.
*
* @param pos The position of the piece in the board.
* @return the piece.
*/
public <Piece> Piece getPiece(Position pos) {
Piece piece;
if (contains(pos)) {
piece = (Piece) square[pos.getRow()][pos.getColumn()].getPiece();
} else {
throw new IllegalArgumentException("The given position isn't part of the board :" + pos);
}
return piece;
}
/**
* This method determines whether or not the piece at the given position has the
* same or the opposite color as the given color.
* throws an exception if position is not in the board.
*
* @param pos the position of the piece to be checked.
* @param col the color to check if the piece is in the opposite color or not.
* @return true if the piece is in the opposite color, false otherwise.
*/
public <Piece> boolean containsOppositeColor(Position pos, Color col) {
boolean containsOpposite = false;
if (contains(pos)) {
if (!isFree(pos)) {
Piece piece = getPiece(pos);
containsOpposite = (col.opposite() == piece.getColor());
}
} else {
throw new IllegalArgumentException("The given position isn't part of the board :" + pos);
}
return containsOpposite;
}
The problem is at the line that says : containsOpposite = (col.opposite() == piece.getColor());
Solution 1:[1]
The problem is you define a generic type <Piece> in a lot of the methods in Board. This hides the actual type Piece and replaces it with an unknown type basically equivalent to Object, which doesn't have a getColor method. You need to remove all the occurrences of the generic type <Piece> in the methods of Board.
So, for example
public <Piece> Piece getPiece(Position pos)
should be
public Piece getPiece(Position pos)
And the same goes for the other methods:
public <Piece> void setPiece(Piece piece, Position position)
should be
public void setPiece(Piece piece, Position position)
and
public <Piece> boolean containsOppositeColor(Position pos, Color col)
should be
public boolean containsOppositeColor(Position pos, Color col)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
