我正在制作一款使用unity的纸牌游戏。即我有5张不同的纸牌(A,B,C,D,E),而我手中的纸牌最多是6张。我有一个按钮。抽出一张牌后,点击按钮。所有这些函数都准备好了。但我想在抽到的牌中执行一个动作。如果任意三张牌相似
例如:-我抽出5张牌,抽出的牌是按照这个顺序:A,A,A,B,C现在你看到我有3-A,我想执行一个动作。或者如果牌是这样的A B C C C C E如果想要执行C函数因为有3个C。因此,如果3张牌是相同的类型,我想执行一个与之相关的操作。
所以为了更简短,我将解释一次流程。绘图按钮点击->每次点击都能获得卡片->将这些卡片添加到一个名为_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:没有使用List" _cardList">
Question - 1相反,我想检查是否有任何3件事在_CardList列表本身是相似的。就像每次我抽到一张牌,它们都会把自己添加到cardlist的List中,我想检查列表中是否有三个元素相似,然后执行相关的操作。我在互联网上搜索,但没有找到或得到一个想法做什么或如何尝试。
但是我尝试了一种方法,但现在我也陷入了困境。下面提到的方法
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张牌,我的cardlist计数将是3,这将验证我的条件并检查函数。如果我的牌不是相同的类型它将进入下一个迭代此时我的i值将是cardList[1] || cardList[2] &&cardList[2] || cardList[3]表示我的卡片值1,2 &3在我的for循环的第二次迭代,但当它检查cardList[3]时,它抛出Index超出范围,因为我的cardList计数是3,它没有for循环正在检查的第4个元素。所以不知道如何克服这个。
要解决问题- 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;
}
}
}
}
我假设卡片的顺序无关紧要,并且认为您正在寻找LinqGroupBy
// Just a simplified example since you didn't show your Card class
class Card
{
public CardType _cardType;
public Card(CardType type)
{
_cardType = type;
}
}
var list = new List<Card>
{
new Card(CardType.A),
new Card(CardType.B),
new Card(CardType.C),
new Card(CardType.C),
new Card(CardType.B),
new Card(CardType.B),
new Card(CardType.A),
new Card(CardType.A),
new Card(CardType.B)
};
// This creates separated groups according to the value of the card
var groups = list.GroupBy(c => c._cardType)
// Then it filters out only those groups that have 3 or more items
.Where(g => g.Count() >= 3);
// Then you can simply iterate through them
foreach (var @group in groups)
{
// The Key is the value the grouping was based on
// so in this example the Card._cardType
Debug.Log($"There are {@group.Count()} cads with cardType = {@group.Key}");
// you can also get all the according cards if needed
//var cards = @group.ToArray();
}
将打印
There are 3 cards with cardType = A
There are 4 cards with cardType = B
进一步,如果你只想获得第一个包含3
项的组并且无论如何已经知道不会同时有两个或更多项你可以输入
// again first create separate groups based on the value
var group = list.GroupBy(c => c._cardType)
// Pick the first group that has at least 3 items
// or NULL if there is none
.FirstOrDefault(g => g.Count() >= 3);
if (group != null)
{
Debug.Log($"Found a group of {group.Count()} for the value = {group.Key}!");
}
else
{
Debu.Log("There is no group with 3 elements yet...");
}
嗯,我不确定我是否理解对了。让我把这个问题简化一下,如果可行,那就有解决办法了。
问题:我有一张卡片列表,如果列表中有3张相同类型的卡片,我需要执行一个操作。
答案:使用Linq将列表按类型分组。然后,过滤结果以找到"3-cards case"。
给你:
var answer = _CardList
.GroupBy(c => c.GetCardType())
.ToDictionary(g => g.Key, g => g.Count()) // Count by the card type.
.Where(x => x.Value >= 3) // Three or more cards of the same type.
.Select(x => x.Key) // Fetch the card type.
.FirstOrDefault(); // Only one match is needed.
if (answer != null) {
// answer is the type of the card that is counted 3 or more times.
}
如果您需要检测所有的"3张牌"情况去除FirstOrDefault
,处理收集