Пример #1
0
        public static MoveSummary CanRookMove(Board board, Square from, Square to)
        {
            //throw new NotImplementedException("CanRookMove");
            var piece       = board[from];
            var isSameColor = board[from].Color == board[to].Color;
            var isCapturing = board[to].IsNone() ?
                              false :
                              !isSameColor;
            var isMovePossible =
                !isSameColor &&
                Checker.CanMoveLineary(
                    MoveProperty.IsHorizontalLine(from, to) ||
                    MoveProperty.IsVerticalLine(from, to),
                    board, from, to);

            var isWhite = piece.Color.IsWhite();
            var startQueensideSquare = isWhite ?
                                       WhiteCastlingSquares.QueensideRookStartSquare :
                                       BlackCastlingSquares.QueensideRookStartSquare;

            var startKingsideSquare = isWhite ?
                                      WhiteCastlingSquares.KingsideRookStartSquare :
                                      BlackCastlingSquares.KingsideRookStartSquare;

            var isQueensideRookMoving =
                from == startQueensideSquare;
            var isKingsideRookMoving =
                from == startKingsideSquare;

            return(MoveSummaryBuilder.RookSummary(
                       isCapturing, isMovePossible,
                       isQueensideRookMoving, isKingsideRookMoving, to));
        }
Пример #2
0
        public static MoveSummary CanKnightMove(Board board, Square from, Square to)
        {
            var isSameColor = board[from].Color == board[to].Color;
            var isCapturing = board[to].IsNone() ?
                              false :
                              !isSameColor;
            var isMovePossible =
                !isSameColor &&
                MoveProperty.AbsDeltaX(from, to) *
                MoveProperty.AbsDeltaY(from, to) == 2;

            return(MoveSummaryBuilder.DefaultMoveSummary(isCapturing, isMovePossible, to));
        }
Пример #3
0
        private static bool CanCapture(
            Color pawnColor,
            Board board,
            Square from, Square to)
        {
            // white: b2 -> a3 || c3; from.y - to.y == 1; |from.x - to.x| == 1
            // black: b7 -> a6 || c6; from.y - to.y == -1; |from.x == to.x| == 1
            var step = GetPawnStep(pawnColor);

            return
                (!board[to].IsNone() &&
                 MoveProperty.AbsDeltaX(from, to) == 1 &&
                 MoveProperty.DeltaY(from, to) == step);
        }
Пример #4
0
        private static bool CanForward(
            Color pawnColor,
            Board board,
            Square from, Square to)
        {
            // white: a1 -> a2; from.y - to.y == 1; from.x == to.x
            // black: a2 -> a1; from.y - to.y == -1; from.x == to.x
            var step = GetPawnStep(pawnColor);

            return
                (MoveProperty.DeltaX(from, to) == 0 &&
                 MoveProperty.DeltaY(from, to) == step &&
                 board[to].IsNone());
        }
Пример #5
0
        public static MoveSummary CanBishopMove(Board board, Square from, Square to)
        {
            var isSameColor = board[from].Color == board[to].Color;
            var isCapturing = board[to].IsNone() ?
                              false :
                              !isSameColor;
            var isMovePossible =
                !isSameColor &&
                Checker.CanMoveLineary(
                    MoveProperty.IsDiagonalLine(from, to),
                    board, from, to);

            return(MoveSummaryBuilder.DefaultMoveSummary(isCapturing, isMovePossible, to));
        }
Пример #6
0
        public float PlayMove(string name, float time, Transform move_target, MoveProperty move_property)
        {
            AnimationState state = Animation.Play(true, name);

            if (state != null && state != Animation.DamageState)
            {
                PlaybackTime = time;
                if (Animation.CurrentMoveState != null || Animation.CurrentFxState != null)
                {
                    m_MoveTarget   = move_target;
                    m_MoveProperty = move_property;
                }
            }
            return(Animation.CurrentStateLength);
        }
Пример #7
0
        private static bool CanEnPassantCapture(
            ChessGame game,
            Color pawnColor,
            Square from, Square to)
        {
            if (game.EnPassantTargetSquare.IsNone())
            {
                return(false);
            }

            var step = GetPawnStep(pawnColor);

            return
                (to == game.EnPassantTargetSquare &&
                 MoveProperty.AbsDeltaX(from, to) == 1 &&
                 MoveProperty.DeltaY(from, to) == step);
        }
Пример #8
0
        private static bool CanPush(
            Color pawnColor,
            Board board,
            Square from, Square to)
        {
            // white: a2 -> a4; from.y - to.y == 2; from.x == to.x
            // black: a7 -> a5; from.y - to.y == -2; from.x == to.x
            var row  = GetPawnStartRow(pawnColor);
            var step = GetPawnStep(pawnColor);

            return
                (row == from.Y &&
                 board[to].IsNone() &&
                 board[new Square(from.X, from.Y + step)].IsNone() &&
                 MoveProperty.DeltaX(from, to) == 0 &&
                 MoveProperty.DeltaY(from, to) == 2 * step);
        }
Пример #9
0
        /// <summary>
        /// Called after the view is loaded.
        /// </summary>
        protected override void AfterLoad()
        {
            base.AfterLoad();
            if (IgnoreObject)
            {
                return;
            }

            // add unity event system triggers
            if (!BeginDragProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, BeginDrag, EventTriggerType.BeginDrag);
            }
            if (!CancelProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Cancel, EventTriggerType.Cancel);
            }
            if (!DeselectProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Deselect, EventTriggerType.Deselect);
            }
            if (!DragProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Drag, EventTriggerType.Drag);
            }
            if (!DropProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Drop, EventTriggerType.Drop);
            }
            if (!EndDragProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, EndDrag, EventTriggerType.EndDrag);
            }
            if (!InitializePotentialDragProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, InitializePotentialDrag, EventTriggerType.InitializePotentialDrag);
            }
            if (!MoveProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Move, EventTriggerType.Move);
            }
            if (!ClickProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Click, EventTriggerType.PointerClick);
            }
            if (!MouseDownProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, MouseDown, EventTriggerType.PointerDown);
            }
            if (!MouseEnterProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, MouseEnter, EventTriggerType.PointerEnter);
            }
            if (!MouseExitProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, MouseExit, EventTriggerType.PointerExit);
            }
            if (!MouseUpProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, MouseUp, EventTriggerType.PointerUp);
            }
            if (!ScrollProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Scroll, EventTriggerType.Scroll);
            }
            if (!SelectProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Select, EventTriggerType.Select);
            }
            if (!SubmitProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, Submit, EventTriggerType.Submit);
            }
            if (!UpdateSelectedProperty.IsUndefined(this))
            {
                GameObject.AddEventTrigger(this, UpdateSelected, EventTriggerType.UpdateSelected);
            }

            IsActiveChanged(false);
        }
Пример #10
0
        private static MoveSummary CanCastle(ChessGame game, Color kingColor, Board board, Square from, Square to)
        {
            var isHorizontalLine = MoveProperty.IsHorizontalLine(from, to);

            if (isHorizontalLine)
            {
                var isAnyPieceBetween = Checker.IsAnyPieceBetween(board, from, to);
                if (!isAnyPieceBetween)
                {
                    var isKingWhite  = kingColor.IsWhite();
                    var hasKingMoved =
                        isKingWhite ?
                        game.HasWhiteKingMoved :
                        game.HasBlackKingMoved;

                    if (hasKingMoved)
                    {
                        return
                            (MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare));
                    }

                    var kingStartSquare = GetKingStartSquare(isKingWhite);
                    if (kingStartSquare == from)
                    {
                        var    isQueensideDirection = IsQueensideDirection(isKingWhite, to);
                        var    isKingsideDirection  = IsKingsideDirection(isKingWhite, to);
                        Square rookStartSquare;
                        bool   hasRookMoved;
                        if (isQueensideDirection)
                        {
                            rookStartSquare = GetQueensideRookStartSquare(isKingWhite);
                            hasRookMoved    = isKingWhite ?
                                              game.HasWhiteQueensideRookMoved :
                                              game.HasBlackQueensideRookMoved;
                        }

                        else if (isKingsideDirection)
                        {
                            rookStartSquare = GetKingsideRookStartSquare(isKingWhite);
                            hasRookMoved    = isKingWhite ?
                                              game.HasWhiteKingsideRookMoved :
                                              game.HasBlackKingsideRookMoved;
                        }

                        else
                        {
                            return(MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare));
                        }

                        var piece = board[rookStartSquare];
                        if (piece.IsNone())
                        {
                            return
                                (MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare));
                        }

                        var isValid =
                            !hasRookMoved &&
                            piece.Color == kingColor &&
                            piece.Type == PieceType.Rook;
                        if (isValid)
                        {
                            return(MoveSummaryBuilder.
                                   KingSummary(false, true, isQueensideDirection, isKingsideDirection, Square.NoneSquare));
                        }
                    }
                }
            }

            return(MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare));
        }
Пример #11
0
 private static bool CanDefaultMove(Square from, Square to, bool isSameColor) =>
 !isSameColor &&
 MoveProperty.AbsDeltaX(from, to) <= 1 &&
 MoveProperty.AbsDeltaY(from, to) <= 1;