/// <summary> /// Initializes a new instance of the <see cref="Piece"/> class. /// </summary> /// <param name="name"> /// The piece name e.g. bishop, queen. /// </param> /// <param name="player"> /// The player that owns the piece. /// </param> /// <param name="file"> /// Board file that the piece starts on. /// </param> /// <param name="rank"> /// Board rank that the piece starts on. /// </param> /// <param name="identifier"> /// Piece identifier. /// </param> public Piece(PieceNames name, Player player, int file, int rank, PieceIdentifierCodes identifier) { this.LastMoveTurnNo = -1; this.IsInPlay = true; Square square = Board.GetSquare(file, rank); this.Player = player; this.StartLocation = this.Square = square; square.Piece = this; this.IdentifierCode = identifier; switch (name) { case PieceNames.Pawn: this.Top = new PiecePawn(this); break; case PieceNames.Bishop: this.Top = new PieceBishop(this); break; case PieceNames.Knight: this.Top = new PieceKnight(this); break; case PieceNames.Rook: this.Top = new PieceRook(this); break; case PieceNames.Queen: this.Top = new PieceQueen(this); break; case PieceNames.King: this.Top = new PieceKing(this); break; } }
/// <summary> /// Promote a pawn (change its top to a queen or knight). /// </summary> /// <param name="name"> /// Name of piece to change the pawn into. /// </param> /// <exception cref="ApplicationException"> /// Indicates failure to promote the piece. /// </exception> public void Promote(PieceNames name) { if (this.HasBeenPromoted) { throw new ApplicationException("Piece has already been promoted!"); } if (this.Name != PieceNames.Pawn) { throw new ApplicationException("Attempt to promote piece that is not a pawn"); } switch (name) { case PieceNames.Bishop: this.Top = new PieceBishop(this); break; case PieceNames.Knight: this.Top = new PieceKnight(this); break; case PieceNames.Rook: this.Top = new PieceRook(this); break; case PieceNames.Queen: this.Top = new PieceQueen(this); break; default: throw new ApplicationException("Can only promote pawn to either Bishop, Knight, Rook or Queen"); } this.Player.DecreasePawnCount(); this.Player.IncreaseMaterialCount(); this.HasBeenPromoted = true; }
static public bool DoesLeaperPieceTypeAttackSquare(Square square, Player player, PieceNames pieceName, int[] vector, out Piece attackingPiece) { Piece piece; attackingPiece = null; for (int i = 0; i < vector.Length; i++) { piece = Board.GetPiece(square.Ordinal + vector[i]); if (piece != null && piece.Name == pieceName && piece.Player.Colour == player.Colour) { attackingPiece = piece; return(true); } } return(false); }
public static bool DoesLeaperPieceTypeAttackSquare(Square square, Player player, PieceNames pieceName, int[] vector, out Piece attackingPiece) { Piece piece; attackingPiece = null; for (int i = 0; i < vector.Length; i++) { piece = Board.GetPiece(square.Ordinal + vector[i]); if (piece != null && piece.Name == pieceName && piece.Player.Colour == player.Colour) { attackingPiece = piece; return true; } } return false; }