using Chess; using System; using System.Collections.Generic; using System.Drawing; using System.Windows; namespace ChessPanel { public class Bishop : Piece { public Bishop(PieceColor color, Vector position, Grid fields) : base(position, fields) { type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name); this.color = color; this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}"); } private static bool AddFieldToTargetFields(Grid fields, Field field, int x, int y, Dictionary targetFields, PieceColor color) { if (fields[field.location, x, y].IsEmpty) { targetFields.Add(fields[field.location, x, y], PieceTeam.Ally); } else { if (fields[field.location, x, y].CurrentPiece?.color != color) targetFields.Add(fields[field.location, x, y], PieceTeam.Enemy); return true; } return false; } internal override Dictionary GetTargetFields(Grid fields, Field field, PieceColor color) { return StaticGetTargetFields(fields, field, color); } internal static Dictionary StaticGetTargetFields(Grid fields, Field field, PieceColor color) { Dictionary targetFields = new Dictionary(); #region nach RechtsOben for (int xy = 1; xy <= 7; xy++) { if (CheckOutOfBounds(field, xy, xy) || AddFieldToTargetFields(fields, field, xy, xy, targetFields, color)) break; } #endregion // LinksOben for (int xy = 1; xy <= 7; xy++) { if (CheckOutOfBounds(field, -xy, xy) || AddFieldToTargetFields(fields, field, -xy, xy, targetFields, color)) break; } // RechtsUnten for (int xy = 1; xy <= 7; xy++) { if (CheckOutOfBounds(field, xy, -xy) || AddFieldToTargetFields(fields, field, xy, -xy, targetFields, color)) break; } // LinksUnten for (int xy = 1; xy <= 7; xy++) { if (CheckOutOfBounds(field, -xy, -xy) || AddFieldToTargetFields(fields, field, -xy, -xy, targetFields, color)) break; } return targetFields; } private static bool CheckOutOfBounds(Field field, int x, int y) { return field.location.X + x > 7 || (int)field.location.Y + y > 7 || field.location.X + x < 0 || (int)field.location.Y + y < 0; } /// /// /// /// Global Field Grid /// Field that should be checked for endager /// Piece that should be moved /// Target of the moved piece /// internal override bool EndageresFieldWithVirtualMove(Grid fields, Field possibleEndageredField, Piece movePiece, Field targetPos) { return StaticEndageresFieldWithVirtualMove(fields, this.field, possibleEndageredField, movePiece, targetPos, color); } internal static bool StaticEndageresFieldWithVirtualMove(Grid fields, Field thisField, Field possibleEndageredField, Piece movePiece, Field targetPos, PieceColor color) { var skipField = movePiece.field; for (int xy = 1; xy <= 7; xy++) // nach RechtsOben { if (thisField.location.X + xy > 7 || thisField.location.Y + xy > 7) break; var possibleTargetField = fields.GetFieldWithOffset(thisField, xy, xy); if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt)) return true; if (breakIt) break; } for (int xy = 1; xy <= 7; xy++) { if (thisField.location.X - xy < 0 || thisField.location.Y + xy > 7) // LinksOben break; var possibleTargetField = fields.GetFieldWithOffset(thisField, -xy, xy); if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt)) return true; if (breakIt) break; } for (int xy = 1; xy <= 7; xy++) { if (thisField.location.X + xy > 7 || thisField.location.Y - xy < 0) // RechtsUnten break; var possibleTargetField = fields.GetFieldWithOffset(thisField, xy, -xy); if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt)) return true; if (breakIt) break; } for (int xy = 1; xy <= 7; xy++) { if (thisField.location.X - xy < 0 || thisField.location.Y - xy < 0) // LinksUnten break; var possibleTargetField = fields.GetFieldWithOffset(thisField, -xy, -xy); if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt)) return true; if (breakIt) break; } return false; } } // FINALIZED }