Exemplo n.º 1
0
        internal override List <PiecePosition> GetPossiblePiecePositions(PiecePosition position, Board board)
        {
            var positions = GetPossiblePiecePositionsWithoutRoque(position, board);

            if (CanBigRoque(board))
            {
                positions.Add(new PiecePosition('A', _initialeLine));
            }
            if (CanSmallRoque(board))
            {
                positions.Add(new PiecePosition('H', _initialeLine));
            }
            return(positions);
        }
Exemplo n.º 2
0
 internal List <PiecePosition> GetPossiblePiecePositionsWithoutRoque(PiecePosition position, Board board)
 {
     return(new List <PiecePosition>
     {
         new PiecePosition((char)(position.Column - 1), position.Line - 1),
         new PiecePosition((char)(position.Column - 1), position.Line),
         new PiecePosition((char)(position.Column - 1), position.Line + 1),
         new PiecePosition(position.Column, position.Line + 1),
         new PiecePosition(position.Column, position.Line - 1),
         new PiecePosition((char)(position.Column + 1), position.Line - 1),
         new PiecePosition((char)(position.Column + 1), position.Line),
         new PiecePosition((char)(position.Column + 1), position.Line + 1)
     }
            .Where(x => IsInBoard(x) && !IsOccupiedBySameColorPiece(board, x) && !board.IsPositionThreatened(x, Color))
            .ToList());
 }
Exemplo n.º 3
0
        private bool Roque(PiecePosition fromPosition, PiecePosition toPosition)
        {
            var pieceToMove        = GetPiece(fromPosition) as King;
            var pieceAtDestination = GetPiece(toPosition) as Rook;

            if (pieceToMove == null || pieceAtDestination == null)
            {
                return(false);
            }
            var king = GetPiece(fromPosition);
            var rook = GetPiece(toPosition);

            SetPiece(toPosition, king);
            SetPiece(fromPosition, rook);
            return(true);
        }
Exemplo n.º 4
0
        private bool CanBigRoque(Board board)
        {
            if (HasMoved)
            {
                return(false);
            }
            var rook = board.GetPiece(new PiecePosition('A', _initialeLine));

            if (rook.HasMoved)
            {
                return(false);
            }
            for (char col = 'B'; col <= 'D'; col++)
            {
                var piecePosition = new PiecePosition(col, _initialeLine);
                if (board.GetPiece(piecePosition) != null || board.IsPositionThreatened(piecePosition, Color))
                {
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 5
0
        public bool IsPositionThreatened(PiecePosition position, PieceColor color)
        {
            PieceColor colorThreat;

            if (color == PieceColor.Black)
            {
                colorThreat = PieceColor.White;
            }
            else if (color == PieceColor.White)
            {
                colorThreat = PieceColor.Black;
            }
            else
            {
                throw new ArgumentException("Unknown piece color");
            }
            var allPositionsThreatened = new List <PiecePosition>();

            for (char col = 'A'; col <= 'H'; col++)
            {
                for (int line = 1; line <= 8; line++)
                {
                    var piecePosition = new PiecePosition(col, line);
                    var piece         = GetPiece(piecePosition);
                    if (piece?.Color == colorThreat)
                    {
                        var possiblePiecePositions = GetPossiblePiecePositions(piecePosition);
                        if (possiblePiecePositions != null)
                        {
                            allPositionsThreatened.AddRange(possiblePiecePositions);
                        }
                    }
                }
            }
            return(allPositionsThreatened.Contains(position));
        }
Exemplo n.º 6
0
 internal override List <PiecePosition> GetPossiblePiecePositions(PiecePosition position, Board board)
 {
     return(GetDiagonalMoves(position, board));
 }
Exemplo n.º 7
0
 internal override List <PiecePosition> GetPossiblePiecePositions(PiecePosition position, Board board)
 {
     return(GetForwardBackwardMoves(position, board));
 }
Exemplo n.º 8
0
 internal abstract List <PiecePosition> GetPossiblePiecePositions(PiecePosition position, Board board);
Exemplo n.º 9
0
        protected List <PiecePosition> GetForwardBackwardMoves(PiecePosition position, Board board)
        {
            var positions = new List <PiecePosition>();
            int line      = position.Line + 1;

            while (IsInBoard(position.Column, line))
            {
                var newPosition = new PiecePosition(position.Column, line);
                if (IsOccupiedBySameColorPiece(board, newPosition))
                {
                    break;
                }
                positions.Add(newPosition);
                if (IsOccupiedByOtherColorPiece(board, newPosition))
                {
                    break;
                }
                line++;
            }
            line = position.Line - 1;
            while (IsInBoard(position.Column, line))
            {
                var newPosition = new PiecePosition(position.Column, line);
                if (IsOccupiedBySameColorPiece(board, newPosition))
                {
                    break;
                }
                positions.Add(newPosition);
                if (IsOccupiedByOtherColorPiece(board, newPosition))
                {
                    break;
                }
                line--;
            }

            char column = (char)(position.Column + 1);

            while (IsInBoard(column, position.Line))
            {
                var newPosition = new PiecePosition(column, position.Line);
                if (IsOccupiedBySameColorPiece(board, newPosition))
                {
                    break;
                }
                positions.Add(newPosition);
                if (IsOccupiedByOtherColorPiece(board, newPosition))
                {
                    break;
                }
                column++;
            }
            column = (char)(position.Column - 1);
            while (IsInBoard(column, position.Line))
            {
                var newPosition = new PiecePosition(column, position.Line);
                if (IsOccupiedBySameColorPiece(board, newPosition))
                {
                    break;
                }
                positions.Add(newPosition);
                if (IsOccupiedByOtherColorPiece(board, newPosition))
                {
                    break;
                }
                column--;
            }
            return(positions);
        }
Exemplo n.º 10
0
        protected bool IsOccupiedByOtherColorPiece(Board board, PiecePosition position)
        {
            var newPiece = board.GetPiece(position);

            return(newPiece != null && newPiece?.Color != Color);
        }
Exemplo n.º 11
0
        protected bool IsOccupiedBySameColorPiece(Board board, PiecePosition position)
        {
            var newPiece = board.GetPiece(position);

            return(newPiece?.Color == Color);
        }
Exemplo n.º 12
0
 protected bool IsInBoard(PiecePosition position)
 {
     return(IsInBoard(position.Column, position.Line));
 }
Exemplo n.º 13
0
 private void SetPiece(PiecePosition position, Piece piece)
 {
     _pieces[position.Column - 'A', position.Line - 1] = piece;
 }
Exemplo n.º 14
0
        public List <PiecePosition> GetPossiblePiecePositions(PiecePosition position)
        {
            var piece = GetPiece(position);

            return(piece?.GetPossiblePiecePositions(position, this));
        }
Exemplo n.º 15
0
 private void RemovePiece(PiecePosition position)
 {
     _pieces[position.Column - 'A', position.Line - 1] = null;
 }