コンテキスト:カードゲーム。デッキからゲーム内の各プレイヤーにきれいにカードを配りたいです。
これは私が念頭に置いていたものです:
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キーワードを使用すると、見栄えの悪いコードになりますが、やむを得ない場合があります。助言がありますか?
私は、他の「カード通過」状況(プレイヤーがカードを山にプレイする、カードを山から「ゴミ箱」デッキに移動するなど)を処理するのに十分な柔軟性のあるソリューションを好みます。