Context: A card game; I want to deal out cards from a deck to each player in the game, in a clean way.
This is what I had in mind:
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;
}
Surely you can see the reference problems. Using the ref keyword leads to some not-so-nice looking code, but it may be unavoidable. Any suggestions?
I would prefer a solution that is flexible enough to handle other "card-passing" situations (a player playing a card to the pile, moving cards from the pile to a "trash" deck, etc.).