2 using System.Drawing;
\r
3 using System.Windows.Forms;
\r
4 using System.Globalization;
\r
9 /// Summary description for Player.
\r
14 private Point location;
\r
15 private double bank;
\r
16 private string backgroundImage;
\r
17 private Strategy plyrStrategy;
\r
18 private CountMethod plyrMethod;
\r
19 private int currentHandIndex = 0;
\r
20 private NumericUpDown plyrBet;
\r
21 private Hand[] hands;
\r
22 private int numberOfHands = 1;
\r
23 private playerType plyrType;
\r
25 public enum LabelType
\r
33 public enum playerType
\r
39 public Player( Point pntlocation, string imageFile, double dblBank, NumericUpDown betControl, playerType type, Strategy strategy, CountMethod method )
\r
41 // Since the game is limited to one split (two hands), just set them up now
\r
42 hands = new Hand[2];
\r
43 hands[0] = new Hand(new Point(0,0));
\r
44 hands[1] = new Hand(new Point(50,0));
\r
46 // Player specific variables
\r
47 location = pntlocation;
\r
48 backgroundImage = imageFile;
\r
50 plyrStrategy = strategy;
\r
51 plyrMethod = method;
\r
53 plyrBet = betControl;
\r
55 // Start out with one hand, they may split pairs to get two
\r
63 // When we destroy the player object, get rid of the betting control too
\r
64 // But first make it invisible in case the garbage collector is slow.
\r
65 plyrBet.Visible = false;
\r
72 public void MoveControls( float scaleX, float scaleY )
\r
74 plyrBet.Left = (int)((location.X +110)* scaleX);
\r
75 plyrBet.Top = (int)((location.Y-20) * scaleY);
\r
78 public void DrawBackground( Graphics drawingSurface, Card dealerCard )
\r
80 // Draw different colored backgrounds to match the advice
\r
81 switch( GetAdvice(dealerCard) )
\r
83 case Strategy.AdviceType.Hit:
\r
84 drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_hit"), location);
\r
86 case Strategy.AdviceType.Stand:
\r
87 drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_stand"), location);
\r
89 case Strategy.AdviceType.Split:
\r
90 drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_split"), location);
\r
92 case Strategy.AdviceType.DoubleDown:
\r
93 drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_ddown"), location);
\r
95 case Strategy.AdviceType.None:
\r
96 drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_noadvice"), location);
\r
103 public Hand[] GetHands()
\r
108 public int NumberOfHands
\r
110 get{ return numberOfHands; }
\r
113 public Hand CurrentHand
\r
115 // This is just a shortcut to get to the current hand being played
\r
116 get{ return hands[currentHandIndex]; }
\r
119 public void Won( Hand hand )
\r
121 // Since the bet is taken from the bank at the beginning of play,
\r
122 // give back twice as much on a win.
\r
123 bank += hand.Wager * 2;
\r
126 public void Blackjack( Hand hand )
\r
128 // Since the bet is taken from the bank at the beginning of play,
\r
129 // give it back plus 1.5 times the wager.
\r
130 bank += hand.Wager + hand.Wager * 1.5;
\r
133 public void Push( Hand hand )
\r
135 // Since the bet is taken from the bank at the beginning of play,
\r
136 // give it back on a push.
\r
137 bank += hand.Wager;
\r
140 public void Reset()
\r
142 // This method is called at the beginning of a new hand.
\r
143 hands = new Hand[2];
\r
144 hands[0] = new Hand(new Point(0,0));
\r
145 hands[1] = new Hand(new Point(50,0));
\r
147 // Take away the betting control during play
\r
148 plyrBet.Visible = false;
\r
150 // Reset variables that keep track of play
\r
152 currentHandIndex = 0;
\r
155 public void GetWager()
\r
157 // Reset the wager to match what the user entered.
\r
158 if( plyrType == playerType.computer && plyrMethod != null )
\r
159 hands[0].Wager = plyrMethod.GetWager( (double)plyrBet.Value );
\r
161 hands[0].Wager = (double)plyrBet.Value;
\r
163 // Reduce the bank up front
\r
164 bank -= hands[0].Wager;
\r
167 public bool DoubleDown( Hand hand )
\r
169 // This method determines whether a double-down is possible and,
\r
170 // if so, doubling the bet.
\r
171 int handTotal = hand.Total();
\r
173 if(((handTotal >=7 && handTotal <=11) || hand.IsSoft) && hand.Count == 2 )
\r
176 bank -= hand.Wager;
\r
181 // Mark the hand as doubled so the last card is drawn at an angle
\r
182 hand.Doubled = true;
\r
184 // Tell the form that we doubled. The form then moves on to the next player.
\r
190 public bool Split()
\r
192 // This method determines if a split is possible and,
\r
193 // if so, create the new hand and double the bet
\r
194 if( hands[0].Count == 2 && numberOfHands == 1 )
\r
196 if( hands[0][0].FaceValue == hands[0][1].FaceValue )
\r
198 // Move the first hand to the left 50 pixels.
\r
199 hands[0].HandLocation = new Point(hands[0].HandLocation.X - 50, hands[0].HandLocation.Y);
\r
200 // Put the second card of the first hand into the first card of the second hand
\r
201 hands[1].Add(hands[0][1]);
\r
202 // Remove the card from the first hand
\r
203 hands[0].RemoveAt( hands[0].Count );
\r
204 // Make the second hand's wager equal to the first
\r
205 hands[1].Wager = hands[0].Wager;
\r
206 // Decrement the bank accordingly
\r
207 bank -= hands[1].Wager;
\r
208 // increase the number of hands
\r
210 // Tell the form that the split was successful
\r
217 public void NextHand()
\r
219 // This is just a shortcut to incrementing the hand index
\r
220 currentHandIndex++;
\r
223 public bool LastHand()
\r
225 // This is a convenient way to determine if the player has any more
\r
226 // hands to draw to.
\r
227 return currentHandIndex + 1 == numberOfHands;
\r
230 public double TotalWager()
\r
232 // The total wager for the player is the sum of all the hand's wagers
\r
235 foreach( Hand hand in hands )
\r
237 wager += hand.Wager;
\r
243 public CountMethod Method
\r
245 get{ return plyrMethod; }
\r
246 set{ plyrMethod = (CountMethod)value; }
\r
249 public Strategy CardStrategy
\r
251 get{ return plyrStrategy; }
\r
252 set{ plyrStrategy = (Strategy)value; }
\r
255 public playerType Type
\r
257 get{ return plyrType; }
\r
258 set{ plyrType = value; }
\r
261 public NumericUpDown Bet
\r
263 get{ return plyrBet; }
\r
266 public void DrawHands(Graphics drawingSurface, Player.LabelType labelType, Hand dealerHand, bool currentPlayer)
\r
268 // This routine is responsible for drawing the player's cards and the appropriate label
\r
269 foreach( Hand hand in hands )
\r
271 // Increment the drawing position
\r
272 int x = location.X + hand.HandLocation.X;
\r
273 int y = location.Y + hand.HandLocation.Y;
\r
275 // Make sure there are cards in the hand to draw.
\r
276 if( hand.Count > 0 )
\r
278 // Draw the appropriate label type in the upper left corner
\r
279 switch( labelType )
\r
281 case LabelType.none:
\r
283 case LabelType.bothHands:
\r
284 drawingSurface.DrawString( hand.Label(numberOfHands==1), new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.Yellow), x-10, y-20 );
\r
286 case LabelType.drawToHand:
\r
287 if( hand == CurrentHand || !currentPlayer )
\r
288 drawingSurface.DrawString( hand.Label(numberOfHands==1), new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.Yellow), x-10, y-20 );
\r
290 case LabelType.outcome:
\r
291 switch( hand.Outcome( dealerHand, numberOfHands ))
\r
293 case Hand.OutcomeType.Won:
\r
294 case Hand.OutcomeType.Blackjack:
\r
295 drawingSurface.DrawString("WON", new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.LimeGreen), x-10, y-20 );
\r
297 case Hand.OutcomeType.Lost:
\r
298 drawingSurface.DrawString("LOST", new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.Crimson), x-10, y-20 );
\r
300 case Hand.OutcomeType.Push:
\r
301 drawingSurface.DrawString("PUSH", new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.Yellow), x-10, y-20 );
\r
307 // Increment the drawing position
\r
308 x += (int)Card.cardSpacing.Width;
\r
309 y += (int)Card.cardSpacing.Height;
\r
312 int cardNumber = 0;
\r
313 foreach( Card card in hand )
\r
318 card.Draw( drawingSurface, new Point(x, y), true, currentPlayer && hand!=CurrentHand, hand.Doubled && cardNumber==3 );
\r
319 x += (int)Card.cardSpacing.Width;
\r
320 y += (int)Card.cardSpacing.Height;
\r
327 drawingSurface.DrawString( "$" + TotalWager().ToString(CultureInfo.InvariantCulture), new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.DarkKhaki), location.X+110, location.Y-20 );
\r
330 drawingSurface.DrawString( "$" + bank.ToString(CultureInfo.InvariantCulture), new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.DarkKhaki), location.X+130, location.Y );
\r
332 // Draw the running card count
\r
333 if( plyrMethod != null )
\r
335 //drawingSurface.DrawString( plyrMethod.MethodName, new Font("Arial",6,FontStyle.Bold), new SolidBrush(Color.DarkKhaki), location.X+140, location.Y+20 );
\r
336 drawingSurface.DrawString( plyrMethod.GetWager((double)plyrBet.Value).ToString("F0"), new Font("Arial",6,FontStyle.Bold), new SolidBrush(Color.DarkKhaki), location.X+140, location.Y+20 );
\r
337 drawingSurface.DrawString( plyrMethod.Count.ToString("F1",CultureInfo.InvariantCulture), new Font("Arial",6,FontStyle.Bold), new SolidBrush(Color.DarkKhaki), location.X+145, location.Y+40 );
\r
342 public Strategy.AdviceType GetAdvice( Card dealerCard )
\r
344 // Get the advice for this player's chosen strategy
\r
345 return hands[currentHandIndex].GetAdvice( dealerCard, plyrStrategy, !(numberOfHands == 2), plyrMethod!=null ? plyrMethod.Count : 0 );
\r
348 public void ResetCount( int decks )
\r
350 if( plyrMethod != null )
\r
351 plyrMethod.Reset( decks );
\r
354 public void CountCard( Card newCard )
\r
356 if( plyrMethod != null )
\r
357 plyrMethod.CountCard( newCard );
\r