/// <summary> /// Is a move legal? /// </summary> /// <returns></returns> private bool IsMoveLegal(IPiece piece, IMove move, IBoardState state) { piece.GenerateMoves(state); bool canPieceMove = piece.CanMoveTo(move.EndingPosition); bool doesMoveLeaveKingInCheck = DoesPotentialMoveLeaveKingInCheck(move); return(canPieceMove && !doesMoveLeaveKingInCheck); }
/// <summary> /// Returns whether the piece has any possible legal moves. /// </summary> /// <param name="piece"></param> /// <returns></returns> private bool DoesPieceHaveLegalMove(IPiece piece) { piece.GenerateMoves(GameBoard.State); IMove move = ModelLocator.Move; move.StartingPosition = piece.Location; IBoardState moveSet = ModelLocator.BoardState; moveSet.Add(piece.MoveSet); foreach (ChessPosition position in moveSet) { move.EndingPosition = position; if (IsMoveLegal(piece, move, GameBoard.State)) { return(true); } } if (!(piece is IKing)) { return(false); } IEnumerable <ChessPosition> moves = ModelLocator.CastlingHelper.GetCastleMovesForKing((IKing)piece); foreach (ChessPosition castleMove in moves) { move.EndingPosition = castleMove; if (IsCastleLegal(piece, move, GameBoard)) { return(true); } } return(false); }