public async Task OnTestCountCards()
        {
            for (int i = _gridComputer.Items.Count - 1; i >= 0; i--)
            {
                CardView card = _gridPlayer.Items[i];
                card.Owner = Owner.Player;
                await _gridPlayer.MoveCardToTarget(card, _gridPlayedCards);

                await _gridPlayer.UpdateCardLayout(MoveCardOptions.MoveAllAtSameTime);

                card       = _gridComputer.Items[i];
                card.Owner = Owner.Computer;
                await _gridComputer.MoveCardToTarget(card, _gridPlayedCards);

                await card.SetOrientation(CardOrientation.FaceUp);

                await _gridComputer.UpdateCardLayout(MoveCardOptions.MoveAllAtSameTime);
            }
        }
        public async Task MoveCardToTarget(CardView card, CardContainer target, double animationDuration = Double.MaxValue, bool rotate = false, bool transferCard = true)
        {
            if (this.Items.Contains(card) == false)
            {
                //Debug.WriteLine("Attempting to move Card {0} from {1} to {2} and it is not in {1}", card.CardName, this.FriendlyName, target.FriendlyName);
                return;
            }

            if (animationDuration == Double.MaxValue)
            {
                animationDuration = MainPage.AnimationSpeeds.Medium;
            }
            Point ptTo = SetupCardForMove(card, target, rotate, transferCard);
            await card.AnimateTo(ptTo, rotate, false, animationDuration);

            if (transferCard) // if we don't transfer the card, UpdateCardLayout() just moves it back... :)
            {
                await this.UpdateCardLayout();
            }
        }
        public void TransferCard(CardView card, CardContainer target)
        {
            card.PointerPressed -= this.Card_OnPointerPressed;


            if (target.Selectable)
            {
                card.PointerPressed       += target.Card_OnPointerPressed;
                card.DoubleTapped         += target.Card_DoubleTapped;
                card.CardSelectionChanged += target.OnCardSelectionChanged;
            }
            else
            {
                card.PointerPressed       -= target.Card_OnPointerPressed;
                card.DoubleTapped         -= target.Card_DoubleTapped;
                card.CardSelectionChanged -= OnCardSelectionChanged;
            }

            this.Items.Remove(card);
            target.Items.Add(card);
        }
        public async Task UpdateCardLayout(MoveCardOptions options = MoveCardOptions.MoveAllAtSameTime, double animationDuration = Double.MaxValue)
        {
            if (animationDuration == Double.MaxValue)
            {
                animationDuration = 250;
            }

            if (this.Items.Count == 0)
            {
                return;
            }


            List <Task <object> > taskList = new List <Task <object> >();
            int prevZIndex = Canvas.GetZIndex(this.Items[0]);

            for (int i = 0; i < this.Items.Count; i++)
            {
                CardView         card = this.Items[i];
                GeneralTransform gt   = this.TransformToVisual(card);
                Point            ptTo = gt.TransformPoint(this.GetCardTopLeft(i));
                card.AnimationPosition = ptTo;
                card.AnimateToTaskList(ptTo, false, animationDuration, taskList);
                if (_layout == CardLayout.PlayedOverlapped)
                {
                    Canvas.SetZIndex(card, prevZIndex++);
                }
            }

            if (options == MoveCardOptions.MoveAllAtSameTime)
            {
                await Task.WhenAll(taskList);
            }
            else
            {
                Task.WaitAll(taskList.ToArray());
            }
        }
        private Point SetupCardForMove(CardView card, CardContainer target, bool rotate, bool transferCard)
        {
            int targetCount = target.Items.Count;

            //
            //  if they are overlapped, make the ZIndex such that they are on top of each other
            if (target.CardLayout == CardLayout.PlayedOverlapped)
            {
                if (targetCount > 0)
                {
                    int zIndex = Canvas.GetZIndex(target.Items.Last());
                    Canvas.SetZIndex(card, zIndex + 1);
                }
            }

            //SetCardSize(card);

            //
            //  set up the animation values so we can do the animation
            GeneralTransform gt   = target.TransformToVisual(card);
            Point            ptTo = gt.TransformPoint(target.GetCardTopLeft(targetCount));

            card.AnimationPosition = ptTo;
            if (rotate)
            {
                card.AnimateRotation += 360;
            }

            //
            // if we are going to transferownership...
            if (transferCard)
            {
                TransferCard(card, target);
            }
            return(ptTo);
        }
        public void UpdateCardLayout(List <Task <object> > taskList, double animationDuration = Double.MaxValue, bool rotate = false)
        {
            if (animationDuration == Double.MaxValue)
            {
                animationDuration = 50;
            }


            if (this.Items.Count == 0)
            {
                return;
            }

            int prevZIndex = Canvas.GetZIndex(this.Items[0]);

            for (int i = 0; i < this.Items.Count; i++)
            {
                CardView         card = this.Items[i];
                GeneralTransform gt   = this.TransformToVisual(card);
                Point            ptTo = gt.TransformPoint(this.GetCardTopLeft(i));
                card.AnimationPosition = ptTo;
                card.AnimateToTaskList(ptTo, false, animationDuration, taskList);
                if (rotate)
                {
                    card.AnimateRotation += 360;
                }

                if (_layout == CardLayout.PlayedOverlapped)
                {
                    Canvas.SetZIndex(card, prevZIndex++);
                }
            }


            this.UpdateLayout();
        }
        public Task <Point> DragAsync(CardContainer container, CardView card, PointerRoutedEventArgs origE, DragList dragList, IDragAndDropProgress progress = null)
        {
            TaskCompletionSource <Point> taskCompletionSource = new TaskCompletionSource <Point>();
            UIElement mousePositionWindow = Window.Current.Content;
            Point     pointMouseDown      = origE.GetCurrentPoint(mousePositionWindow).Position;

            card.PushCard(true);
            bool dragging = false;

            if (dragList.Contains(card) == false)
            {
                dragList.Insert(0, card); // card you clicked is always the first one
            }
            PointerEventHandler pointerMovedHandler    = null;
            PointerEventHandler pointerReleasedHandler = null;

            pointerMovedHandler = (Object s, PointerRoutedEventArgs e) =>
            {
                Point pt    = e.GetCurrentPoint(mousePositionWindow).Position;
                Point delta = new Point();
                delta.X = pt.X - pointMouseDown.X;
                delta.Y = pt.Y - pointMouseDown.Y;

                CardView localCard    = (CardView)s;
                bool     reorderCards = false;
                //
                //  fixup our lists
                foreach (var c in this.SelectedCards)
                {
                    if (dragList.Contains(c) == false)
                    {
                        dragList.Add(c);
                    }
                }

                if (dragList.Contains(localCard) == false)
                {
                    dragList.Add(localCard);
                }

                if (dragList.Count > _maxSelected)
                {
                    CardView c = this.FirstSelectedCard;
                    c.Selected = false;
                    dragList.Remove(c);
                    c.UpdateLayout();
                }



                if (Math.Abs(delta.X - MOUSE_MOVE_SENSITIVITY) > 0 || Math.Abs(delta.Y - MOUSE_MOVE_SENSITIVITY) > 0)
                {
                    dragging = true;
                }

                //
                //  check to see if we have moved out of the container in the Y direction
                if (container.HitTest(pt))
                {
                    reorderCards = true;
                }

                if (dragList.Count > 1)
                {
                    reorderCards = false;
                    CardView otherCard = dragList[0];
                    double   cardWidth = card.CardWidth;
                    if (card.Index == otherCard.Index)
                    {
                        otherCard = dragList[1];
                    }

                    //
                    //  this moves the card to make space for reordering
                    int left = (int)(card.AnimationPosition.X - otherCard.AnimationPosition.X);

                    if (left > cardWidth)
                    {
                        otherCard.AnimateToReletiveAsync(new Point(left - cardWidth, 0), 0);
                        return;
                    }
                    else if (left < -card.CardWidth)
                    {
                        otherCard.AnimateToReletiveAsync(new Point(left + cardWidth, 0), 0);
                        return;
                    }
                }

                if (progress != null)
                {
                    progress.Report(pt);
                }

                foreach (CardView c in dragList)
                {
                    c.AnimateToReletiveAsync(delta);
                }

                if (reorderCards)
                {
                    int indexOfDraggedCard = container.Items.IndexOf(card);

                    if (delta.X > 0)
                    {
                        if (indexOfDraggedCard < container.Items.Count - 1)
                        {
                            CardView cardToMove = container.Items[indexOfDraggedCard + 1];
                            if (card.AnimationPosition.X + card.CardWidth * 0.5 > cardToMove.AnimationPosition.X)
                            {
                                cardToMove.AnimateToReletiveAsync(new Point(-card.CardWidth, 0), MainPage.AnimationSpeeds.VeryFast);
                                container.Items.Remove(card);
                                container.Items.Insert(container.Items.IndexOf(cardToMove) + 1, card);
                            }
                        }
                    }
                    else //moving left
                    {
                        if (indexOfDraggedCard > 0)
                        {
                            CardView cardToMove = container.Items[indexOfDraggedCard - 1];
                            if (card.AnimationPosition.X - card.CardWidth * 0.5 < cardToMove.AnimationPosition.X)
                            {
                                cardToMove.AnimateToReletiveAsync(new Point(card.CardWidth, 0), MainPage.AnimationSpeeds.VeryFast);
                                container.Items.Remove(card);
                                container.Items.Insert(container.Items.IndexOf(cardToMove), card);
                            }
                        }
                    }
                }


                pointMouseDown = pt;
            };

            pointerReleasedHandler = (Object s, PointerRoutedEventArgs e) =>
            {
                CardView localCard = (CardView)s;
                localCard.PointerMoved    -= pointerMovedHandler;
                localCard.PointerReleased -= pointerReleasedHandler;
                localCard.ReleasePointerCapture(origE.Pointer);
                localCard.PushCard(false);
                if (!dragging)
                {
                    ToggleSelectCard(card);
                    dragList.Clear();
                }



                Point exitPoint = e.GetCurrentPoint(mousePositionWindow).Position;

                if (progress != null)
                {
                    progress.PointerUp(exitPoint);
                }

                //if (container.HitTest(exitPoint) == true) // you landed where you started
                //    container.UpdateCardLayoutAsync(500, false);

                //
                //  returns the point that the mouse was released.  the _selectedCards list
                //  will have the cards that were selected.  if dragging occurred, the card(s)
                //  will be in the _draggingList
                taskCompletionSource.SetResult(exitPoint);
            };

            card.CapturePointer(origE.Pointer);
            card.PointerMoved    += pointerMovedHandler;
            card.PointerReleased += pointerReleasedHandler;
            return(taskCompletionSource.Task);
        }
Exemple #8
0
        public CardView GetCardByIndex(int index)
        {
            CardView card = _cards[index];

            return(card);
        }
        public async Task <Dictionary <string, string> > Load(string s)
        {
            if (s == "")
            {
                return(null);
            }

            Dictionary <string, string> sections = StaticHelpers.GetSections(s);

            if (_game.Deserialize(sections, _view.Deck) == false)
            {
                return(null);
            }

            Dictionary <string, string> game = StaticHelpers.DeserializeDictionary(sections["State"]);

            if (sections == null)
            {
                return(null);
            }

            if (game["Version"] != SERIALIZATION_VERSION)
            {
                return(null);
            }

            _gameState               = (GameState)Enum.Parse(typeof(GameState), game["GameState"]);
            _dealer                  = (PlayerType)Enum.Parse(typeof(PlayerType), game["Dealer"]);
            _totalCardsDropped       = Convert.ToInt32(game["TotalCardsDropped"]);
            _nPlayerCountingPoint    = Convert.ToInt32(game["PlayerCountingPoints"]);
            _nPlayerPointsThisTurn   = Convert.ToInt32(game["PlayerPointsThisTurn"]);
            _nComputerCountingPoint  = Convert.ToInt32(game["ComputerCountingPoints"]);
            _nComputerPointsThisTurn = Convert.ToInt32(game["ComputerPointsThisTurn"]);
            _nCribPointsThisTurn     = Convert.ToInt32(game["CribPointsThisTurn"]);
            int playerScore   = Convert.ToInt32(game["PlayerScore"]);
            int computerScore = Convert.ToInt32(game["ComputerScore"]);


            _hfs      = _game.GetHfs();
            _view.Hfs = _hfs;

            Dictionary <string, string> grids = StaticHelpers.DeserializeDictionary(sections["Grids"]);

            List <CardView> PlayerCards   = StaticHelpers.DeserializeToList(grids["Player"], _view.Deck);
            List <CardView> ComputerCards = StaticHelpers.DeserializeToList(grids["Computer"], _view.Deck);
            List <CardView> cribCards     = StaticHelpers.DeserializeToList(grids["Crib"], _view.Deck);



            CardView        SharedCard  = StaticHelpers.CardFromString(grids["SharedCard"], _view.Deck);
            List <CardView> playedCards = StaticHelpers.DeserializeToList(grids["Played"], _view.Deck);

            List <Task <object> > taskList = new List <Task <object> >();

            _view.MoveCards(taskList, playedCards, _view.GridPlayedCards);
            _view.MoveCards(taskList, cribCards, _view.GridCrib);
            _view.MoveCards(taskList, PlayerCards, _view.GridPlayer);
            _view.MoveCards(taskList, ComputerCards, _view.GridComputer);

            if (_gameState == GameState.PlayerGiveToCrib)
            {
                _crib.AddRange(playedCards);
            }

            await Task.WhenAll(taskList);

            SharedCard.BoostZindex(ZIndexBoost.SmallBoost);

            _game.UpdateScoreDirect(PlayerType.Player, ScoreType.Saved, playerScore);     // give the points to the player
            _game.UpdateScoreDirect(PlayerType.Computer, ScoreType.Saved, computerScore); // give the points to the player

            await UiState.AddScore(PlayerType.Player, playerScore);

            await UiState.AddScore(PlayerType.Computer, computerScore);

            _view.CountControl.Count = _game.CurrentCount;

            await FixUpUiState();

            string scoreHistory = "";

            if (sections.TryGetValue("Score History", out scoreHistory))
            {
                await _view.Load(scoreHistory);
            }

            if (_gameState == GameState.PlayerScoreHand)
            {
                //
                //  if we saved in the state where the player is supposed to score the hard, we need to drive the state machine
                //  through the completion of the GameState.ScoreHand states
                if (_dealer == PlayerType.Player)
                {
                    await SetState(GameState.PlayerScoreHand);
                    await SetState(GameState.ShowCrib);
                    await SetState(GameState.PlayerScoreCrib);

                    _gameState = GameState.EndOfHand;
                }
                else
                {
                    _gameState = GameState.ScoreHands;
                }
            }
            else if (_gameState == GameState.PlayerScoreCrib)
            {
                await SetState(GameState.PlayerScoreCrib);

                _gameState = GameState.EndOfHand;
            }



            SetStateAsync(_gameState);


            return(sections);
        }
        public async Task OnCountCard(CardView card, PlayerType currentPlayer, CountingData countingData)
        {
            string message = "";

            if (countingData.Score > 0)
            {
                message = String.Format("{0} Scored Points\n\n{1}", currentPlayer == PlayerType.Player ? "You" : "The Computer", countingData.ScoreStory.Format());
                if (currentPlayer == PlayerType.Player)
                {
                    _pointsPlayerCountedThisTurn++;
                }
                else
                {
                    _pointsComputerCountedThisTurn++;
                }
            }
            if (currentPlayer == PlayerType.Computer)
            {
                //
                //  if it is the player, the card gets moved there via drag and drop
                await GridComputer.MoveCardToTarget(card, GridPlayedCards, MainPage.AnimationSpeeds.Medium);
            }


            if (card.Orientation == CardOrientation.FaceDown)
            {
                await card.SetOrientation(CardOrientation.FaceUp);
            }


            if (countingData.Score > 0)
            {
                await AddToScoreHistory(GridPlayedCards.Items, countingData.ScoreStory, currentPlayer);
            }


            UpdateCount(countingData.CurrentCount);


            if (countingData.ResetCount) // the card we just dropped hit 31 or a Go
            {
                UpdateCount(countingData.CountBeforeReset);
                await OnCountResetUpdateUi();
            }


            if (countingData.NextPlayer == PlayerType.Player)
            {
                GridPlayer.MaxSelected = 1;
                message += "Your Turn.\nDrag and drop one card into the middle window";
            }
            else
            {
                GridPlayer.MaxSelected = 0;
            }

            if (message != "")
            {
                ShowHintWindowAsync(true, false, message);
            }
        }