Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

Blackjack - a real world OOD example

Rate me:
Please Sign up or sign in to vote.
4.87/5 (47 votes)
18 Jul 20076 min read 236.5K   9.6K   153  
Learn OOD in .NET by examining a Blackjack game
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Reflection;
using System.Resources;
using System.Collections;
using System.Globalization;

namespace BlackJack
{
	/// <summary>
	/// Summary description for Card.
	/// </summary>
	public class Card : ICloneable
	{
		private CardType cardType;
		private Suits cardSuit;
		private Image image;
		private int value;
		private int trueValue;
		public static SizeF cardSpacing;
		public static SizeF cardSize;

		public enum CardType
		{
			Ace = 0,
			Two = 1,
			Three = 2,
			Four = 3,
			Five = 4,
			Six = 5,
			Seven = 6,
			Eight = 7,
			Nine = 8,
			Ten = 9,
			Jack = 10,
			Queen = 11,
			King = 12
		}

		public enum Suits 
		{
			Clubs = 0,
			Diamonds = 1,
			Hearts = 2,
			Spades = 3
		}

		public Card( CardType type, Suits suit )
		{
			cardSuit = suit;
			cardType = type;
			value = ((int)suit * 13) + (int)cardType + 1;
			image = Resources.GetImage(value);
			trueValue = (int)cardType;
			if( trueValue > 9 )
				trueValue = 9;

			cardSize = image.PhysicalDimension;
			cardSpacing.Width = cardSize.Width / 5;
			cardSpacing.Height = cardSize.Height / 7;
		}

		public void Draw( Graphics drawingSurface, Point location, bool show, bool dim, bool doubledownCard )
		{
			float opaqueness = dim ? .5F : 1;
			float rotationAngle = doubledownCard ? 45 : 0;
			float[][] ptsArray ={	new float[] {1, 0, 0, 0, 0},
									new float[] {0, 1, 0, 0, 0},
									new float[] {0, 0, 1, 0, 0},
									new float[] {0, 0, 0, opaqueness, 0}, 
									new float[] {0, 0, 0, 0, 1}};
			ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
			ImageAttributes imgAttributes = new ImageAttributes();
			imgAttributes.SetColorMatrix(clrMatrix,	ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

			GraphicsState previousState = drawingSurface.Save();

				Point Pcenter = new Point( location.X + image.Width / 2, location.Y + image.Height / 2 );
				drawingSurface.TranslateTransform( Pcenter.X, Pcenter.Y );
				drawingSurface.RotateTransform( rotationAngle );

				//			drawingSurface.DrawImage(shadow, new Rectangle( -curImage.Width/2, -curImage.Height/2 , shadow.Width, shadow.Height ), 0, 0, shadow.Width, shadow.Height, GraphicsUnit.Pixel, imgAttributesShadow );
				drawingSurface.DrawImage( show ? image : Shoe.BackImage, new Rectangle( -image.Width/2, -image.Height/2 , image.Width, image.Height ), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imgAttributes );

			drawingSurface.Restore( previousState );
		}
		public int Value 
		{
			get { return value; }
		}

		public CardType FaceValue
		{
			get { return cardType; }
		}

		public int TrueValue
		{
			get { return trueValue; }
		}

		public Image Image 
		{
			get { return image; }
		}

		public Suits Suit 
		{
			get { return cardSuit; }
		}	

		//	ICloneable
		Object ICloneable.Clone() 
		{
			return new Card(cardType, cardSuit);
		}

		public Card Clone() 
		{
			return new Card(cardType, cardSuit);
		}

		public static Card Clone(Card card) 
		{
			return new Card(card.FaceValue, card.Suit);
		}
	}

	//	A shared reference to access images and other resources.
	internal abstract class Resources 
	{
		private static ResourceManager images;
		
		static Resources() 
		{
			images = new ResourceManager("Blackjack.Images", Assembly.GetExecutingAssembly());
		}

		public static ResourceManager Images 
		{
			get { return images; }
		}

		public static Image GetImage(int imageId) 
		{
			return (Image)images.GetObject(imageId.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture );
		}

		public static Image GetImage(string imageId) 
		{
			return (Image)images.GetObject(imageId, CultureInfo.InvariantCulture);
		}
	} // Resources
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I'm a software engineer and consultant working in San Diego, California. I began using .NET during the early alpha releases since I worked for a Microsoft subsidiary then, and now I've focused my career on .NET technologies.

There are a lot of .NET sites out there now, but The Code Project is still top of the heap.

Comments and Discussions