I am currently trying to make a chess game and have tried to implement an interface however I cannot access the interface.
public interface IChessPiece
{
bool CheckMove(int row, int col);
}
public class ChessPiece { ... }
public class Pawn : ChessPiece, IChessPiece
{
public bool CheckMove(int row, int col) { ... }
}
public class ChessPieces { public List<ChessPieces> chessPieces; ... }
I cannot seem to access the CheckMove() method.
board.chessPieces.Find(x => <condition>).CheckMove(row, col);
You can implement ChessPiece as abstract class:
public interface IChessPiece {
bool CheckMove(int row, int col);
}
// Note "abstract"
public abstract class ChessPiece: IChessPiece {
...
// Note "abstract"
public abstract bool CheckMove(int row, int col);
}
// Pawn implements IChessPiece since it's derived form ChessPiece
public class Pawn: ChessPiece {
// Actual implementation
public override bool CheckMove(int row, int col) { ... }
}
Your class also requires to implement the IChessPiece interface and most likely make it abstract, since it should not be instanciated directly. Then you should change the List on the board to have IChessPiece type:
public class ChessPiece : IChessPiece { ... }
public class Pawn : ChessPiece, IChessPiece
{
public bool CheckMove(int row, int col) { ... }
}
public class ChessPieces { public List<IChessPieces> chessPieces; ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With