I'm making solitaire game in mvc. I use this design pattern for the first time so it makes me a little bit confused. I've already created scripts for card - model, controller and view and also a factory to creating them. Now I wonder how to solve the problem of creating cards and assign them to the proper stack. For stacks I want to create models with list of cards and controller with functions(obvious).
The first problem is if the stack list should contain only card models or created card objects?
Another problem is the proces of assigning cards to lists. My idea was to creates a list of cards models with set value and suit, then assign cards to bottom stacks and the rest of cards assign to deck stack. Here is the code:
public class CardsManagerr : MonoBehaviour { public enum Suits { Hearts = 1, Diamonds, Clubs, Spades } public enum Values { Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, J, Q, K, A = 1}
[SerializeField] DeckPileController deck; [SerializeField] BottomPileController bottom1; [SerializeField] BottomPileController bottom2; [SerializeField] BottomPileController bottom3; [SerializeField] BottomPileController bottom4; [SerializeField] BottomPileController bottom5; [SerializeField] BottomPileController bottom6; [SerializeField] BottomPileController bottom7; [SerializeField] TopPileController top1; [SerializeField] TopPileController top2; [SerializeField] TopPileController top3; [SerializeField] TopPileController top4; private CardFactory factory; private void Start() { CreateDecks(); } private void CreateDecks() { List<CardModel> cardModels = new List<CardModel>(); List<CardModel> tempBottom1 = new List<CardModel>(); List<CardModel> tempBottom2 = new List<CardModel>(); List<CardModel> tempBottom3 = new List<CardModel>(); List<CardModel> tempBottom4 = new List<CardModel>(); List<CardModel> tempBottom5 = new List<CardModel>(); List<CardModel> tempBottom6 = new List<CardModel>(); List<CardModel> tempBottom7 = new List<CardModel>(); foreach (Values value in (Values[])Enum.GetValues(typeof(Values))) { foreach (Suits suit in (Suits[])Enum.GetValues(typeof(Suits))) { int intValue = (int)value; CardModel card = new CardModel(); card.SetSuit(suit.ToString()); card.SetValue(intValue); cardModels.Add(card); } } Shuffle(cardModels); SplitTheCards(cardModels, tempBottom1, 1); SplitTheCards(cardModels, tempBottom2, 2); SplitTheCards(cardModels, tempBottom3, 3); SplitTheCards(cardModels, tempBottom4, 4); SplitTheCards(cardModels, tempBottom5, 5); SplitTheCards(cardModels, tempBottom6, 6); SplitTheCards(cardModels, tempBottom7, 7); bottom1.SetCardsInPile(tempBottom1); bottom2.SetCardsInPile(tempBottom1); bottom3.SetCardsInPile(tempBottom1); bottom4.SetCardsInPile(tempBottom1); bottom5.SetCardsInPile(tempBottom1); bottom6.SetCardsInPile(tempBottom1); bottom7.SetCardsInPile(tempBottom1); deck.SetCardsInPile(cardModels); } private void Shuffle<T>(List<T> list) { System.Random random = new System.Random(); int n = list.Count; while (n > 1) { int k = random.Next(n); n--; T temp = list[k]; list[k] = list[n]; list[n] = temp; } } private void SplitTheCards<T>(List<T> givingList, List<T> receivingList, int numOfElements) { for (int i = 0; i < numOfElements; i++) { receivingList.Add(givingList.Last<T>()); givingList.RemoveAt(givingList.Count - 1); } }
It works but I'm not sure if it looks good. Do you have any tips how to code it better? Or any ideas how to design it?