Exemplo n.º 1
0
        private int ScorePairs(List <CardView> list, ScoreCollection scoreStory, HandType handType)
        {
            List <List <CardView> > listlist = GetPairCards(list);
            int score = 0;

            foreach (List <CardView> cards in listlist)
            {
                int cardCount = cards.Count;

                StatName name = StatName.Ignored;
                if (cardCount == 2)
                {
                    name   = (handType == HandType.Regular) ? StatName.HandPairs : StatName.CribPairs;
                    score += 2;
                }
                else if (cardCount == 3)
                {
                    name   = (handType == HandType.Regular) ? StatName.Hand3OfAKind : StatName.Crib3OfAKind;
                    score += 6;
                }
                else if (cardCount == 4)
                {
                    score += 12;
                    name   = (handType == HandType.Regular) ? StatName.Hand4OfAKind : StatName.Crib4OfAKind;
                }

                scoreStory.Scores.Add(new ScoreInstance(name, 1, score, CardListToIntList(cards)));
            }

            return(score);
        }
Exemplo n.º 2
0
        public ScoreCollection UpdateScoreForCrib(PlayerType type, long scoredelta)
        {
            Player p = GetPlayer(type);

            ScoreCollection ss = new ScoreCollection();

            ss.ScoreType = ScoreType.Crib;
            ss.Accepted  = false;
            if (p != null && p.HasCrib)
            {
                int score = p.Crib.ScoreHand(_deck.SharedCard, ss, HandType.Crib);


                if (p.Type == PlayerType.Computer)
                {
                    scoredelta = score;
                }

                if (score == scoredelta)
                {
                    ss.Accepted = true;
                    p.Score    += score;
                }
            }
            if (ss.Accepted)
            {
                ToggleDeal();
            }
            else
            {
                MainPage.LogTrace.TraceMessageAsync(String.Format("Warning: Crib backScore not accepted! Deal not toggled."));
            }

            return(ss);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     the score is set to what is in the Total field, not the ActualScore field
        /// </summary>
        /// <param name="type"></param>
        /// <param name="handType"></param>
        /// <param name="scoredelta"></param>
        /// <param name="difficulty"></param>
        /// <param name="muggins"></param>
        /// <returns></returns>
        public ScoreCollection UpdateScore(PlayerType type, HandType handType, long scoredelta, GameDifficulty difficulty)
        {
            ScoreCollection score = new ScoreCollection();

            if (handType == HandType.Crib)
            {
                score.ScoreType = ScoreType.Crib;;
                score           = UpdateScoreForCrib(type, scoredelta);
            }
            else
            {
                Player p = GetPlayer(type);
                score.ScoreType = ScoreType.Hand;
                if (p != null)
                {
                    int s = p.Hand.ScoreHand(_deck.SharedCard, score, handType);


                    if (p.Type == PlayerType.Computer)
                    {
                        scoredelta = s;
                    }

                    if (s == scoredelta)
                    {
                        score.Accepted = true;
                        p.Score       += score.Total;
                    }
                }
            }
            return(score);
        }
Exemplo n.º 4
0
        int ScoreFifteens(List <CardView> list, ScoreCollection scoreStory, HandType handType)
        {
            int score = 0;
            List <List <CardView> > listlist = ScoreFifteensInternal(list);

            foreach (List <CardView> cards in listlist)
            {
                StatName name = (handType == HandType.Regular) ? StatName.Hand15s : StatName.Crib15s;
                scoreStory.Scores.Add(new ScoreInstance(name, 1, 2, CardListToIntList(cards)));
                score += 2;
            }
            return(score);
        }
Exemplo n.º 5
0
        public int GetScore(PlayerType type, HandType handType)
        {
            Player          p     = GetPlayer(type);
            ScoreCollection score = new ScoreCollection();

            if (handType == HandType.Regular)
            {
                score.ScoreType = ScoreType.Hand;
                return(p.Hand.ScoreHand(_deck.SharedCard, score, handType));
            }



            score.ScoreType = ScoreType.Crib;

            return(p.Crib.ScoreHand(_deck.SharedCard, score, handType));
        }
Exemplo n.º 6
0
        /// <summary>
        /// a web request comes into the service to play a card during counting phase.
        ///
        /// we
        /// 1. Verify it is the right turn
        /// 2. Verify the card is in the hand
        /// 3. Count the card
        /// 4. remove the card
        /// 5. set the turn state
        /// 6. return the state of the count
        ///
        /// Whoever should go next should be definitively set
        /// </summary>
        /// <param name="PlayerId"></param>
        /// <param name="card"></param>
        /// <returns></returns>
        public CountingState PlayCard(Player player, CardView card, GameDifficulty diffuculty)
        {
            if (_state.TurnPlayer != player)
            {
                throw new Exception("Not your turn!");
            }

            List <CardView> cards = _playerHand;

            if (player.Type == PlayerType.Computer)
            {
                cards = _computerHand;
            }


            if (cards.Contains(card))
            {
                ScoreCollection score = null;
                if (CountCard(card, out score) != -1)
                {
                    _state.ScoreStory = score;
                    _state.LastScore  = score.Total;
                    cards.Remove(card);
                    if (!HasCardsToPlay)
                    {
                        _state.LastScore++; // point for last card
                        score.Scores.Add(new ScoreInstance(StatName.CountingLastCard, 1, 1, card.Index));
                        score.Total++;
                    }

                    score.ActualScore = score.Total;
                }
            }

            UpdateTurnState();

            MainPage.LogTrace.TraceMessageAsync(String.Format("In PlayCard.  Player:{0} Score:{1} Adding:{2} CardPlayed:{3}", player.Type, player.Score, _state.LastScore, card.Data.Name));
            player.Score += _state.LastScore;



            return(_state);
        }
Exemplo n.º 7
0
        public List <CardView> PickCribCards(List <CardView> hand, CardView sharedCard, bool myCrib, GameDifficulty diffuculty)
        {
            Combinations <CardView> combinations = new Combinations <CardView>(hand, 4);
            List <CardView>         maxCrib      = null;
            double maxScore = -1000.0;

            foreach (List <CardView> cards in combinations)
            {
                if (sharedCard != null)
                {
                    cards.Add(sharedCard);
                }
                ScoreCollection scoreStory    = new ScoreCollection();
                double          score         = (double)ScoreCards(cards, scoreStory, HandType.Regular);
                List <CardView> crib          = GetCrib(hand, cards);
                double          expectedValue = 0.0;
                if (diffuculty == GameDifficulty.Hard)
                {
                    if (myCrib)
                    {
                        expectedValue = CribbageStats.dropTableToMyCrib[crib[0].Rank - 1, crib[1].Rank - 1];
                        score        += expectedValue;
                    }
                    else
                    {
                        expectedValue = CribbageStats.dropTableToYouCrib[crib[0].Rank - 1, crib[1].Rank - 1];
                        score        -= expectedValue;
                    }
                }

                if (score > maxScore)
                {
                    maxScore = score;
                    maxCrib  = crib;
                }
            }

            return(maxCrib);
        }
Exemplo n.º 8
0
        public int ScoreCards(List <CardView> list, ScoreCollection scoreStory, HandType handType)
        {
            int score = 0;

            score += ScoreNibs(list, scoreStory, handType);                    // this is the only one where it matters which particular card is shared

            //
            //   DON't SORT BEFORE NIBS!!!
            list.Sort(CardView.CompareCardsByRank);

            score += ScoreFifteens(list, scoreStory, handType);
            score += ScorePairs(list, scoreStory, handType);
            score += ScoreRuns(list, scoreStory, handType);
            score += ScoreFlush(list, scoreStory, handType);


            scoreStory.Total       = score;
            scoreStory.ActualScore = score;


            return(score);
        }
Exemplo n.º 9
0
        private int ScoreNibs(List <CardView> list, ScoreCollection scoreStory, HandType handType)
        {
            if (list.Count == 4) // shared card, not passed in, we can't tell if we have nibs
            {
                return(0);
            }

            for (int i = 0; i < 4; i++)
            {
                if (list[i].Rank == 11) //Jack -- 1 indexed
                {
                    if (list[i].Suit == list[4].Suit)
                    {
                        StatName name = (handType == HandType.Regular) ? StatName.HandJackOfTheSameSuit : StatName.CribJackOfTheSameSuit;
                        scoreStory.Scores.Add(new ScoreInstance(name, 1, 1, list[i].Index));
                        return(1);
                    }
                }
            }

            return(0);
        }
Exemplo n.º 10
0
        public int ScoreHand(CardView sharedCard, ScoreCollection scoreStory, HandType handType)
        {
            int             score = 0;
            List <CardView> list  = new List <CardView>(_cards);

            list.Add(sharedCard);
            score = ScoreCards(list, scoreStory, handType);

            string trace = "Cards: ";

            foreach (CardView c in _cards)
            {
                trace += c.Data.Name.ToString() + " ";
            }

            trace += "Shared: " + sharedCard.Data.Name.ToString();
            trace += " Score: " + score;

            MainPage.LogTrace.TraceMessageAsync(trace);

            return(score);
        }
Exemplo n.º 11
0
        /// <summary>
        /// by the time we've gotten here, we've verified that Card is valid
        /// </summary>
        /// <param name="card"></param>
        private int CountCard(CardView card, out ScoreCollection score)
        {
            int nScore = 0;

            score           = new ScoreCollection();
            score.ScoreType = ScoreType.Count;

            if (card.Value + _state.Count > 31)
            {
                throw new Exception("The total count must be less than 31. Play a different card");
            }
            List <int> cardIndeces = new List <int>();

            if (card.Value + _state.Count == 15)
            {
                cardIndeces = Hand.CardListToIntList(_countedCards);
                cardIndeces.Add(card.Index);
                nScore += 2;
                score.Scores.Add(new ScoreInstance(StatName.CountingHit15, 1, 2, cardIndeces));
                cardIndeces.Clear();
            }

            if (card.Value + _state.Count == 31)
            {
                nScore += 2;
                score.Scores.Add(new ScoreInstance(StatName.CountingHit31, 1, 2, card.Index));
                cardIndeces.Clear();
            }

            _countedCards.Insert(0, card);
            int      run      = FindARun(_countedCards);
            StatName statName = StatName.Ignored;

            if (run > 0)
            {
                switch (run)
                {
                case 3:
                    statName = StatName.Counting3CardRun;
                    break;

                case 4:
                    statName = StatName.Counting4CardRun;
                    break;

                case 5:
                    statName = StatName.Counting5CardRun;
                    break;

                case 6:
                    statName = StatName.Counting6CardRun;
                    break;

                case 7:
                    statName = StatName.Counting7CardRun;
                    break;

                default:
                    MainPage.LogTrace.TraceMessageAsync(String.Format("ERROR: Bug in CountCard looking for run length! run: {0}", run));
                    break;
                }
                score.Scores.Add(new ScoreInstance(statName, run, run, Hand.CardListToIntList(_countedCards, run)));
            }
            nScore += run;
            int pairs = FindPairs(_countedCards);

            if (pairs > 0)
            {
                switch (pairs) // this is points from pairs, not pairs count
                {
                case 2:
                    cardIndeces = Hand.CardListToIntList(_countedCards, 2);
                    statName    = StatName.CountingPair;
                    break;

                case 6:
                    cardIndeces = Hand.CardListToIntList(_countedCards, 3);
                    statName    = StatName.Counting3OfAKind;
                    break;

                case 12:
                    cardIndeces = Hand.CardListToIntList(_countedCards, 4);
                    statName    = StatName.Counting4OfAKind;
                    break;

                default:
                    MainPage.LogTrace.TraceMessageAsync(String.Format("ERROR: Bug in CountCard looking for pairs! pairs:{0}", pairs));
                    break;
                }

                score.Scores.Add(new ScoreInstance(statName, 1, pairs, cardIndeces));
            }

            nScore          += pairs;
            _state.Count    += card.Value;
            _state.LastScore = nScore;
            _state.CardsCounted++;

            score.Total = nScore;
            return(nScore);
        }
Exemplo n.º 12
0
        //
        //  just call ScoreRuns to get ths list..
        private int ScoreRuns(List <CardView> list, ScoreCollection scoreStory, HandType handType)
        {
            List <List <CardView> > cardLists = DemuxPairs(list);
            List <List <CardView> > runs      = new List <List <CardView> >();
            List <ScoreInstance>    scores    = new List <ScoreInstance>();

            foreach (List <CardView> cards in cardLists)
            {
                List <CardView> l = GetRuns(cards);
                if (l != null)
                {
                    runs.Add(l);
                }
            }
            //
            //  eliminate duplicate lists - this happens if you have a hand that looks like 5, 5, 7, 8, 9 where the pair is not in the run
            if (runs.Count == 2)
            {
                if (runs[0].Count == runs[1].Count) // same length
                {
                    bool same = false;
                    for (int i = 0; i < runs[0].Count; i++)
                    {
                        if (runs[0][i] != runs[1][i])
                        {
                            same = false;
                            break;
                        }

                        same = true;
                    }

                    if (same)
                    {
                        runs.RemoveAt(1);
                    }
                }
            }


            //
            //  runs now how the list of cards that have runs in them
            int score = 0;

            {
                foreach (List <CardView> cards in runs)
                {
                    if (cards.Count > 2)
                    {
                        StatName name = StatName.Ignored;
                        switch (cards.Count)
                        {
                        case 3:
                            name = (handType == HandType.Regular) ? StatName.Hand3CardRun : StatName.Crib3CardRun;
                            break;

                        case 4:
                            name = (handType == HandType.Regular) ? StatName.Hand4CardRun : StatName.Crib4CardRun;
                            break;

                        case 5:
                            name = (handType == HandType.Regular) ? StatName.Hand5CardRun : StatName.Crib5CardRun;
                            break;

                        default:
                            MainPage.LogTrace.TraceMessageAsync(String.Format("ERRROR: invalid Maxrun.  id:2109347809. maxrun: {0}", cards.Count));
                            break;
                        }
                        score += cards.Count;
                        scoreStory.Scores.Add(new ScoreInstance(name, 1, cards.Count, CardListToIntList(cards)));
                    }
                }
            }

            return(score);
        }
Exemplo n.º 13
0
        private int ScoreFlush(List <CardView> list, ScoreCollection scoreStory, HandType handType)
        {
            List <int>[] cards = new List <int> [4];
            for (int i = 0; i < 4; i++)
            {
                cards[i] = new List <int>();
            }
            foreach (CardView c in list)
            {
                switch (c.Suit)
                {
                case Suit.Clubs:
                    cards[0].Add(c.Index);
                    break;

                case Suit.Hearts:
                    cards[1].Add(c.Index);
                    break;

                case Suit.Diamonds:
                    cards[2].Add(c.Index);
                    break;

                case Suit.Spades:
                    cards[3].Add(c.Index);
                    break;
                }
            }

            int minCardsForFlush = 4;

            if (handType == HandType.Crib)
            {
                minCardsForFlush++;
            }

            int score = 0;

            for (int i = 0; i < 4; i++)
            {
                if (cards[i].Count >= minCardsForFlush)
                {
                    StatName name = StatName.Ignored;
                    switch (cards[i].Count)
                    {
                    case 4:
                        name = StatName.Hand4CardFlush;     // no 4 card flush in crib
                        break;

                    case 5:
                        name = (handType == HandType.Regular) ? StatName.Hand5CardFlush : StatName.Crib5CardFlush;
                        break;

                    default:
                        MainPage.LogTrace.TraceMessageAsync(String.Format("ERROR: flushcount wrong. count:{0}", cards[i].Count));
                        break;
                    }
                    score = cards[i].Count;
                    scoreStory.Scores.Add(new ScoreInstance(name, 1, cards[i].Count, cards[i]));
                    break;
                }
            }

            return(score);
        }