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

namespace BlackJack
{
	/// <summary>
	/// Summary description for StrategyForm.
	/// </summary>
	public class StrategyForm : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private Player player;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnuOptions;
		private System.Windows.Forms.MenuItem mnuAlwaysOnTop;
		private Card dealerCard;

		public StrategyForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.mnuOptions = new System.Windows.Forms.MenuItem();
			this.mnuAlwaysOnTop = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mnuOptions});
			// 
			// mnuOptions
			// 
			this.mnuOptions.Index = 0;
			this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.mnuAlwaysOnTop});
			this.mnuOptions.Text = "Options";
			// 
			// mnuAlwaysOnTop
			// 
			this.mnuAlwaysOnTop.Checked = true;
			this.mnuAlwaysOnTop.Index = 0;
			this.mnuAlwaysOnTop.Text = "Always On Top";
			this.mnuAlwaysOnTop.Click += new System.EventHandler(this.mnuAlwaysOnTop_Click);
			// 
			// StrategyForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.AutoScroll = true;
			this.ClientSize = new System.Drawing.Size(363, 501);
			this.ControlBox = false;
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
			this.MaximizeBox = false;
			this.Menu = this.mainMenu1;
			this.MinimizeBox = false;
			this.Name = "StrategyForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "StrategyForm";
			this.TopMost = true;
			this.Resize += new System.EventHandler(this.StrategyForm_Resize);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.StrategyForm_Paint);

		}
		#endregion

		private void StrategyForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			StringFormat myFormat = new StringFormat();
			myFormat.Alignment = StringAlignment.Center;

			if( player == null )
			{
				e.Graphics.DrawString("There is no current player.", new Font("Arial",9), Brushes.Brown, new Point(this.Width/2,this.Height/3), myFormat);
			}
			else if( player.CardStrategy == null )
			{
				e.Graphics.DrawString("The current player has no strategy.", new Font("Arial",9), Brushes.Brown, new Point(this.Width/2,this.Height/3), myFormat);
			}
			else
			{
				player.CardStrategy.Draw( e.Graphics, new Rectangle(0,0,this.Width/12,this.Height/48), player.CurrentHand, dealerCard, player.Method.Count );
			}
		}

		private void StrategyForm_Resize(object sender, System.EventArgs e)
		{
			this.Refresh();
		}

		private void mnuAlwaysOnTop_Click(object sender, System.EventArgs e)
		{
			mnuAlwaysOnTop.Checked = !mnuAlwaysOnTop.Checked;
			this.TopMost = mnuAlwaysOnTop.Checked;
		}

		public Player CurrentPlayer
		{
			get
			{
				return player;
			}
			set
			{
				player = value;
			}
		}

		public Card DealerCard
		{
			get
			{
				return dealerCard;
			}
			set
			{
				dealerCard = value;
			}
		}
	}
}

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