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

Building a puzzle game using Generics

Rate me:
Please Sign up or sign in to vote.
3.93/5 (7 votes)
8 Sep 200410 min read 38.8K   975   16  
This article describes how to build a simple game using a doubly linked list
#region == Application Information ==
//
// Author: Carlos R Lacerda
// email: carlos@byteshift.com
// website: www.byteshift.com
// Date: 08/15/2004
// version: 1.0.0.0
// All rights reserved
//
#endregion

#region Using directives

using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

#endregion

namespace WinGenericsDemo
{
    /// <summary>
    /// This is the game item class
    /// </summary>
    public partial class GameItems
    {
        #region Add Methods
        /// <summary>
        /// Add the selected piece to the target head list
        /// </summary>
        /// <returns>Returns null or the control added</returns>
        public PictureBox AddPieceToHead()
        {
            Item selectedItem = null;
            // verify if there is a selected item in source list
            selectedItem = GetSelectedItem(sourceListItems);
            // if null, need select an item
            if (selectedItem != null)
            {
                // add item to target
                targetListItems.AddHead(selectedItem);
                // remove from source list
                sourceListItems.Remove(selectedItem);

                // try to shift all items to accomodete the new added
                Point nextPosition = solutionPosition;
                // update the list
                UpdatePositionForEntireList(targetListItems, nextPosition);
            }
            else
                return null;
            return selectedItem.PicItem;
        }

        /// <summary>
        /// Add the selected piece to the target tail list
        /// </summary>
        /// <returns>Returns null or the control added</returns>
        public PictureBox AddPieceToTail()
        {
            Item selectedItem = null;
            // verify if there is a selected item in source list
            selectedItem = GetSelectedItem(sourceListItems);
            // if null, need select an item
            if (selectedItem != null)
            {
                // add item to tail
                targetListItems.AddTail(selectedItem);
                // remove from source list
                sourceListItems.Remove(selectedItem);
                // unselect the item
                selectedItem.IsSelected = false;
                // because the new item goes to the end list , there is
                // no need to shift the others
                if (targetListItems.Count == 1)
                    selectedItem.UpdatePosition(solutionPosition);
                else
                {
                    // there is more than one item. Need to find the sibiling
                    Item sibiling = targetListItems.GetSiblingItem(selectedItem, false);
                    selectedItem.UpdatePosition(UpdateNextPosition(sibiling));
                }
            }
            else
                return null;
            return selectedItem.PicItem;
        }

        /// <summary>
        /// Add the selected source piece before the selected target
        /// </summary>
        /// <returns>Returns null or the control added</returns>
        public PictureBox AddPieceBefore()
        {
            Item selectedSrcItem = null;
            Item selectedTgtItem = null;

            // get the selected item in source list
            selectedSrcItem = GetSelectedItem(sourceListItems);
            // get the selected item in target list
            selectedTgtItem = GetSelectedItem(targetListItems);

            // if null, need select an item
            if (selectedSrcItem != null && selectedTgtItem != null)
            {
                // add item to tail
                targetListItems.AddBeforeItem(selectedTgtItem, selectedSrcItem);
                // remove from source list
                sourceListItems.Remove(selectedSrcItem);
                // unselect the item
                selectedSrcItem.IsSelected = false;
                selectedTgtItem.IsSelected = false;

                // try to shift all items to accomodete the new added
                Point nextPosition = solutionPosition;
                // update the list
                UpdatePositionForEntireList(targetListItems, nextPosition);
            }
            else
                return null;
            return selectedSrcItem.PicItem;
        }

        /// <summary>
        /// Add the selected source piece after the selected target
        /// </summary>
        /// <returns>Returns null or the control added</returns>
        public PictureBox AddPieceAfter()
        {
            Item selectedSrcItem = null;
            Item selectedTgtItem = null;

            // get the selected item in source list
            selectedSrcItem = GetSelectedItem(sourceListItems);
            // get the selected item in target list
            selectedTgtItem = GetSelectedItem(targetListItems);

            // if null, need select an item
            if (selectedSrcItem != null && selectedTgtItem != null)
            {
                // add item to tail
                targetListItems.AddAfterItem(selectedTgtItem, selectedSrcItem);
                // remove from source list
                sourceListItems.Remove(selectedSrcItem);
                // unselect the item
                selectedSrcItem.IsSelected = false;
                selectedTgtItem.IsSelected = false;

                // try to shift all items to accomodete the new added
                Point nextPosition = solutionPosition;
                // update the list
                UpdatePositionForEntireList(targetListItems, nextPosition);
            }
            else
                return null;
            return selectedSrcItem.PicItem;
        }
        #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 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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions