背景:纸牌游戏;我想以一种干净的方式将牌组中的牌发给游戏中的每个玩家。
这就是我的想法:
public static CardGame.IGame DealAll(this CardGame.IGame objThis, CardGame.Card[] cards)
{
if (objThis.Players.Length > 0)
{
for (int i = 0; i < cards.Length; i++)
{
objThis.Deck.MoveTo(cards[i], objThis.CurrentPlayer.Hand);
objThis.AdvancePlayer();
}
}
return objThis;
}
public static Card[] MoveTo(this Card[] objThis, Card card, Card[] cards)
{
List<Card> lstCards = cards.ToList();
List<Card> lstThis = objThis.ToList();
lstThis.Remove(card);
lstCards.Add(card);
objThis = lstThis.ToArray();
cards = lstCards.ToArray();
return cards;
}
当然,您可以看到参考问题。使用 ref 关键字会导致一些看起来不太好看的代码,但这可能是不可避免的。有什么建议么?
我更喜欢一种足够灵活的解决方案来处理其他“传牌”情况(玩家将牌打到牌堆中,将牌从牌堆移到“垃圾”牌组等)。