Exemplo n.º 1
0
    private void PlayCardResultSound(Card.CardValue match)
    {
        AudioSource audio = GetComponent <AudioSource>();

        if (match == Card.CardValue.Ammo)
        {
            audio.PlayOneShot(Reload);
        }
        else if (match == Card.CardValue.Health)
        {
            audio.PlayOneShot(HealthPickup);
        }
        else if (match == Card.CardValue.Poison)
        {
            audio.PlayOneShot(Poison);
        }
        else if (match == Card.CardValue.Health100)
        {
            audio.PlayOneShot(HealthPickup);
        }
        else if (match == Card.CardValue.Unknown)
        {
            // No match
            audio.PlayOneShot(NoMatch);
        }
    }
Exemplo n.º 2
0
    private Texture2D GetTextureForCardValue(Card.CardValue cardValue)
    {
        switch (cardValue)
        {
        case Card.CardValue.Unknown:
            return(Question);

        case Card.CardValue.Ammo:
            return(Ammo);

        case Card.CardValue.Health:
            return(Health);

        case Card.CardValue.Poison:
            return(Poison);

        case Card.CardValue.Poison2:
            return(Poison2);

        case Card.CardValue.Block:
            return(Block);

        case Card.CardValue.Health100:
            return(Health100);

        default:
            return(Question);
        }
    }
Exemplo n.º 3
0
        /**
         * Check if player is allowed to make meld of given CardValue,
         * in terms of natural to wild card ratio.
         * @param cv CardValue of meld
         * @return True -> is allowed, False -> is not allowed
         */
        private bool CanMeld(Card.CardValue cv)
        {
            int natural = CardAmount(cv, myCards);
            int wild    = WildCardAmount();

            return(natural > 1 && (wild > 0 || natural > 2));
        }
Exemplo n.º 4
0
 /**
  * Run given meld -> remove cards from player hand and add cards to player melds.
  * @param meld      Meld to run
  * @param meldValue CardValue of meld
  */
 private void RunMeld(List <Card> meld, Card.CardValue meldValue)
 {
     foreach (Card card in meld)
     {
         myCards.Remove(card);
     }
     myMelds[CardValueToValue(meldValue) - 3] = meld;
 }
Exemplo n.º 5
0
        public void ReturnHighestCardInHand(Card.CardValue[] sourceCardValues,
                                            Card.CardValue expectedValue)
        {
            Hand instance         = new Hand(sourceCardValues);
            var  highestCardValue = instance.GetHighestCardValue();

            Assert.AreEqual(highestCardValue, expectedValue);
        }
Exemplo n.º 6
0
        /**
         * Count amount of given cards of given or lower CardValue
         * @param cv       CardValue (or lower) to consider
         * @param cards    Cards to count
         * @return Amount of cards of CardValue or lower
         */
        private static int CountLowerOrSame(List <Card.CardValue> values, Card.CardValue cv)
        {
            int count = 0;

            foreach (Card.CardValue value in values)
            {
                count += CardValueToValue(value) <= CardValueToValue(cv) ? 1 : 0;
            }
            return(count);
        }
Exemplo n.º 7
0
        /**
         * Count amount of given cards of given CardValue
         * @param cv    CardValue to consider
         * @param cards Cards to count
         * @return Amount of cards of CardValue
         */
        private static int CardAmount(Card.CardValue cv, List <Card> cards)
        {
            int amount = 0;

            foreach (Card card in cards)
            {
                amount += card.value == cv ? 1 : 0;
            }
            return(amount);
        }
Exemplo n.º 8
0
 public Sprite FindCardSprite(Card.CardValue v, Card.CardSuit s)
 {
     foreach (var item in commonFronts)
     {
         if (item.suit == s && item.value == v)
         {
             return(item.faceArt);
         }
     }
     return(null);
 }
Exemplo n.º 9
0
 /**
  * Search for first card of player of given CardValue
  * @param cv CardValue to consider
  * @return First Card of given CardValue, Null if not found
  */
 private Card FindFirstCard(Card.CardValue cv)
 {
     foreach (Card card in myCards)
     {
         if (card.value == cv)
         {
             return(card);
         }
     }
     return(null);
 }
Exemplo n.º 10
0
        public Card CreateCard(Card.CardSuit suit, Card.CardValue value)
        {
            //make sure we can find our prefab in resorces
            var _cardObj = Resources.Load("Prefabs/Card") as GameObject;

            if (_cardObj == null)
            {
                Debug.LogError("Unable to find card prefab");
                return(null);
            }

            //now instatiate (creates an instance of that prefab and adds it to the scene)
            _cardObj = Instantiate(_cardObj);
            if (_cardObj == null)
            {
                Debug.LogError("Failed to create card prefab");
                return(null);
            }

            //we will be using the "Card" component of the newly created card a few times,
            // GetComponent is expensive so we do it once and store it. Because _card was created in this function
            //it will get freed up after the funtion and destroyed.
            var _card = _cardObj.GetComponent <Card> ();

            if (_card == null)            //if for some reason the card doesnt have a card class/component on it we will just add it.
            {
                _card = _cardObj.AddComponent <Card> ();
            }

            _card.CSuit  = suit;
            _card.CValue = value;

            //now we want to set the name of the card so it makes sense as we look at it in the hierarchy
            //and set the parent to the deck, so all the cards get hidden underneath
            _cardObj.name = _card.CValue.ToString() + "_" + _card.CSuit.ToString();
            _cardObj.transform.SetParent(transform, false);

            //we will now loop though all the children (possible card types) and
            //remove the ones we are not set to for this specific card.
            //this makes creating them a little heavier but ends up the cleanest so far for me.
            Transform _child = null;

            //loop though and destroy the kids we are not, that we dont need.
            for (int m = 0; m < _cardObj.transform.childCount; m++)
            {
                _child = _cardObj.transform.GetChild(m);
                if (_child.name != Card.CardBack && _child.name != _cardObj.name)
                {
                    Destroy(_child.gameObject);
                }
            }

            return(_card);
        }
Exemplo n.º 11
0
 private static int CardValueToValue(Card.CardValue cv)
 {
     return(cv switch
     {
         Card.CardValue.JOKER => 0,
         Card.CardValue.TWO => 1,
         Card.CardValue.THREE => 2,
         Card.CardValue.FOUR => 3,
         Card.CardValue.FIVE => 4,
         Card.CardValue.SIX => 5,
         Card.CardValue.SEVEN => 6,
         Card.CardValue.EIGHT => 7,
         Card.CardValue.NINE => 8,
         Card.CardValue.TEN => 9,
         Card.CardValue.JACK => 10,
         Card.CardValue.QUEEN => 11,
         Card.CardValue.KING => 12,
         Card.CardValue.ACE => 13,
         _ => - 1,
     });
Exemplo n.º 12
0
        /* Constructor loops through different suits and kinds in double for loop to
         * create a deck of 52 cards
         */
        public Deck()
        {
            _Deck = new List <Card>();
            string[] suits = new string[] { "Clubs", "Hearts", "Diamonds", "Spades" };
            //string[] kinds = new string[] { "2", "3", "4", "5", "6", "7", "8", "9", "10",
            //                                "Jack", "Queen", "King", "Ace" };
            Card.CardValue[] kinds = new Card.CardValue[] { Card.CardValue.TWO, Card.CardValue.THREE, Card.CardValue.FOUR,
                                                            Card.CardValue.FIVE, Card.CardValue.SIX, Card.CardValue.SEVEN, Card.CardValue.EIGHT,
                                                            Card.CardValue.NINE, Card.CardValue.TEN, Card.CardValue.JACK, Card.CardValue.QUEEN,
                                                            Card.CardValue.KING, Card.CardValue.ACE };

            foreach (var suit in suits)
            {
                foreach (var kind in kinds)
                {
                    _Deck.Add(new Card()
                    {
                        Kind = kind, Suit = suit
                    });
                }
            }
        }
Exemplo n.º 13
0
    private void OnCardMatch(Card.CardValue match)
    {
        if (match == Card.CardValue.Ammo)
        {
            AmmoCount += 10;

            gameUI.ShowAmmoBonus(10);
        }
        else if (match == Card.CardValue.Health)
        {
            Health += 25;

            gameUI.ShowHealthBonus(25);
        }
        else if (match == Card.CardValue.Poison)
        {
            Health -= 12;

            gameUI.ShowHealthBonus(-12);

            Health = Mathf.Max(Health, 0);
        }
        else if (match == Card.CardValue.Health100)
        {
            int diff = 100 - Health;
            if (diff > 0)
            {
                gameUI.ShowHealthBonus(diff);
            }

            Health = Mathf.Max(Health, 100);
        }

        UpdateUI(false);

        PlayCardResultSound(match);
    }
Exemplo n.º 14
0
        /**
         * Use drop net to predict card to drop. When the player does not hold the card,
         * the second highest prediction value gets chosen and so on.
         * @return Card to drop, identified by SUIT and VALUE
         */
        public Card Drop()
        {
            Card toDrop;
            //get raw predictions and copy it. when cutting of later the orginal one is not touched
            NDarray pred        = dropModel.Predict(input)[0];
            NDarray predCleaned = np.array(pred);

            //CardValues that the AI predicted but are not in the playershand
            List <Card.CardValue> tried = new List <Card.CardValue>();

            do
            {
                //convert prediction vector to CardValue
                NDarray        argMax  = np.argmax(predCleaned);
                int            value   = argMax.item <int>();
                Card.CardValue tryDrop = value > 0 ? ValueToCardValue(value + 1) : Card.CardValue.JOKER;
                tryDrop = ValueToCardValue(CardValueToValue(tryDrop) + CountLowerOrSame(tried, tryDrop));

                //check if player actually holds predicted CardValue
                toDrop = FindFirstCard(tryDrop);

                //JOKER stands for wild card in general -> also consider TWO
                if (tryDrop == Card.CardValue.JOKER)
                {
                    toDrop = toDrop ?? FindFirstCard(Card.CardValue.TWO);
                }

                //cuts out tried CardValue
                var sliceIdx = Numpy.Models.Slice.Index(
                    tryDrop != Card.CardValue.JOKER ? CardValueToValue(tryDrop) - CountLowerOrSame(tried, tryDrop) - 1 : 0);
                predCleaned = np.delete(predCleaned, sliceIdx);
                tried.Add(tryDrop);
            }while (toDrop == null);

            return(toDrop);
        }
Exemplo n.º 15
0
 public CardInfo(Card.CardColor color, Card.CardValue value)
 {
     this.color = color;
     this.value = value;
 }
Exemplo n.º 16
0
 /// <summary>
 /// Check if cards have same card value
 /// </summary>
 public bool EqualValue(Card.CardValue cardValue)
 {
     return(this.value == cardValue);
 }
Exemplo n.º 17
0
        /**
         * Use meldIf net to predict wether to meld cards or not. If yes, meld value and (wild)card amount
         * get predicted by (meld)value/wild net. Runs as long AI wants(= prediction) to and also can(=cards) meld.
         * @param minMeldPoints Minimal points needed to be a valid meld
         * @return All melds to be made. Every array element is representing one meld with a list of cards.
         * The element at index == 0 represents the meld with CardValue == Four, index == 1 -> CardValuue == Five, ...
         * When an array element == null, there are no cards of this CardValue to be meld.
         */
        public List <Card>[] Meld(int minMeldPoints = 0)
        {
            List <Card>[] toMeld     = new List <Card> [11];
            int           meldPoints = 0;

            //get raw "if" prediction and convert vector to boolean
            NDarray predIf  = meldIfModel.Predict(input)[0];
            float   valueIf = predIf.item <float>();
            bool    doMeld  = Math.Round(valueIf) == 1;

            if (doMeld)
            {
                Card toMeldElem;
                do
                {
                    //get raw "value" predictions and copy it. when cutting of later the orginal one is not touched
                    NDarray predValue        = meldValueModel.Predict(input)[0];
                    NDarray predValueCleaned = np.array(predValue);

                    //CardValues that the AI predicted but are not in the playershand
                    List <Card.CardValue> tried = new List <Card.CardValue>();
                    do
                    {
                        //convert prediction vector to CardValue
                        NDarray        argMaxValue = np.argmax(predValueCleaned);
                        int            valueValue  = argMaxValue.item <int>();
                        Card.CardValue tryMeld     = ValueToCardValue(valueValue + 3);
                        tryMeld = ValueToCardValue(CardValueToValue(tryMeld) + CountLowerOrSame(tried, tryMeld));

                        //check if player actually holds predicted CardValue
                        toMeldElem = FindFirstCard(tryMeld);

                        //cuts out tried CardValue
                        var sliceIdx = Numpy.Models.Slice.Index(CardValueToValue(tryMeld) - 3 - CountLowerOrSame(tried, tryMeld));
                        predValueCleaned = np.delete(predValueCleaned, sliceIdx);
                        tried.Add(tryMeld);
                    }while (predValueCleaned.size > 0 && (toMeldElem == null || !CanMeld(toMeldElem.value)));

                    if (toMeldElem != null)
                    {
                        //get raw "wild" prediction and convert vector to wild card amount. also considering max wild cards
                        NDarray predWild       = meldWildModel.Predict(input)[0];
                        NDarray argMaxWild     = np.argmax(predWild);
                        int     wildCardAmount = WildCardAmount();
                        int     cardAmount     = CardAmount(toMeldElem.value, myCards);
                        int     valueWild      = argMaxWild.item <int>() <= wildCardAmount?argMaxWild.item <int>() : wildCardAmount;



                        //correct if not enough cards or too many wild cards
                        if (valueWild + cardAmount < 3)
                        {
                            valueWild = 3 - cardAmount;
                        }
                        if (valueWild > cardAmount)
                        {
                            valueWild = cardAmount;
                        }

                        //build meld
                        List <Card> meld = AllCardsOfCVs(new List <Card.CardValue>()
                        {
                            toMeldElem.value
                        });
                        meld.AddRange(AllCardsOfCVs(new List <Card.CardValue>()
                        {
                            Card.CardValue.TWO, Card.CardValue.JOKER
                        }, valueWild));

                        //update meld array and meld points
                        toMeld[CardValueToValue(toMeldElem.value) - 3] = meld;
                        meldPoints += MeldToPoints(meld);

                        //update myCards and myMelds
                        RunMeld(meld, toMeldElem.value);

                        //get raw "if" prediction (after updating input vector) and convert vector to boolean
                        FillInput();
                        predIf  = meldIfModel.Predict(input)[0];
                        valueIf = predIf.item <float>();
                        doMeld  = Math.Round(valueIf) == 1;
                    }
                    else
                    {
                        doMeld = false;
                    }
                } while (doMeld);
            }
            return(meldPoints >= minMeldPoints ? toMeld : new List <Card> [11]);
        }