예제 #1
0
파일: Piece.cs 프로젝트: toronacii/Chess
        protected virtual bool IsValidLinearMovement(Square source, Square target)
        {
            var allowedSquares = new List <Square>();
            var rowDelta       = target.Row - source.Row;
            var columnDelta    = target.Column - source.Column;

            var rowIncrement    = ((rowDelta < 0) ? -1 : (rowDelta > 0) ? 1 : 0);
            var columnIncrement = ((columnDelta < 0) ? -1 : (columnDelta > 0) ? 1 : 0);

            var nextPosition = source.Move(1, rowIncrement, columnIncrement);

            while (nextPosition != null && !nextPosition.Equals(target))
            {
                allowedSquares.Add(nextPosition);

                nextPosition = nextPosition.Move(1, rowIncrement, columnIncrement);
            }

            return(!allowedSquares.Any(s => s.Piece != null));
        }
예제 #2
0
파일: Piece.cs 프로젝트: hcesar/Chess
        protected IEnumerable<Square> MoveUntilObstruction(Square from, MoveDirection dir)
        {
            var target = from.Move(dir);
            bool last = false;

            var player = this.Board[from].Player;

            while (target.HasValue && (this.Board[target.Value] == null || this.Board[target.Value].Player != player) && !last)
            {
                yield return target.Value;
                last = this.Board[target.Value] != null;
                target = target.Move(dir);
            }
        }
예제 #3
0
파일: Board.cs 프로젝트: hcesar/Chess
        public IList<Piece> GetAttackers(Square square, PlayerColor attacker)
        {
            var ret = new List<Piece>();

            //Find adjacent king
            if (this[attacker].First(i => i is King).Square.IsAdjacent(square))
                ret.Add(this[attacker].First(i => i is King));

            //Find attacking Pawn
            foreach (var dir in Pawn.GetAttackingDirection(attacker).GetDiagonals())
            {
                var pawn = this[square.Move(dir)] as Pawn;
                if (pawn != null && pawn.Player == attacker)
                    ret.Add(pawn);
            }

            var opponents = this[attacker].Where(i => !(i is King));
            var moves = opponents.Where(i => !(i is King || i is Pawn)).SelectMany(o => o.GetValidMoves().Where(i => i.Target == square && i.CanCapture));

            ret.AddRange(moves.Select(i => i.Piece));
            return ret;
        }