imported hobo11 data... this is a fragment of vmware svn, whatever I had
[tues-crep.git] / sites / hobo11.nerdland.org / coursework / 20070 / dfontanesi / .svn / text-base / Player.cs.svn-base
blob67a5c8b5f7ddf87d00840e2617e9287faabb65fb
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using System.Globalization;\r
5 \r
6 namespace BlackJack\r
7 {\r
8         /// <summary>\r
9         /// Summary description for Player.\r
10         /// </summary>\r
11         public class Player\r
12         {\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
26                 {\r
27                         none = 0,\r
28                         drawToHand = 1,\r
29                         bothHands = 2,\r
30                         outcome = 3\r
31                 }\r
33                 public enum playerType\r
34                 {\r
35                         computer = 0,\r
36                         human = 1\r
37                 }\r
39                 public Player( Point pntlocation, string imageFile, double dblBank, NumericUpDown betControl, playerType type, Strategy strategy, CountMethod method )\r
40                 {\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
49                         bank = dblBank;\r
50                         plyrStrategy = strategy;\r
51                         plyrMethod = method;\r
52                         plyrType = type;\r
53                         plyrBet = betControl;\r
55                         // Start out with one hand, they may split pairs to get two\r
56                         numberOfHands = 1;\r
57                 }\r
59                 ~Player()\r
60                 {\r
61                         try\r
62                         {\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
66                                 plyrBet.Refresh();\r
67                                 plyrBet.Dispose();\r
68                         }\r
69                         catch {}\r
70                 }\r
72                 public void MoveControls( float scaleX, float scaleY )\r
73                 {\r
74                         plyrBet.Left = (int)((location.X +110)* scaleX);\r
75                         plyrBet.Top = (int)((location.Y-20) * scaleY);\r
76                 }\r
78                 public void DrawBackground( Graphics drawingSurface, Card dealerCard )\r
79                 {\r
80                         // Draw different colored backgrounds to match the advice\r
81                         switch( GetAdvice(dealerCard) )\r
82                         {\r
83                                 case Strategy.AdviceType.Hit:\r
84                                         drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_hit"), location);\r
85                                         break;\r
86                                 case Strategy.AdviceType.Stand:\r
87                                         drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_stand"), location);\r
88                                         break;\r
89                                 case Strategy.AdviceType.Split:\r
90                                         drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_split"), location);\r
91                                         break;\r
92                                 case Strategy.AdviceType.DoubleDown:\r
93                                         drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_ddown"), location);\r
94                                         break;\r
95                                 case Strategy.AdviceType.None:\r
96                                         drawingSurface.DrawImage( Resources.GetImage(backgroundImage + "_noadvice"), location);\r
97                                         break;\r
98                                 default:\r
99                                         break;\r
100                         }\r
101                 }\r
103                 public Hand[] GetHands() \r
104                 {\r
105                         return hands;\r
106                 }\r
108                 public int NumberOfHands\r
109                 {\r
110                         get{ return numberOfHands; }\r
111                 }\r
113                 public Hand CurrentHand\r
114                 {\r
115                         // This is just a shortcut to get to the current hand being played\r
116                         get{ return hands[currentHandIndex]; }\r
117                 }\r
119                 public void Won( Hand hand )\r
120                 {\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
124                 }\r
126                 public void Blackjack( Hand hand )\r
127                 {\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
131                 }\r
133                 public void Push( Hand hand )\r
134                 {\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
138                 }\r
140                 public void Reset()\r
141                 {\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
151                         numberOfHands = 1;\r
152                         currentHandIndex = 0;\r
153                 }\r
155                 public void GetWager()\r
156                 {\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
160                         else\r
161                                 hands[0].Wager = (double)plyrBet.Value;\r
163                         // Reduce the bank up front\r
164                         bank -= hands[0].Wager;\r
165                 }\r
167                 public bool DoubleDown( Hand hand )\r
168                 {\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
174                         {\r
175                                 // Reduce the bank\r
176                                 bank -= hand.Wager;\r
178                                 // Double the bet\r
179                                 hand.Wager *= 2;\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
185                                 return true;\r
186                         }\r
187                         return false;\r
188                 }\r
190                 public bool Split()\r
191                 {\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
195                         {\r
196                                 if( hands[0][0].FaceValue == hands[0][1].FaceValue )\r
197                                 {\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
209                                         numberOfHands++;\r
210                                         // Tell the form that the split was successful\r
211                                         return true;\r
212                                 }\r
213                         }\r
214                         return false;\r
215                 }\r
217                 public void NextHand()\r
218                 {\r
219                         // This is just a shortcut to incrementing the hand index\r
220                         currentHandIndex++;\r
221                 }\r
223                 public bool LastHand()\r
224                 {\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
228                 }\r
230                 public double TotalWager()\r
231                 {\r
232                         // The total wager for the player is the sum of all the hand's wagers\r
233                         double wager = 0;\r
235                         foreach( Hand hand in hands )\r
236                         {\r
237                                 wager += hand.Wager;\r
238                         }\r
240                         return wager;\r
241                 }\r
243                 public CountMethod Method\r
244                 {\r
245                         get{ return plyrMethod; }\r
246                         set{ plyrMethod = (CountMethod)value; }\r
247                 }\r
249                 public Strategy CardStrategy\r
250                 {\r
251                         get{ return plyrStrategy; }\r
252                         set{ plyrStrategy = (Strategy)value; }\r
253                 }\r
255                 public playerType Type\r
256                 {\r
257                         get{ return plyrType; }\r
258                         set{ plyrType = value; }\r
259                 }\r
261                 public NumericUpDown Bet\r
262                 {\r
263                         get{ return plyrBet; }\r
264                 }\r
266                 public void DrawHands(Graphics drawingSurface, Player.LabelType labelType, Hand dealerHand, bool currentPlayer)\r
267                 {\r
268                         // This routine is responsible for drawing the player's cards and the appropriate label\r
269                         foreach( Hand hand in hands )\r
270                         {\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
277                                 {\r
278                                         // Draw the appropriate label type in the upper left corner\r
279                                         switch( labelType )\r
280                                         {\r
281                                                 case LabelType.none:\r
282                                                         break;\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
285                                                         break;\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
289                                                         break;\r
290                                                 case LabelType.outcome:\r
291                                                         switch( hand.Outcome( dealerHand, numberOfHands ))\r
292                                                         {\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
296                                                                         break;\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
299                                                                         break;\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
302                                                                         break;\r
303                                                         }\r
304                                                         break;\r
305                                         }\r
307                                         // Increment the drawing position\r
308                                         x += (int)Card.cardSpacing.Width;\r
309                                         y += (int)Card.cardSpacing.Height;\r
311                                         // Draw the cards.\r
312                                         int cardNumber = 0;\r
313                                         foreach( Card card in hand )\r
314                                         {\r
315                                                 if( card != null )\r
316                                                 {\r
317                                                         cardNumber++;\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
321                                                 }\r
322                                         }\r
323                                 }\r
324                         }\r
325                         \r
326                         // Draw the bet\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
329                         // Draw the bank\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
334                         {\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
338                         }\r
340                 }\r
342                 public Strategy.AdviceType GetAdvice( Card dealerCard )\r
343                 {\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
346                 }\r
348                 public void ResetCount( int decks )\r
349                 {\r
350                         if( plyrMethod != null )\r
351                                 plyrMethod.Reset( decks );\r
352                 }\r
354                 public void CountCard( Card newCard )\r
355                 {\r
356                         if( plyrMethod != null )\r
357                                 plyrMethod.CountCard( newCard );\r
358                 }\r
359         }\r