Rock Collection – More Generics & Collections

Rocks

RocksInterface

Here is another counting game that makes the use of generics along with some class inheritance built in. Like I said before I’m starting to like working with collections more and more because of the simplicity of dealing with them. It makes for some cleaner code. I also wanted to work in the class inheritance. Granted, yes, this is on a simple scale. However, I think it demonstrates the power of what inheritance can do. My idea for this program was a visualization of adding items to fish tank. Don’t laugh at the graphics aspect of this program. It was all in my head and I wanted to base the interface off my last post. Also note that I’m only going to include code for one of the rock objects. The other are pretty much the same so if you want to see them download the source and take a look.

Cheers
Tails8


MainForm Class


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Rocks
{
    public partial class MainForm : Form
    {
        private RockGame _rockGame = new RockGame();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            //Lambda expression for exiting the program.
            exitButton.Click += (from, ea) => this.Close();

            //Lambda expression for the reset button.
            resetButton.Click += (from, ea) =>
                {
                    _rockGame.Reset();
                    rockPictureBox.Invalidate();
                };

            //Lambda expression for the igneous rock button.
            addIgneousButton.Click += (from, ea) =>
                {
                    _rockGame.Add(new IgneousRock());
                    rockPictureBox.Invalidate();
                };

            //Lambda expression for the sedimentary rock button.
            addSedimentaryButton.Click += (from, ea) =>
                {
                    _rockGame.Add(new SedimentaryRock());
                    rockPictureBox.Invalidate();
                };

            //Lamda expression for the metamorphic rock button.
            addMetamorphicButton.Click += (from, ea) =>
                {
                    _rockGame.Add(new MetamorphicRock());
                    rockPictureBox.Invalidate();
                };

            //Lambda expression for the add sand button.
            addSandButton.Click += (from, ea) =>
                {
                    _rockGame.Add(new Sand());
                    rockPictureBox.Invalidate();
                };

            //Lambda expression for the add dirt button.
            addDirtButton.Click += (from, ea) =>
                {
                    _rockGame.Add(new Dirt());
                    rockPictureBox.Invalidate();
                };

            //Lambda expression for the rock picture box display.
            rockPictureBox.Paint += (from, ea) =>
                {
                    _rockGame.Update(ea.Graphics, rockPictureBox.ClientSize);       //This updates the picturebox to show the rock.
                    rocksAmountValueLabel.Text = _rockGame.TotalRocks.ToString();   //Updating the total amount of works.
                    igneousRockValueLabel.Text = _rockGame.CountIgneousRocks.ToString();   //Updating the igneous rock counter.
                    sedimentaryRockValueLabel.Text = _rockGame.CountSedimentaryRocks.ToString();    //Updating the sedimentary rock counter.
                    metamorphicRockValueLabel.Text = _rockGame.CountMetamorphicRocks.ToString();    //Updating the metamorphic rock counter.
                };

            //PictureBox MouseUp lambda expression used to remove the user selected rocks.
            rockPictureBox.MouseUp += (from, ea) =>
                {
                    _rockGame.Remove(ea.Location);
                    rockPictureBox.Invalidate();
                };

        }
    }
}

 

RockGame Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Rocks
{
    class RockGame
    {
        //Private class variables.
        private RockCounter _myRockObjects = new RockCounter();
        private RockCounter _myIgneousRock = new RockCounter();
        private RockCounter _mySedimentaryRock = new RockCounter();
        private RockCounter _myMetamorphicRock = new RockCounter();
        private RockCounter _mySand = new RockCounter();
        private RockCounter _myDirt = new RockCounter();
        private int _rockHeight = 20;

        //Properties / Accessors.
        public int TotalRocks { get { return _myRockObjects.RockCount; } }
        public int CountIgneousRocks { get { return _myIgneousRock.RockCount; } }
        public int CountSedimentaryRocks { get { return _mySedimentaryRock.RockCount; } }
        public int CountMetamorphicRocks { get { return _myMetamorphicRock.RockCount; } }

        //Public methods.
        public void Add(RockObjects rocks)
        {
            _myRockObjects.Add(rocks);      //Adding the rock to the general collection list.
            switch (rocks.GetType().Name)
            {
                case "IgneousRock":
                    _myIgneousRock.Add(rocks as IgneousRock);       //Calling the method to add the rock.
                    break;
                case "SedimentaryRock":
                    _mySedimentaryRock.Add(rocks as SedimentaryRock);
                    break;
                case "MetamorphicRock":
                    _myMetamorphicRock.Add(rocks as MetamorphicRock);
                    break;
                case "Sand":
                    _mySand.Add(rocks as Sand);
                    break;
                case "Dirt":
                    _myDirt.Add(rocks as Dirt);
                    break;
            }
        }

        public void Remove(Point location)
        {
            //Using a foreach loop to which rock is selected.
            foreach (RockObjects rocks in _myRockObjects)
            {
                if (rocks.Hit(location))
                {
                    switch (rocks.GetType().Name)
                    {
                        case "IgneousRock":
                            _myIgneousRock.Remove(rocks as IgneousRock);    //Calling the method to remove the rock.
                            break;
                        case "SedimentaryRock":
                            _mySedimentaryRock.Remove(rocks as SedimentaryRock);
                            break;
                        case "MetamorphicRock":
                            _myMetamorphicRock.Remove(rocks as MetamorphicRock);
                            break;
                        case "Sand":
                            _mySand.Remove(rocks as Sand);
                            break;
                        case "Dirt":
                            _myDirt.Remove(rocks as Dirt);
                            break;
                    }
                    _myRockObjects.Remove(rocks);   //Removing the rock from the general collection list.
                    break;
                }
            }
        }

        //Method used to reset the collections.
        public void Reset()
        {
            _myRockObjects.Reset();
            _myIgneousRock.Reset();
            _mySedimentaryRock.Reset();
            _myMetamorphicRock.Reset();
            _mySand.Reset();
            _myDirt.Reset();
        }

        public void Update(Graphics graphics, Size boardsize)
        {
            //Using a for loop to allow updating.
            for (int i = 0; i < _myRockObjects.RockCount; i++)
            {
                //Creating a rock object each time the loop runs to pull data from.
                RockObjects rocks = _myRockObjects[i];

                //Set the added rock's location.
                int startingY = boardsize.Height - (i * (_rockHeight + 1)) - _rockHeight - 1;
                rocks.Location = new Point(1, startingY);
                rocks.Dimensions = new Size(boardsize.Width - 2, _rockHeight);

                //Draw the selected rock.
                using (SolidBrush solidBrush = new SolidBrush(rocks.Color))
                    graphics.FillRectangle(solidBrush, new Rectangle(rocks.Location, rocks.Dimensions));

                //Drawing the text label for each rock.
                RectangleF boundingRectangle = new RectangleF(rocks.Location.X, rocks.Location.Y,
                    rocks.Dimensions.Width, rocks.Dimensions.Height);
                using (Font font = new Font("Arial", 12, FontStyle.Bold))
                using (StringFormat stringFormat = new StringFormat())
                using (SolidBrush brush = new SolidBrush(Color.White))
                {
                    //Aliging the text to be in the middle of the rock graphic.
                    stringFormat.Alignment = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;
                    graphics.DrawString(rocks.MaterialType, font, brush, boundingRectangle, stringFormat);
                }
            }
        }
    }
}

 

RockCounter Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rocks
{
    public class RockCounter where T : RockObjects
    {
        //Private class variables.
        private List _rockList;

        //Accessors / Properties.
        public int RockCount { get { return _rockList.Count; } }

        //Indexers
        public T this[int index]
        {
            get
            {
                if (_rockList.Count > 0 && index < _rockList.Count)
                    return _rockList[index];
                else
                    return default(T);
            }
        }

        //Public enumerator
        public IEnumerator GetEnumerator()
        {
            return _rockList.GetEnumerator();
        }

        //Constructors.
        public RockCounter()
        {
            _rockList = new List();
        }

        //Public methods.
        //Method used to add rocks to a collection.
        public void Add(T rock)
        {
            _rockList.Add(rock);
        }

        //Method used to remove rocks from a collection.
        public void Remove(T rock)
        {
            T foundRock = _rockList.Find(item => item == rock);
            if (foundRock != null)
            {
                _rockList.Remove(foundRock);
            }
        }

        //Method used to clear the collection.
        public void Reset()
        {
            _rockList.Clear();
        }
    }
}

 

RockObjects Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Rocks
{
    public class RockObjects
    {
        //Private class variables.
        private Point _location;
        private Size _dimensions;
        private string _materialType;
        private Color _color;

        //Accessors / Properties
        public Point Location { get { return _location; } set { _location = value; } }
        public Size Dimensions { get { return _dimensions; } set { _dimensions = value; } }
        public string MaterialType { get { return _materialType; } set { _materialType = value; } }
        public Color Color { get { return _color; } set { _color = value; } }

        //Constructors
        public RockObjects(string material, Color color)
        {
            _materialType = material;
            _color = color;
        }

        //Public methods.
        public bool Hit(Point location)
        {
            return new Rectangle(_location, _dimensions).Contains(location);
        }
    }
}

One Sample Rock Type Class – IgneousRock Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Rocks
{
    public class IgneousRock : RockObjects
    {
        public IgneousRock() : base("Igneous", Color.LightSlateGray) { }
    }
}

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s