I am making a chess game and have an error where the override method in the child class is not always overriding the parent class.
More specifically on temp2.setgetMoves(generation) but only on the second time it is used for a piece. The error occurs because I have used inheritance with a virtual method in the parent and an override method in the child classes. For example in the parent class the method is:
abstract class Piece
{
String pieceType;
int pieceVal;
Panel piecePanel;
public Piece(String type, int value, Panel image)
{
image.BackgroundImage = (Image)(Properties.Resources.ResourceManager.GetObject(type));
pieceType = type;
pieceVal = value;
piecePanel = image;
}
public abstract List<Panel> setgetMoves(BoardGen board);
public virtual List<Panel> getMoves()
{
return null;
}
public String getType()
{
return pieceType;
}
public void setPanel(Panel newPanel)
{
piecePanel = newPanel;
}
public Panel getPanel()
{
return piecePanel;
}
public int getValue()
{
return pieceVal;
}
}
and in the child class (as an example this is an uncomplete pawn class, it does not have taking pieces or the en passant or promotion yet but it is all I have gotten around to so far) the code is:
class Pawn : Piece
{
public List<Panel> possibleMoves;
public Pawn(string type, int value, Panel image) : base(type, value, image)
{
}
public override List<Panel> setgetMoves(BoardGen board)
{
possibleMoves = new List<Panel>();
foreach (Panel x in board.getPanels())
{
if (this.getType().Substring(0, 1).Equals("W"))
{
if (this.getPanel().Location.Y == 240 && ((x.Location.Y == this.getPanel().Location.Y - 40) || (x.Location.Y == this.getPanel().Location.Y - 80)) && (x.Location.X == this.getPanel().Location.X))
{
possibleMoves.Add(x);
}
else if((x.Location.Y == (this.getPanel().Location.Y - 40)) && (x.Location.X == this.getPanel().Location.X))
{
possibleMoves.Add(x);
}
}
else if (this.getType().Substring(0,1).Equals("B"))
{
if (this.getPanel().Location.Y == 40 && ((x.Location.Y == this.getPanel().Location.Y + 40) || (x.Location.Y == this.getPanel().Location.Y + 80)) && (x.Location.X == this.getPanel().Location.X))
{
possibleMoves.Add(x);
}
else if (x.Location.Y == this.getPanel().Location.Y + 40 && (x.Location.X == this.getPanel().Location.X))
{
possibleMoves.Add(x);
}
}
}
return possibleMoves;
}
public override List<Panel> getMoves()
{
return possibleMoves;
}
Any help with why it may not override the second time would be really helpful. Thank you.