Click here to Skip to main content
15,895,799 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.6K   9.6K   153  
Learn OOD in .NET by examining a Blackjack game
using System;
using System.Drawing;
using System.Windows.Forms;

namespace BlackJack
{
	/// <summary>
	/// The dealer object is fairly simple since he has only one hand of cards
	/// and no bank.  The bank in this game is unlimited and money comes out of 
	/// thin air to pay the players (if they win).
	/// </summary>
	public class Dealer
	{
		private Hand dealerHand;
		private Point location;

		public Dealer(Point pntlocation)
		{
			// Tell the dealer where his origin point is
			location = pntlocation;

			// Create a hand for the dealer and locate its origin at the dealer's origin
			dealerHand = new Hand(new Point(0,0));
		}

		public void Reset()
		{
			// Get a new hand for each round
			dealerHand = new Hand(new Point(0,0));
		}

		public void AddCard( Card card )
		{
			// Take a card
			dealerHand.Add(card);
		}

		public int Total()
		{
			// The dealer's total is his hand's total.  This is just a shortcut to it.
			return dealerHand.Total();
		}
		
		public Hand Hand
		{
			get { return dealerHand; }
		}

		public void DrawHand(Graphics drawingSurface, bool displayDownCard)
		{
			// Draw the dealer's hand on the drawing surface
			bool firstCard = true;

			// Not really necessary for the dealer since he only has one hand, but...
			int x = this.location.X + dealerHand.HandLocation.X;
			int y = this.location.Y + dealerHand.HandLocation.Y;

			// We have to check to see if the dealer has any cards because this routine
			// gets called every time the screen needs to repaint.
			if( dealerHand.Count > 0 )
			{
				// We don't draw the dealer's label until it's time to show their down card
				if( displayDownCard )
					drawingSurface.DrawString( dealerHand.Label(true), new Font("Arial",8,FontStyle.Bold), new SolidBrush(Color.Yellow), x-10, y-10 );
				
				// Move down and to the right
				x += (int)Card.cardSpacing.Width;
				y += (int)Card.cardSpacing.Height;

				// Now draw each of the dealer's cards
				foreach( Card card in dealerHand )
				{
					if( card != null )
					{
						card.Draw( drawingSurface, new Point(x, y), !firstCard || displayDownCard, false, false );
						firstCard = false;
						x += (int)Card.cardSpacing.Width;
						y += (int)Card.cardSpacing.Height;
					}
				}
			}
		}
	}
}

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