private async Task OnDeal()
        {
            ResetCards();

            var(computerCards, playerCards, sharedCard) = Game.GetHands();

            var orig = _cgDeck.GetNextCardPosition(sharedCard[0]);

            await Deal(playerCards, computerCards, sharedCard, new List <CardCtrl> {
                computerCards[0], computerCards[2]
            },
                       PlayerType.Computer);

            var playerCribCards = new List <CardCtrl> {
                _cgPlayer.Cards[0], _cgPlayer.Cards[1]
            };
            var index = 2;

            foreach (var card in playerCribCards)
            {
                await CardGrid.AnimateMoveOneCard(_cgPlayer, _cgDiscarded, card, index ++, false,
                                                  MOVE_CARDS_ANIMATION_DURATION, 0);
            }

            CardGrid.TransferCards(_cgPlayer, _cgDiscarded, playerCribCards);
        }
Example #2
0
        private async Task AnimateSendCardsBackToOwner()
        {
            var tList = new List <Task>();
            var targetIndexForComputer = 0;
            var targetIndexForPlayer   = 0;
            var playerCards            = new List <CardCtrl>();
            var computerCards          = new List <CardCtrl>();

            foreach (var card in _cgDiscarded.Cards)
            {
                card.Opacity = 1.0;
                Task task;
                if (card.Owner == Owner.Computer)
                {
                    task = CardGrid.AnimateMoveOneCard(_cgDiscarded, _cgComputer, card, targetIndexForComputer, false,
                                                       MOVE_CARDS_ANIMATION_DURATION, targetIndexForComputer * MOVE_CARDS_ANIMATION_DURATION);
                    computerCards.Add(card);
                    targetIndexForComputer++;
                }
                else
                {
                    task = CardGrid.AnimateMoveOneCard(_cgDiscarded, _cgPlayer, card, targetIndexForPlayer, false,
                                                       MOVE_CARDS_ANIMATION_DURATION, MOVE_CARDS_ANIMATION_DURATION * targetIndexForPlayer);
                    targetIndexForPlayer++;
                    playerCards.Add(card);
                }

                tList.Add(task);
            }

            playerCards.Sort(CardCtrl.CompareCardsByRank); // sort from lowest to highest, ignoring suit

            await Task.WhenAll(tList);

            CardGrid.TransferCards(_cgDiscarded, _cgPlayer, playerCards);
            CardGrid.TransferCards(_cgDiscarded, _cgComputer, computerCards);
        }
        //
        //  if this is the computer, do the animation to move it to the shared grid and then flip the card.
        //
        //  if this is the player, update which cards can be played.
        //
        public async Task CountCard(PlayerType playerTurn, CardCtrl card, int newCount)
        {
            if (playerTurn == PlayerType.Computer)
            {
                var tList = new List <Task>();
                var task  = CardGrid.AnimateMoveOneCard(_cgComputer, _cgDiscarded, card, _cgDiscarded.Cards.Count, false,
                                                        MOVE_CARDS_ANIMATION_DURATION, 0);
                tList.Add(task);
                task = card.SetOrientationTask(CardOrientation.FaceUp, FlipAnimationDuration, 0);
                if (task != null)
                {
                    tList.Add(task);
                }

                CardGrid.TransferCards(_cgComputer, _cgDiscarded, new List <CardCtrl> {
                    card
                });

                await Task.WhenAll(tList);
            }


            _game.SetPlayableCards(); // both enables playable cards and disables non-playable ones
        }
        public async Task <PlayerType> ChooseDealer()
        {
            List <CardCtrl> cards       = null;
            Task            t           = null;
            var             taskList    = new List <Task>();
            const double    waitToReset = 0;

            while (true)
            {
                cards          = CardCtrl.GetCards(2, Owner.Uninitialized);
                cards[0].Owner = Owner.Player;
                cards[1].Owner = Owner.Computer;
                AddCardsToDeckVisually(cards);
                _cgDeck.SetCardPositionsNoAnimation();
                await Task.Delay(10);
                await DealOneCardEach();

                await Task.Delay(10);
                await Reset();

                ResetCards();
                if (cards[0].Card.CardOrdinal == cards[1].Card.CardOrdinal)
                {
                    AddMessage("Tie!  Try again.");
                    await Task.Delay(250);

                    continue;
                }

                break;
            }

            var playerType = PlayerType.Computer;

            if (cards[0].Card.CardOrdinal < cards[1].Card.CardOrdinal)
            {
                playerType = PlayerType.Player;
            }

            var user    = playerType == PlayerType.Player ? "You" : "The Computer";
            var message = string.Format($"{user} got low card and will deal.");

            AddMessage(message);

            return(playerType);

            //
            //  wow - local functions!
            async Task DealOneCardEach()
            {
                taskList.Clear();
                t = CardGrid.AnimateMoveOneCard(_cgDeck, _cgPlayer, cards[0], 0, true, MOVE_CARDS_ANIMATION_DURATION,
                                                0);
                taskList.Add(t);
                t = CardGrid.AnimateMoveOneCard(_cgDeck, _cgComputer, cards[1], 0, true, MOVE_CARDS_ANIMATION_DURATION,
                                                0);
                taskList.Add(t);
                t = cards[1].SetOrientationTask(CardOrientation.FaceUp, FlipAnimationDuration, 500);
                taskList.Add(t);
                t = cards[0].SetOrientationTask(CardOrientation.FaceUp, FlipAnimationDuration, 1500);
                taskList.Add(t);
                await Task.WhenAll(taskList);

                await Task.Delay(1000); // let the user see it for a second
            }

            async Task Reset()
            {
                taskList.Clear();
                t = cards[1].SetOrientationTask(CardOrientation.FaceDown, FlipAnimationDuration, waitToReset);
                taskList.Add(t);
                t = cards[0].SetOrientationTask(CardOrientation.FaceDown, FlipAnimationDuration, waitToReset);
                taskList.Add(t);
                t = CardGrid.AnimateMoveOneCard(_cgPlayer, _cgDeck, cards[0], 0, true, MOVE_CARDS_ANIMATION_DURATION,
                                                FlipAnimationDuration);
                taskList.Add(t);
                t = CardGrid.AnimateMoveOneCard(_cgComputer, _cgDeck, cards[1], 0, true, MOVE_CARDS_ANIMATION_DURATION,
                                                FlipAnimationDuration);
                taskList.Add(t);
                await Task.WhenAll(taskList);
            }
        }
Example #5
0
        /// <summary>
        ///     Deal does the following
        ///     1. moves cards from the deck to the computer and player
        ///     2. flips the players cards faceup
        ///     3. moves 2 cards from the computer to the discard grid
        ///     Assume we start with the cards in the deck
        ///     One problem we have is that we can't animate (X,Y) in one TimeLine -- so we have to compose them.
        /// </summary>
        public async Task AnimateDeal(List <CardCtrl> playerCards, List <CardCtrl> computerCards,
                                      List <CardCtrl> discardedComputerCards, PlayerType dealer)
        {
            playerCards.Sort(CardCtrl.CompareCardsByRank); // sort from lowest to highest, ignoring suit

            var taskList = new List <Task>();

            //
            //  assume dealer is player and then set it if it is not
            var dealersCards   = playerCards;
            var nonDealerCards = computerCards;


            if (dealer == PlayerType.Computer)
            {
                dealersCards   = computerCards;
                nonDealerCards = playerCards;
            }


            Task   t         = null;
            double beginTime = 0;

            var zIndex = 53; // this number is important -- the layout engine for cards sets Zindex too...

            for (var z = 0; z < nonDealerCards.Count; z++)
            {
                nonDealerCards[z].ZIndex = zIndex;
                dealersCards[z].ZIndex   = zIndex - 1;
                zIndex -= 2;
            }


            _cgDeck.Cards.Sort((c1, c2) => Canvas.GetZIndex(c2) - Canvas.GetZIndex(c1));

            double targetIndex = 0; // just being a bit silly here -- by incrementing by 0.5 and casting to an int, we can increate the target index by 1 after 2 iterations through the loop

            foreach (var card in _cgDeck.Cards)
            {
                switch (card.Owner)
                {
                case Owner.Player:
                    t = CardGrid.AnimateMoveOneCard(_cgDeck, _cgPlayer, card, (int)targetIndex, true,
                                                    MOVE_CARDS_ANIMATION_DURATION, beginTime);
                    targetIndex  += 0.5;
                    card.Location = CardView.Location.Player;
                    break;

                case Owner.Computer:
                    t = CardGrid.AnimateMoveOneCard(_cgDeck, _cgComputer, card, (int)targetIndex, true,
                                                    MOVE_CARDS_ANIMATION_DURATION, beginTime);
                    targetIndex  += 0.5;
                    card.Location = CardView.Location.Computer;
                    break;

                case Owner.Shared:
                    continue;

                default:
                    throw new InvalidOperationException("Card owner not set");
                }

                taskList.Add(t);
                beginTime += MOVE_CARDS_ANIMATION_DURATION;
            }


            //
            //  Now flip the players cards
            taskList.AddRange(CardGrid.SetCardsToOrientationTask(playerCards, CardOrientation.FaceUp,
                                                                 FlipAnimationDuration, beginTime));
            await Task.WhenAll(taskList);

            taskList.Clear();
            //
            //  move computer cards to the crib.  do it slow the first time so that the user can learn where to place the cards
            //
            // don't Transfer the cards because they all still belong to the deck -- we'll transfer below
            await AnimateMoveComputerCardstoCrib(discardedComputerCards, false);

            //
            //  Now put the cards where they belong - they are all currently owned by the deck...
            CardGrid.TransferCards(_cgDeck, _cgComputer, computerCards);
            CardGrid.TransferCards(_cgDeck, _cgPlayer, playerCards);
            CardGrid.TransferCards(_cgComputer, _cgCrib, discardedComputerCards);
        }