Esempio n. 1
0
        //TODO: Factory?
        private Move CreateMoveFromRayMove(Piece piece, RayMove rayMove, int x)
        {
            if (rayMove.IsShortCastle)
            {
                return(new ShortCastle(piece, Board.GetCell(rayMove.Origin + x * rayMove.Vector)));
            }

            if (rayMove.IsLongCastle)
            {
                return(new LongCastle(piece, Board.GetCell(rayMove.Origin + x * rayMove.Vector)));
            }

            if (rayMove.IsPassant)
            {
                return(new Passant(piece, Board.GetCell(rayMove.Origin + x * rayMove.Vector)));
            }

            if (piece is Pawn pawn)
            {
                int targetY = (rayMove.Origin + x * rayMove.Vector).Y;
                if (pawn.Color == PlayerColor.White && targetY == Board.Size - 1 ||
                    pawn.Color == PlayerColor.Black && targetY == 0)
                {
                    return(new Promotion(piece, Board.GetCell(rayMove.Origin + x * rayMove.Vector)));
                }
            }

            return(new Move(piece, Board.GetCell(rayMove.Origin + x * rayMove.Vector)));
        }
Esempio n. 2
0
        private void CalculateAttackFromRayMove(RayMove rayMove)
        {
            Cell originCell = Board.GetCell(rayMove.Origin);

            for (int x = 1; x <= rayMove.Range; x++)
            {
                Cell targetCell = Board.GetCell(rayMove.Origin + x * rayMove.Vector);

                targetCell?.SetUnderAttack(originCell.CurrentPiece.Color);

                if (targetCell is null || !targetCell.IsEmpty && !(targetCell.CurrentPiece is King))
                {
                    return;
                }
            }
        }
Esempio n. 3
0
        private void CalculatePinsFromRayMove(RayMove rayMove)
        {
            Piece enemyPieceInPath = null;
            Cell  originCell       = Board.GetCell(rayMove.Origin);

            for (int x = 1; x <= rayMove.Range; x++)
            {
                Cell targetCell = Board.GetCell(rayMove.Origin + x * rayMove.Vector);

                if (targetCell == null)
                {
                    return;
                }

                if (targetCell.IsEmpty)
                {
                    continue;
                }

                if (targetCell.CurrentPiece.Color == originCell.CurrentPiece.Color)
                {
                    return;
                }

                if (targetCell.CurrentPiece is King)
                {
                    if (enemyPieceInPath != null)
                    {
                        enemyPieceInPath.Pin = rayMove;
                    }
                    else
                    {
                        targetCell.CurrentPiece.Pin = new RayMove(rayMove.Origin, rayMove.Vector, x);
                    }

                    return;
                }

                if (enemyPieceInPath != null)
                {
                    return;
                }

                enemyPieceInPath = targetCell.CurrentPiece;
            }
        }
Esempio n. 4
0
        private void CalculateMoveFromRayMove(RayMove rayMove)
        {
            Piece piece = Board.GetCell(rayMove.Origin).CurrentPiece;

            for (int x = 1; x <= rayMove.Range; x++)
            {
                Move move = CreateMoveFromRayMove(piece, rayMove, x);

                if (move.IsLegal(Board))
                {
                    piece.Moves.Add(move);
                }

                if (move.IsLastInPath)
                {
                    break;
                }
            }
        }