-
Notifications
You must be signed in to change notification settings - Fork 919
/
Copy pathSnap.cs
188 lines (159 loc) · 4.11 KB
/
Snap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using SwinGameSDK;
#if DEBUG
using NUnit.Framework;
#endif
namespace CardGames.GameLogic
{
/// <summary>
/// The Snap card game in which the user scores a point if they
/// click when the rank of the last two cards match.
/// </summary>
public class Snap
{
// Keep only the last two cards...
private readonly Card[] _topCards = new Card[2];
// Have a Deck of cards to play with.
private readonly Deck _deck;
// Use a timer to allow the game to draw cards at timed intervals
private readonly Timer _gameTimer;
// The amount of time that must pass before a card is flipped?
private int _flipTime = 1000;
// the score for the 2 players
private int[] _score = new int[2];
private bool _started = false;
/// <summary>
/// Create a new game of Snap!
/// </summary>
public Snap ()
{
_deck = new Deck ();
_gameTimer = SwinGame.CreateTimer ();
}
/// <summary>
/// Gets the card on the top of the "flip" stack. This card will be face up.
/// </summary>
/// <value>The top card.</value>
public Card TopCard
{
get
{
return _topCards [1];
}
}
/// <summary>
/// Indicates if there are cards remaining in the Snap game's Deck.
/// The game is over when there are no cards remaining.
/// </summary>
/// <value><c>true</c> if cards remain; otherwise, <c>false</c>.</value>
public bool CardsRemain
{
get { return _deck.CardsRemaining > 0; }
}
/// <summary>
/// Determines how many milliseconds need to pass before a new card is drawn
/// and placed on the top of the game's card stack.
/// </summary>
/// <value>The flip time.</value>
public int FlipTime
{
get { return _flipTime; }
set { _flipTime = value; }
}
/// <summary>
/// Indicates if the game has already been started. You can only start the game once.
/// </summary>
/// <value><c>true</c> if this instance is started; otherwise, <c>false</c>.</value>
public bool IsStarted
{
get { return _started; }
}
/// <summary>
/// Start the Snap game playing!
/// </summary>
public void Start()
{
if ( ! IsStarted ) // only start if not already started!
{
_started = true;
_deck.Shuffle (); // Return the cards and shuffle
FlipNextCard (); // Flip the first card...
_gameTimer.Start();
}
}
public void FlipNextCard()
{
if (_deck.CardsRemaining > 0) // have cards...
{
_topCards [0] = _topCards [1]; // move top to card 2
_topCards [1] = _deck.Draw (); // get a new top card
_topCards[1].TurnOver(); // reveal card
}
}
/// <summary>
/// Update the game. This should be called in the Game loop to enable
/// the game to update its internal state.
/// </summary>
public void Update()
{
if (_gameTimer.Ticks > _flipTime)
{
_gameTimer.Reset ();
FlipNextCard ();
}//automatically slip cards!
}
/// <summary>
/// Gets the player's score.
/// </summary>
/// <value>The score.</value>
public int Score(int idx)
{
if ( idx >= 0 && idx < _score.Length )
return _score[idx];
else
return 0;
}
/// <summary>
/// The player hit the top of the cards "snap"! :)
/// Check if the top two cards' ranks match.
/// </summary>
public void PlayerHit (int player)
{
//TODO: consider deducting score for miss hits???
if ( player >= 0 && player < _score.Length && // its a valid player
IsStarted && // and the game is started
_topCards [0] != null && _topCards [0].Rank == _topCards [1].Rank) // and its a match
{
_score[player]++;
//TODO: consider playing a sound here...
}
// stop the game...
_started = false;
_gameTimer.Stop ();
}
#region Snap Game Unit Tests
#if DEBUG
public class SnapTests
{
[Test]
public void TestSnapCreation()
{
Snap s = new Snap();
Assert.IsTrue(s.CardsRemain);
Assert.IsNull (s.TopCard);
}
[Test]
public void TestFlipNextCard()
{
Snap s = new Snap();
Assert.IsTrue(s.CardsRemain);
Assert.IsNull (s.TopCard);
s.FlipNextCard ();
Assert.IsNull (s._topCards [0]);
Assert.IsNotNull (s._topCards [1]);
}
}
#endif
#endregion
}
}