public Coordinate Calculate(CandyMoves move)
        {
            var otherCandyPosition = this;

            switch (move)
            {
            case CandyMoves.Left:
                otherCandyPosition.X--;
                break;

            case CandyMoves.Right:
                otherCandyPosition.X++;
                break;

            case CandyMoves.Top:
                otherCandyPosition.Y--;
                break;

            case CandyMoves.Bottom:
                otherCandyPosition.Y++;
                break;

            default:
                throw new ArgumentOutOfRangeException("The indicated candy move is not valid.");
            }

            return(otherCandyPosition);
        }
        public BoardOperation[] Move(Coordinate candyPosition, CandyMoves move)
        {
            var result = new List <BoardOperation>();

            if (this.UpdateCandiesPosition(candyPosition, move))
            {
                IEnumerable <BoardOperation> operations;
                while ((operations = this.GetCurrentOperations()).Any())
                {
                    result.AddRange(operations);
                    this.ExecuteOperation(operations);
                }

                if (result.Count == 0)
                {
                    this.UpdateCandiesPosition(candyPosition, move);
                }
                else if (this.ShuffleIfNecessary())
                {
                    result.Add(new BoardOperation {
                        Type = OperationTypes.Shuffle
                    });
                }
            }

            return(result.ToArray());
        }
        private void CandyTouch_OnMoveOperation(object sender, CandyMoves move)
        {
            var candyTouch      = (CandyTouchComponent)sender;
            var candyAttributes = candyTouch.Owner.FindComponent <CandyAttributesComponent>();
            var coordinate      = candyAttributes.Coordinate;

            var boardOperations = this.gameLogic.Move(coordinate, move);

            if (boardOperations.Any())
            {
                var destCoord          = coordinate.Calculate(move);
                var destCandyAttribute = this.FindCandyAttributes(destCoord);
                destCandyAttribute.Coordinate = coordinate;
                candyAttributes.Coordinate    = destCoord;
                destCandyAttribute.Animator.RefreshPositionAnimation();

                CustomServices.AudioPlayer.PlaySound(Services.Audio.Sounds.ValidMovement);
                this.OnBoardOperation?.Invoke(this, boardOperations);
            }
            else
            {
                CustomServices.AudioPlayer.PlaySound(Services.Audio.Sounds.InvalidMovement);
                candyAttributes.Animator.RefreshPositionAnimation();
            }
        }
        private bool MoveHasOperations(Coordinate candyPosition, CandyMoves move)
        {
            if (this.UpdateCandiesPosition(candyPosition, move))
            {
                var operation = this.GetCurrentOperations();
                this.UpdateCandiesPosition(candyPosition, move);
                return(operation.Any());
            }

            return(false);
        }
 public BoardOperation[] Move(Coordinate candyPosition, CandyMoves move)
 {
     if (this.LeftTime > TimeSpan.Zero)
     {
         var result = this.currentBoard.Move(candyPosition, move);
         this.CurrentScore += (ulong)result.Sum(this.OperationScore);
         return(result);
     }
     else
     {
         return(new BoardOperation[0]);
     }
 }
        private bool UpdateCandiesPosition(Coordinate candyPosition, CandyMoves move)
        {
            if (this.IsValidCoordinate(candyPosition))
            {
                var otherCandyPosition = candyPosition.Calculate(move);

                if (this.IsValidCoordinate(otherCandyPosition))
                {
                    var otherCandy = this.currentStatus[otherCandyPosition.X][otherCandyPosition.Y];
                    this.currentStatus[otherCandyPosition.X][otherCandyPosition.Y] = this.currentStatus[candyPosition.X][candyPosition.Y];
                    this.currentStatus[candyPosition.X][candyPosition.Y]           = otherCandy;

                    return(true);
                }
            }

            return(false);
        }