开始我的第一个OOP项目。我已经初步确定了一张有价值且适合的卡片清单。所以我有一个52个对象的列表。我需要洗牌。我给每个对象一个名为ShuffleRank的字段。我给每张卡片一个1-100之间的随机值。现在我想用这个值对牌组进行排序并返回一个新牌组。我已经尝试过OrderBy和Sort,但我遇到了类型问题。这是我的甲板课:
namespace BlackJack
{
public class Deck
{
//This needs to be public on the get side to be visible outside
//of the Deck object
public List<Card> Cards { get; private set; }
public List<Card> Shuffled { get; set; }
Random rnd = new Random();
public Deck()
{
NewDeck();
}
private void NewDeck()
{
Cards = new List<Card>();
for (var i = 0; i < 4; i++)
{
for (var j = 2; j < 13; j++)
{
Cards.Add(new Card { Suit = (Suit)i, Value = (CardValue)j });
}
}
}
public void shuffleCards()
{
foreach (var Card in Cards)
{
Card.ShuffleRank = rnd.Next(100);
Shuffled = Cards.Sort().......
}
}
}
}