Minor comments
[4DMemory.git] / 4DMemory / Playground.cs
blobba375527a9220cdc140edc41b38f89f06a041c08
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Microsoft.Xna.Framework;
5 using Memory;
7 namespace Memory
9 class Playground
11 private PlayerIndex m_currentPlayer;
12 private Card m_currentCard;
14 private CardsGrid m_cardsGrid;
16 private Random m_random;
18 private static int m_dimension = 10;
20 // Summary:
21 // Constructs a new Playground
22 public Playground()
24 m_currentPlayer = PlayerIndex.One;
25 m_currentCard = null;
26 m_random = new Random();
28 FillField();
31 // Summary:
32 // Selects a card as the currentPlayer, returns the next player
33 //
34 // Parameters:
35 // xpos:
36 // The x coordinate of the card
37 // ypos:
38 // The y coordinate of the card
39 // zpos:
40 // The z coordinate of the card
41 // Returns:
42 // The player that gets to select a card next
43 public PlayerIndex SelectCard(int xpos, int ypos, int zpos)
45 Card pickedCard = m_cardsGrid.getCard(xpos, ypos, zpos);
47 // No card selected, this means that the player is selecting their first card
48 if (m_currentCard == null)
50 m_currentCard = pickedCard;
51 return m_currentPlayer;
53 // They are selecting their second card, compare with the first card
54 else
56 if (m_currentCard.Equals(pickedCard))
58 // !! Player selected the correct card
59 return m_currentPlayer;
61 else
63 // !! Player selected the wrong card
64 return GetNextPlayer();
69 // Summary:
70 // Swaps two planes
71 public void SwapPlanes()
73 int maxvalue = m_cardsGrid.m_dimension - 1;
74 int firstpos = (int)Math.Round(maxvalue * m_random.NextDouble());
75 int secondpos = (int)Math.Round(maxvalue * m_random.NextDouble());
77 m_cardsGrid.swap(firstpos, secondpos);
80 // Summary:
81 // Finds the next player and returns it
83 // Returns:
84 // The next player
85 private PlayerIndex GetNextPlayer()
87 int player = (int)m_currentPlayer;
88 int nextplayer = (player % 4) + 1;
89 m_currentPlayer = (PlayerIndex)nextplayer;
90 return m_currentPlayer;
93 // Summary:
94 // Initializes all the cards
95 private void FillField()
97 IList<Card> m_allCards = GenerateCards();
98 m_cardsGrid = new CardsGrid(m_dimension);
100 for (int x = 0; x < m_dimension; x++)
102 for (int y = 0; y < m_dimension; y++)
104 for (int z = 0; z < m_dimension; z++)
106 Card card = GetRandomCard(m_allCards);
107 m_cardsGrid.setCard(x, y, z, card);
113 // Summary:
114 // Gets a random card from allCards, removes it, and retuns it.
116 // Parameters:
117 // allCards:
118 // The list from which a card will be picked
120 // Returns:
121 // The selected card
122 private Card GetRandomCard(IList<Card> allCards)
124 int maxvalue = allCards.Count - 1;
125 int pos = (int)Math.Round(maxvalue * m_random.NextDouble());
126 Card result = allCards[pos];
127 allCards.Remove(result);
128 return result;
131 // Summary:
132 // Generates a list of Cards, each card is in the list twice
134 // Returns:
135 // The generated list
136 private IList<Card> GenerateCards()
138 int totalCards = (int)Math.Pow(m_dimension, 3);
139 IList<Card> result = new List<Card>(totalCards);
141 for (int i = 0; i < totalCards / 2; i++)
143 Card firtOfPair = new Card(i);
144 Card secondOfPair = new Card(i);
146 result.Add(firtOfPair);
147 result.Add(secondOfPair);
150 return result;