Beispiel #1
0
        //returns true if play valid, false if play impossible (must take pile instead)
        //optionally returns a pile that the player MUST take.
        public bool TryPlayCard(Card c, Player p, out Pile takePile)
        {
            takePile = null;
            //check if valid
            if (!p.Hand.Cards.Contains(c))
            {
                throw new InvalidOperationException($"Player \"{p}\" does not have card \"{c}\".");
            }
            p.Hand.Cards.Remove(c);

            //Player has now played the card
            EventDispatcher.Broadcast(new PlayerCardEvent()
            {
                Player = p, Card = c
            });


            //find pile
            var  delta      = 105;
            Pile targetPile = null;

            foreach (var pile in Piles)
            {
                var diff = c.Number - pile.Cards.Last().Number;
                if (diff > 0 && diff < delta)
                {
                    targetPile = pile;
                    delta      = diff;
                }
            }
            if (targetPile == null)
            {
                return(false);
            }

            else
            {
                try
                {
                    targetPile.AddCard(c);
                }
                catch (InvalidOperationException)
                {
                    takePile = targetPile;
                    return(false);
                }
            }

            return(true);
        }
Beispiel #2
0
        //Take a pile for a player
        public void TakePile(Card c, Player pl, Pile pi)
        {
            // Notify others before the pile is actually taken
            EventDispatcher.Broadcast(new PlayerTookPileEvent()
            {
                NewCard = c, Player = pl, Pile = pi
            });

            var takenPile = pi.ReplacePile(c);

            foreach (var card in takenPile)
            {
                pl.Take(card);
            }
        }