Click here to Skip to main content
15,884,629 members
Articles / Desktop Programming / Windows Forms

Peg That! A Peg Solitaire Game

Rate me:
Please Sign up or sign in to vote.
4.62/5 (7 votes)
30 Mar 2009CPOL3 min read 65.2K   2.1K   43  
An article on creating the classic Peg Solitaire board game
/*************************************************************

    This class was created by:
    Copyright Jason Barry (c) 2008
 
    This code may be used for any legal purpose as long as the
    original copyright above is shown.
    
*************************************************************/

using System;
using System.Drawing;

namespace PegThat
{
    /// <summary>
    /// A single peg piece.
    /// </summary>
    public class Peg
    {
        #region Variables

        private Bitmap image;
        private Rectangle targetRectangle = new Rectangle(0, 0, Settings.PegSize, Settings.PegSize);

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the peg's image.
        /// </summary>
        public Bitmap Image
        {
            get { return image; }
            set { image = value; }
        }

        /// <summary>
        /// Gets or sets the peg's display location.
        /// </summary>
        public Rectangle TargetRectangle
        {
            get { return targetRectangle; }
            set { targetRectangle = value; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new peg.
        /// </summary>
        /// <param name="xLocation">
        /// The peg's x-axis location.
        /// </param>
        /// <param name="yLocation">
        /// The peg's y-axis location.
        /// </param>
        /// <param name="image">
        /// The image for the peg.
        /// </param>
        public Peg(int xLocation, int yLocation, Bitmap image)
        {
            // Set the peg's x-axis location.
            targetRectangle.X = xLocation;

            // Set the peg's y-axis location.
            targetRectangle.Y = yLocation;

            // Set the peg's image.
            this.image = image;
        }

        /// <summary>
        /// Creates an empty peg.
        /// </summary>
        public Peg()
        {

        }

        #endregion

        #region Functions

        /// <summary>
        /// Draws the peg.
        /// </summary>
        public void Draw(Graphics graphicsObject)
        {
            // Only draw the peg if there is an image to draw.
            if (image != null)
                graphicsObject.DrawImage(image, targetRectangle);
        }

        /// <summary>
        /// Gets the peg's location rectangle.
        /// </summary>
        /// <returns>
        /// Returns the peg's location rectangle.
        /// </returns>
        public Rectangle GetBounds()
        {
            return targetRectangle;
        }

        /// <summary>
        /// Sets the peg's location.
        /// </summary>
        /// <param name="xLocation">
        /// The peg's x-axis location.
        /// </param>
        /// <param name="yLocation">
        /// The peg's y-axis location.
        /// </param>
        public void SetLocation(int xLocation, int yLocation)
        {
            // Set the peg's x-axis location.
            targetRectangle.X = xLocation;

            // Set the peg's y-axis location.
            targetRectangle.Y = yLocation;
        }

        #endregion
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I received an associate degree in computer programming in 2005. At work, I create and maintain software that is used in the hospital industry all across the world. I write the software using C# and ASP.NET MVC.

Comments and Discussions