首页 > 解决方案 > 如何在Unity3D中查找列表中的3个元素是否相同

问题描述

我正在团结一致地开发纸牌游戏。我有 5 张不同的牌(A、B、C、D、E),我手上最多可以有 6 张牌。而且我有一个按钮。所以一旦我抽出一张牌就点击按钮。所以所有这些功能都准备好了。但是我想在抽出的牌中执行一个动作。如果任何三张牌是相似的。

例如:- 我抽出 5 张牌,抽出的牌按以下顺序排列:A、A、A、B、C 现在如您所见,我有 3-AI 想要执行 A 动作。或者如果卡片是这样的 A,B,C,C,C,E 如果想要执行 C 功能,因为有 3 个 C。因此,如果 3 张卡是相同类型的,我想执行与其相关的操作。

因此,为了简要介绍一下,我将解释一次流程。Card-Draw Button 点击​​ -> 每次点击继续获取卡片 -> 将卡片添加到名为 _CardList 的列表中并对列表中的卡片进行排序 -> 如果有 3 张相同的卡片,则执行操作。我在最后一次检查卡时面临问题。

我已经做了一个方法,但它超级大而且不可取。因此,如果你们能在这方面帮助我,那将对我有很大帮助。谢谢,我会用我尝试的方法在下面发布我所有的疑问和问题。

Try#1
Have multiple list like:
List<Card> Alist;
List<Card> BList; //and so on until And keep creating list as long as card type
.
.
List<Card> Elist;

void DrawButton() // Function thats passed to the button. Meaning what to happen when the button is clicked
{
//Here we instantiate the card randomly from a list given from inspector.

 GameObject card = Instantiate(cards._cardModel, _playerHandPoints[clicks].localPosition, _playerHandPoints[clicks].localRotation, mCardHolderParent.transform);
 
 //We access the card script attached to card and give its type and everything 
 card.GetComponent<Cards>()._cardType = cards._cardType;
 
 //Here we pass the instantiated card
 AddNewCard(card.GetComponent<Cards>());

 //Every time after adding we check if any list count is 3 and perform related action
 CardMatchThreeChecker();
}

AddNewCard(Card inNewCard)
{ 
//in this function we check the card type and add it to their specific list & also add it to _cardList.
  switch (inNewCard._cardType)
        {
            case CardType.A: AList.Add(inNewCard);
                break;
            case CardType.B: BList.Add(inNewCard);
                break;
            case CardType.C: CList.Add(inNewCard);
                break;
            case CardType.D: DList.Add(inNewCard);
                break;
            case CardType.E: EList.Add(inNewCard);
                break;
              default:
                break;

        }
  _CardList.Add(inNewCard);
}
 void CardMatchThreeChecker()
{
        if (AList.Count == 3)
        {
            SceneManager.LoadScene(1);
        }
        if (BList.Count == 3)
        {
            SceneManager.LoadScene(2);
        }
        if (CList.Count == 3)
        {
            SceneManager.LoadScene(3);
        }
        if (DList.Count == 3)
        {
            SceneManager.LoadScene(4);
        }
        if (EList.Count == 3)
        {
            SceneManager.LoadScene(5);
        }
}


问题 - 1:使用上述方法,如果我将来添加任何新卡,我需要继续添加许多列表,不建议在这些列表上添加,因为一次可玩的卡也可以是 20 或更多

问题-2:没有使用包含所有实例化卡片的列表“_cardList”。

问题 - 1 相反,我想检查 _CardList 列表本身中是否有任何 3 件事相似。就像每次我抽一张牌一样,他们都会将自己添加到卡片列表的列表中,我想检查列表中是否有任何三个元素相似,然后执行相关操作。我在互联网上搜索,但没有找到或知道该做什么或如何尝试。

但是,我尝试了一种方法,但现在我也陷入了困境。下面提到的方法

These lines below come under a function called CardChecking() That is called in DrawButton(). Here Im saying it to check the list only if card count is more than 2 because we have a match only if 3 cards are there and we cant have a match if cards are less that 3 as we are checking if there are 3 similar cards. And as we want to check the elements of the list we have a for loop with (i,i+1,i+2) making it checking elements of the cardlist. Problem of this outside the snippet.

for (int i = 0; i < _CardList.Count; i++)
        {
            if (_CardList.Count > 2)
            {
                if(_CardList[i]._cardType == _CardList[i+1]._cardType && _CardList[i + 1]._cardType == _CardList[i+2]._cardType)
                {
                  SceneManager.LoadScene(_CardList[i]._cardType.ToString());
                }
                else
                {
                    continue;
                }
            }
        }

我面临的问题是:例如,如果我抽 3 张卡片,我的卡片列表计数将为 3,这将满足我的条件并检查功能。如果我的卡不是同一类型,那么它将进入下一个迭代,那时我的 i 值将是 cardList[1] || 卡片列表[2] && 卡片列表[2] || cardList[3] 表示我的卡值在我的 for 循环的第二次迭代中为 1,2 和 3,但是当它检查 cardList[3] 时,它会抛出索引超出范围,因为我的卡列表计数为 3,并且它没有第 4 个元素for 循环正在检查。所以不知道如何克服这个。

标签: c#listunity3dcard

解决方案


要解决问题 - 1,您可以使用 Dictionary<CardType,List> :

    Dictionary<CardType,List<Card>> cardListDictionary = new Dictionary<CardType, List<Card>>();
    
    AddNewCard(Card inNewCard)
    {
        if (!cardListDictionary.TryGetValue(inNewCard._cardType,out var list))
        {
            list = new List<Card>();
            cardListDictionary.Add(inNewCard._cardType,list);
        }
        list.Add(inNewCard);
    }

要解决问题 - 2,请像这样实现 CardChecking:

void CardChecking()
    {
        if (_CardList.Count > 2)
        {
            var type = _CardList[0]._cardType;
            var count = 1;
            for (int i = 1; i < _CardList.Count; i++)
            {
                if(_CardList[i]._cardType == type)
                {
                    count++;
                    if (count == 3)
                    {
                        SceneManager.LoadScene(type.ToString());
                    }
                }
                else
                {
                    type = _CardList[i]._cardType;
                    count = 1;
                }
            
            }
        }
    }

推荐阅读