2 using System
.Collections
.Generic
;
4 using Microsoft
.Xna
.Framework
;
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;
21 // Constructs a new Playground
24 m_currentPlayer
= PlayerIndex
.One
;
26 m_random
= new Random();
32 // Selects a card as the currentPlayer, returns the next player
36 // The x coordinate of the card
38 // The y coordinate of the card
40 // The z coordinate of the card
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
56 if (m_currentCard
.Equals(pickedCard
))
58 // !! Player selected the correct card
59 return m_currentPlayer
;
63 // !! Player selected the wrong card
64 return GetNextPlayer();
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
);
81 // Finds the next player and returns it
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
;
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
);
114 // Gets a random card from allCards, removes it, and retuns it.
118 // The list from which a card will be picked
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
);
132 // Generates a list of Cards, each card is in the list twice
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
);