using Chess; using System; using System.Collections.Generic; using System.Drawing; using System.Windows; namespace ChessPanel { public class Pawn : Piece { public Pawn(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()}"); } internal override bool EndageresFieldWithVirtualMove(Grid fields, Field field, Piece movePiece, Field targetPos) // Pawn does not care about virtually moved pieces, if field is in range it can attack { int direction = this.color == PieceColor.White ? -1 : 1; #region Attack for (int x = -1; x < 2; x += 2) { if (field.location.Y + (direction) > 7 || field.location.Y + (direction) < 0 || field.location.X + x > 7 || field.location.X + x < 0) continue; if (fields[field.location, x, direction] == field) return true; } #endregion return false; } internal override Dictionary GetTargetFields(Grid fields, Field field, PieceColor color) { MoveMode mode = MoveMode.Normal; Dictionary targetFields = new Dictionary(); int direction = this.color == PieceColor.White ? -1 : 1; #region Movement if (mode == MoveMode.Normal || mode == MoveMode.Movement) for (int y = 1; y < 1 + (HasMoved ? 1 : 2); y++) { if (field.location.Y + (direction * y) > 7 || field.location.Y + (direction * y) < 0) break; if (fields[field.location, 0, (direction * y)].IsEmpty) targetFields.Add(fields[field.location, 0, (direction * y)], PieceTeam.Ally); else break; } #endregion #region Attack if (mode == MoveMode.Normal || mode == MoveMode.Attack) { #region EnPassant if (field.location.Y == (this.color == PieceColor.White ? 3 : 4)) for (int x = -1; x < 2; x += 2) if (field.location.X + x <= 7 && field.location.X + x >= 0) if (fields[field.location, x, 0].CurrentPiece != null && fields[field.location, x, 0].CurrentPiece.type == PieceType.Pawn && fields[field.location, x, 0].CurrentPiece.color != this.color && fields[field.location, x, 0].CurrentPiece.EnPassantPossible) targetFields.Add(fields[field.location, x, direction], PieceTeam.Enemy); #endregion for (int x = -1; x < 2; x += 2) { if (field.location.Y + (direction) > 7 || field.location.Y + (direction) < 0 || field.location.X + x > 7 || field.location.X + x < 0) continue; if (fields[field.location, x, direction].CurrentPiece != null && fields[field.location, x, direction].CurrentPiece.color != this.color) { targetFields.Add(fields[field.location, x, direction], PieceTeam.Enemy); } } #endregion } return targetFields; } } }