To support this image I had to refactor the Card class we created in the last post. This was done in 5 minutes, thanks to the unit testing.
The two enums have been changed to inherently support the image:
public enum SuitEnum { Clubs , Diamonds , Hearts , Spades } public enum ValueEnum { Ace , _2 , _3 , _4 , _5 , _6 , _7 , _8 , _9 , _10 , Jack , Queen , King } |
Also, the ToString() function was modified to support the changes:
public override string ToString() { string tempString = string.Empty; switch (this.value) { case ValueEnum._10: tempString = "T"; break; case ValueEnum.Jack: tempString = "J"; break; case ValueEnum.Queen: tempString = "Q"; break; case ValueEnum.King: tempString = "K"; break; case ValueEnum.Ace: tempString = "A"; break; default: tempString = ((int)this.value + 1).ToString(); break; } switch (this.suit) { case SuitEnum.Clubs: tempString += "c"; break; case SuitEnum.Diamonds: tempString += "d"; break; case SuitEnum.Hearts: tempString += "h"; break; case SuitEnum.Spades: tempString += "s"; break; } return tempString; } |
Run the unit tests, and see: the tests succeed!
Next, create a new C# Windows Application called Poker.WinApp, add a reference to Poker.Common and add the image to the file. Click the image to view the properties and make the Build Action: Embedded Resource.
Add a new function to the form:
private Bitmap getCard(Card c) { Assembly a = Assembly.GetExecutingAssembly(); using (Image img = Image.FromStream(a.GetManifestResourceStream("Poker.WinApp.cards.png"))) { using (Bitmap bmp = new Bitmap(img)) { int w = bmp.Width / 13; int h = bmp.Height / 5; int posX = (int)c.Value * w; int posY = (int)c.Suit * h; return bmp.Clone(new Rectangle(posX, posY, w, h), System.Drawing.Imaging.PixelFormat.DontCare); } } } |
To test this, add a button and a picturebox to the form and add the following code to the Click event of the button:
Card c = new Card(Card.SuitEnum.Diamonds, Card.ValueEnum._6); this.pictureBox1.Image = getCard(c); |
Start the WinApp, click the button and voila!
Geen opmerkingen:
Een reactie posten