黑马程序员 wp7,连连看项目分析

来源:互联网 发布:java四种循环 编辑:程序博客网 时间:2024/05/18 00:43

---------------------- Windows Phone 7手机开发  Net培训、期待与您交流! ----------------------

连连看的项目的基本思路

我这里 有一个类 承载了每一个项目的图片的项

 下面需要一个controller来控制游戏的引擎的类 

  这个类有几个关键方法

一个setlevel 方法与fillGrid 用于每个级别的连连看的初始化

一个ClearCache方法用于缓存的方法 提高了游戏的速度

一个IsCanLink方法 判断两点是否相连的方法

CheckLineX、CheckLineY、CheckLBRT、CheckRBRLT判断上中下是否可连

  我们开始一个简单的游戏连连看项目的分析  

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using ZCW.LLK.POJO;
using System.Collections.Generic;

namespace ZCW.LLK.BLL
{
    public class Engine
    {
        //游戏级别数
        public const int LevelCount = 7;
        //项大小
        public static Size ItemSize = new Size(60, 60);
        //已经选取成功的点个数
        static int SelectedCount = 0;
        enum ItemDer
        {
            LineX,//水平X方向
            LineY,//垂直Y方向
            LTRB,//左上右下
            //左下右上
            LBRT
        };

        /// <summary>
        /// 画布格子的最大行列数
        /// </summary>
        public static Point MaxBackGridSplits
        {
            get
            {
            return new Point(10,6);
            }
        }
        ///<summary>
        /// 当前选中的点
        /// </summary>
        static POJO.ImgItem CurSelectedItem = null;
        /// <summary>
        /// 当前级别的格子数
        /// SLConfig.MaxBackGridSplits.X*MaxBackGridSplits.Y
        /// </summary>
        static double ItemCount
        {
            get;
            set;
        }
        //public delegate void ItemLinked(POJO.ImgItem item);
        //public static event ItemLinked ItemLinkedHanlder;

        /// <summary>
        /// 当前级别
        /// </summary>
        public static int CurLevelIndex
        {
            get;
            set;
        }
        /// <summary>
        /// /游戏背景画布
        /// </summary>
        public static Canvas GameBackGrid
        {
            get;
            set;
        }
        //当前级别对象
        public static POJO.GameLevel CurLevel = null;
        /// <summary>
        /// 所有的方块
        /// </summary>
        public static POJO.ImgItem[][] AllItems
        {
            get;
            set;
        }
        //public delegate void delegateRefreshGrid();
        //public static event delegateRefreshGrid RefreshGridHanlder;

        /// <summary>
        /// 游戏完成
        /// </summary>
        /// <param name="level"></param>
        public delegate void delegateItemCompleted(POJO.GameLevel level);
        public static event delegateItemCompleted GameCompletedHandler;
        /// <summary>
        /// 初始化数据
        /// </summary>
        public static void InitEngine()
        {
            ClearItemsCach();
        }
        /// <summary>
        /// 设置相应的级别
        /// </summary>
        public delegate void delegateSetLevel();
        /// <summary>
        /// 设置相应的级别的事件
        /// </summary>
        public static event delegateSetLevel SetLevelEventHandler;
        /// <summary>
        /// 设置级别
        /// </summary>
        /// <param name="level"></param>
        public static void SetLevel(int level)
        {
            int TotalScore = 0;
            if (CurLevel != null)
            {
                TotalScore = CurLevel.Score;
            }

            if (level == 0)
            {
                level = 1;
            }
            else if (level > LevelCount)
            {
                TotalScore = 0;
            }

            //初始化游戏级别
            CurLevel = new GameLevel();
            CurLevel.Level = level;
            CurLevel.BackGridSplits = MaxBackGridSplits;
            CurLevel.Score = TotalScore;
            //初始化图片
            //int imgIndex = Config.SLConfig.AllGameLevelImages.Length < level ? Config.SLConfig.AllGameLevelImages.Length - 1 : level - 1;
            CurLevel.ItemImages = new ImageBrush[CurLevel.BaseImgCount];//[Config.SLConfig.AllGameLevelImages[imgIndex].Length];
          
            for (int i = 0; i < CurLevel.ItemImages.Length; i++)
            {
                if (CurLevel.ItemImages[i] == null)
                {
                    CurLevel.ItemImages[i] = new ImageBrush();
                }
                CurLevel.ItemImages[i].ImageSource = new BitmapImage(new Uri("/ZCW.LLK;component/Images/" + CurLevel.Level + "/" + (i + 1) + ".jpg", UriKind.Relative));//(new Uri(Config.SLConfig.AllGameLevelImages[level - 1][i], UriKind.Relative));
            }

            ItemCount = CurLevel.BackGridSplits.X * CurLevel.BackGridSplits.Y;
            SelectedCount = 0;
            if (SetLevelEventHandler != null)
            {
                SetLevelEventHandler();
            }
        }

        /// <summary>
        /// 初始化背景
        /// </summary>
        public static void InitGrid()
        {
            double w = GameBackGrid.ActualWidth / MaxBackGridSplits.X;
            double h = GameBackGrid.ActualHeight / MaxBackGridSplits.Y;
            ItemSize = new Size(w,h);
        }

        /// <summary>
        /// 填充图标
        /// </summary>
        public static void FillGrid()
        {
            GameBackGrid.Width = CurLevel.BackGridSplits.X * ItemSize.Width;
            GameBackGrid.Height = CurLevel.BackGridSplits.Y * ItemSize.Height;
            List<POJO.ImgItem> CreatedItems = new List<ImgItem>();

            Random rand = new Random();
            for (int i = 0; i < ItemCount / 2; i++)
            {
                int index = rand.Next(CurLevel.ItemImages.Length - 1);
                POJO.ImgItem item = new POJO.ImgItem();

                item.ItemImg = CurLevel.ItemImages[index];
                POJO.ImgItem item2 = new POJO.ImgItem() { ItemImg = item.ItemImg };
                item.SelectIt += item_SelectIt;
                item2.SelectIt += item_SelectIt;

                CreatedItems.Add(item);
                CreatedItems.Add(item2);
            }

            for (int i = 0; i < ItemCount; i++)
            {
                int y = (int)(i / CurLevel.BackGridSplits.X);
                int x = (int)(i % CurLevel.BackGridSplits.X);
                int index = rand.Next(CreatedItems.Count - 1);
                POJO.ImgItem item = CreatedItems[index];
                item.Width = ItemSize.Width;
                item.Height = ItemSize.Height;
                item.Location = new Point(x, y);
                item.Parent = GameBackGrid;
                item.Show();
                AllItems[x][y] = item;
                CreatedItems.Remove(item);
            }
        }   

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
       
        /// <summary>
        /// 重新排列
        /// </summary>
        public static void RefreshGrid()
        {
            //if (RefreshGridHanlder != null)
            //{
            //    RefreshGridHanlder();
            //}

            CurSelectedItem = null;
            CurSelectedItem = null;
            var lastItems = GetExistsItems();//获取没有消掉的点
            if (lastItems.Count == 0)
            {
                FillGrid();
                return;
            }
            //ClearItemsCach();
            Random rand = new Random();

            //项位置重新排序算法
            //for (int i = 0; i < ItemCount; i++)
            //{
            //    if (lastItems.Count <= 0) break;

            //    int x = (int)(i % CurLevel.BackGridSplits.X);
            //    int y = (int)(i / CurLevel.BackGridSplits.X);              

            //    int index = rand.Next(lastItems.Count - 1);
            //    POJO.ImgItem item = lastItems[index];
            //    item.Hide();
            //    lastItems.Remove(item);

            //    item = new LLH.POJO.ImgItem() { ItemImg=item.ItemImg };
            //    item.Location = new Point(x,y );
            //    item.BackGround = GameBackGrid;
            //    item.SelectIt += item_SelectIt;
            //    item.Show();
            //    AllItems[x][y] = item;
            //}

            if (lastItems.Count<=0)
            {
                return;
            }
            //只重新排序项.位置不变算法
            for (int x = 0; x < AllItems.Length; x++)
            {
                for (int y = 0; y < AllItems[x].Length; y++)
                {
                    if (AllItems[x][y] == null) continue;
                    int index = rand.Next(lastItems.Count-1);
                    ImgItem item = lastItems[index];
                    item.Hide();
                   
                    lastItems.Remove(item);

                    item = new POJO.ImgItem() { ItemImg = item.ItemImg };
                    item.Location = new Point(x, y);
                    item.Width = ItemSize.Width;
                    item.Height = ItemSize.Height;
                    item.Parent = GameBackGrid;
                    item.SelectIt += item_SelectIt;
                    item.Show();
                    AllItems[x][y] = item;
                }
            }

            //每刷新一次减十分
            CurLevel.Score = CurLevel.Score - CurLevel.BaseImgCount;
            if (CurLevel.Score<0)
            {
                CurLevel.Score = 0;
            }
        }

        /// <summary>
        /// 初始化点缓存
        /// </summary>
        public static void ClearItemsCach()
        {
            AllItems = new POJO.ImgItem[(int)CurLevel.BackGridSplits.X][];
            for (int i = 0; i < CurLevel.BackGridSplits.X; i++)
            {
                AllItems[i] = new POJO.ImgItem[(int)CurLevel.BackGridSplits.Y];
            }
            AllItems = new ImgItem[(int)CurLevel.BackGridSplits.X][];
            for (int i = 0; i < CurLevel.BackGridSplits.X; i++)
            {
                AllItems[i]=new ImgItem[(int)CurLevel.BackGridSplits.Y];
            }
        }

        /// <summary>
        /// 获取没有消掉的点
        /// </summary>
        /// <returns></returns>
        private static List<POJO.ImgItem> GetExistsItems()
        {
            var list = new List<ImgItem>();
            for (int i = 0; i < CurLevel.BackGridSplits.X * CurLevel.BackGridSplits.Y; i++)
            {
                int x = Convert.ToInt32(i%CurLevel.BackGridSplits.X);
                int y = (int)(i / CurLevel.BackGridSplits.X);

                if (AllItems[x][y]!=null)
                {
                    list.Add(AllItems[x][y]);
                }
            }
            return list;
        }

        /// <summary>
        /// 当前节点被选中
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        static bool item_SelectIt(POJO.ImgItem item)
        {
            if (CurSelectedItem != null && BLL.Engine.IsCanLink(item, CurSelectedItem))
            {
                CurLevel.Score = CurLevel.Score + CurLevel.BaseImgCount;
                //if (ItemLinkedHanlder != null)
                //{
                //ItemLinkedHanlder(item);
                //ItemLinkedHanlder(CurSelectedItem);
                //AniImage(item.Rect, item.Left, item.Top, item.Width, item.Height);
                //AniImage(CurSelectedItem.Rect, CurSelectedItem.Left, CurSelectedItem.Top, CurSelectedItem.Width, CurSelectedItem.Height);
                //}

                item.Hide();
                CurSelectedItem.Hide();
                AllItems[(int)item.Location.X][(int)item.Location.Y] = null;
                AllItems[(int)CurSelectedItem.Location.X][(int)CurSelectedItem.Location.Y] = null;
                CurSelectedItem = null;

                SelectedCount = SelectedCount + 2;
                //如果所有的都完成
                if (SelectedCount >= ItemCount)
                {
                    //触发完成事件
                    if (GameCompletedHandler!=null)
                    {
                        GameCompletedHandler(CurLevel);
                    }
                }

            }
            else
            {
                if (CurSelectedItem!=null)
                {
                    CurSelectedItem.CnacelSelect();
                }
                item.Select();
                CurSelectedItem = item;
                return true;
            }
            return false;
        }
        /// <summary>
        /// 消失动画
        /// <!--动画-->       
        /// </summary>
        /// <param name="ue"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        private static void AniImage(UIElement ue, double x, double y, double w, double h)
        {
            var img = new Image();
            img.Source = new WriteableBitmap(ue,null);
            img.Visibility = Visibility.Visible;
            img.Width = w;
            img.Height = h;
            img.SetValue(Canvas.LeftProperty,x);
            img.SetValue(Canvas.TopProperty,y);
            GameBackGrid.Children.Add(img);
            var plnpro = new PlaneProjection();
            plnpro.RotationX = 0;
            plnpro.RotationY = 0;
            img.Projection = plnpro;
            TranslateTransform ttm = new TranslateTransform();
            TransformGroup tcs = new TransformGroup();
            tcs.Children.Add(ttm);
            img.RenderTransform = tcs;
            var ani = new Storyboard();
            //if (!GameBackGrid.Children.Contains(ani)) GameBackGrid.Children.Add(ani);
            var dblani1 = new DoubleAnimation();
            dblani1.From = 0;
            dblani1.To = GameBackGrid.ActualHeight;
            dblani1.BeginTime = TimeSpan.FromSeconds(1);
            dblani1.Duration = new Duration(TimeSpan.FromSeconds(6));
            var cir = new CircleEase();
            cir.EasingMode = EasingMode.EaseIn;
            dblani1.EasingFunction = cir;
            Storyboard.SetTargetProperty(dblani1, new PropertyPath("(PlaneProjection.RotationX)"));
            Storyboard.SetTarget(dblani1, plnpro);
            ani.Children.Add(dblani1);
            var dblani2 = new DoubleAnimation();
            dblani2.From = 0;
            dblani2.To = GameBackGrid.ActualHeight;
            dblani2.BeginTime = TimeSpan.FromSeconds(0);
            dblani2.Duration = new Duration(TimeSpan.FromSeconds(8));
            Storyboard.SetTargetProperty(dblani2, new PropertyPath("(TranslateTransform.Y)"));
            Storyboard.SetTarget(dblani2,ttm);
            ani.Children.Add(dblani2);
            //动画完毕委托
            ani.Completed += delegate { GameBackGrid.Children.Remove(img); };
            ani.Begin();
        }

        #region 二点是否可连算法

        /// <summary>
        /// 判断二个点是否可以连
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        public static bool IsCanLink(POJO.ImgItem item1, POJO.ImgItem item2)
        {
            //如果二个图片不相同。则直接返回否.且如果二个为同一个也不可连
            if (item1.ItemImg!=item2.ItemImg||item1==item2)
            {
                return false;   
            }
            ItemDer itemder = GetItemDer(item1,item2);
            switch (itemder)
            {
                case ItemDer.LineX://二点在同一X轴上
                    {
                        return CheckLineX(item1,item2);
                    }
                case ItemDer.LineY://二点在同一Y轴上
                    {
                        return CheckLineY(item1,item2);
                    }
                case ItemDer.LTRB://左上右下
                    {
                        return CheckLBRT(item1,item2);
                    }
                case ItemDer.LBRT://左下右上
                    {
                        return CheckLBRT(item1, item2);
                    }
            }
            return false;
        }

        /// <summary>
        /// 在同一X轴上的判断
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        private static bool CheckLineX(POJO.ImgItem item1, POJO.ImgItem item2)
        {
            //判断二点间的直线是否可连,或为最顶部点,或为最底部点
            if (CheckInPointsIsEmpty(item1.Location, item2.Location, ItemDer.LineX) || (item1.Location.Y == 0 && item2.Location.Y == 0) ||
                (item1.Location.Y == CurLevel.BackGridSplits.Y - 1 && item2.Location.Y == CurLevel.BackGridSplits.Y - 1))
            {
                return true;
            }

            //向上开始判断
            //每次向上伸一个编移量,如果有连通的则可以连
            for (int i = (int)item1.Location.Y - 1; i >= 0; i--)
            {
                Point p1 = new Point(item1.Location.X, i);
                Point p2 = new Point(item2.Location.X, i);
                //如果这二点有任意一点不为空
                //则向上的方向不可行
                if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break;

                if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineX)) //如果它们之间为空
                {
                    return true;
                }

                if (i == 0)//如果这二点为最顶部,所以可以连
                {
                    return true;
                }
            }

            //向上开始判断
            //每次向下伸一个编移量,如果有连通的则可以连
            for (int i = (int)item1.Location.Y + 1; i <= CurLevel.BackGridSplits.Y - 1; i++)
            {
                Point p1 = new Point(item1.Location.X, i);
                Point p2 = new Point(item2.Location.X, i);
                //如果这二点有任意一点不为空
                //则向下的方向不可行
                if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break;

                if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineX)) //如果它们之间为空
                {
                    return true;
                }

                if (i == CurLevel.BackGridSplits.Y - 1)//如果这二点为最底部,所以可以连
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 在同一Y轴上的判断
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        private static bool CheckLineY(POJO.ImgItem item1, POJO.ImgItem item2)
        {
            //判断二点间的直线是否可连,或为最左边,或为最右边
            if (CheckInPointsIsEmpty(item1.Location, item2.Location, ItemDer.LineY) || (item1.Location.X == 0 && item2.Location.X == 0) ||
                (item1.Location.X == CurLevel.BackGridSplits.X - 1 && item2.Location.X == CurLevel.BackGridSplits.X - 1))
            {
                return true;
            }

            //向左开始判断
            //每次向左伸一个编移量,如果有连通的则可以连
            for (int i = (int)item1.Location.X - 1; i >= 0; i--)
            {
                Point p1 = new Point(i, item1.Location.Y);
                Point p2 = new Point(i, item2.Location.Y);
                //如果这二点有任意一点不为空
                //则向左的方向不可行
                if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break;

                if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineY)) //如果它们之间为空
                {
                    return true;
                }

                if (i == 0)//如果这二点为最左边,所以可以连
                {
                    return true;
                }
            }

            //向右开始判断
            //每次向右伸一个编移量,如果有连通的则可以连
            for (int i = (int)item1.Location.X + 1; i <= CurLevel.BackGridSplits.X - 1; i++)
            {
                Point p1 = new Point(i, item1.Location.Y);
                Point p2 = new Point(i, item2.Location.Y);
                //如果这二点有任意一点不为空
                //则向右的方向不可行
                if (!CheckPointIsEmpty(p1) || !CheckPointIsEmpty(p2)) break;

                if (CheckInPointsIsEmpty(p1, p2, ItemDer.LineY)) //如果它们之间为空
                {
                    return true;
                }

                if (i == CurLevel.BackGridSplits.X - 1)//如果这二点为最右,所以可以连
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 判断左上右下的二点
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        private static bool CheckLTRB(POJO.ImgItem item1, POJO.ImgItem item2)
        {
            //如果item1在右边
            //就对换,保持item1在左边
            if (item1.Location.X > item2.Location.X)
            {
                POJO.ImgItem itemtmp = item1;
                item1 = item2;
                item2 = itemtmp;
            }

            //首先向左检查查
            //因为item2的X值大,所以从item2开始向左延伸
            for (int i = (int)item2.Location.X - 1; i >= 0; i--)
            {
                Point p1 = new Point(i, item2.Location.Y);
                Point p2 = new Point(i, item1.Location.Y);
                //如果左点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果左点上方与item1水平的点不为空,则此线不行。进行下一个
                if (p2 != item1.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item1的左边了,则此方向不可以再连通了
                    if (i < item1.Location.X)
                    {
                        break;
                    }
                    continue;

                }
                //如果p2与item1之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item1.Location, ItemDer.LineX) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineY))
                {
                    return true;
                }
                else if (i == 0)//如果虽然这二边连不通。但这二点已是最边点了。说明可连
                {
                    return true;
                }
            }

            //向右检查查
            //因为item1的X值小,所以从item1开始向右延伸
            for (int i = (int)item1.Location.X + 1; i < CurLevel.BackGridSplits.X; i++)
            {
                Point p1 = new Point(i, item1.Location.Y);
                Point p2 = new Point(i, item2.Location.Y);
                //如果右点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果右点下方与item2水平的点不为空,则此线不行。进行下一个
                if (p2 != item2.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item2的右边了,则此方向不可以再连通了
                    if (i > item2.Location.X)
                    {
                        break;
                    }
                    continue;
                }
                //如果p2与item2之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item2.Location, ItemDer.LineX) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineY))
                {
                    return true;
                }
                //如果虽然这二边连不通。但这二点已是最边点了。说明可连
                else if (i == CurLevel.BackGridSplits.X - 1)
                {
                    return true;
                }
            }

            //向上检查查
            //因为item2的Y值大,所以从item2开始向上延伸
            for (int i = (int)item2.Location.Y - 1; i >= 0; i--)
            {
                Point p1 = new Point(item2.Location.X, i);
                Point p2 = new Point(item1.Location.X, i);
                //如果上点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果上点左方与item1垂直的点不为空,则此线不行。进行下一个
                if (p2 != item1.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item1的上边了,则此方向不可以再连通了
                    if (i < item1.Location.Y)
                    {
                        break;
                    }
                    continue;
                }
                //如果p2与item1之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item1.Location, ItemDer.LineY) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineX))
                {
                    return true;
                }
                else if (i == 0)//如果虽然这二边连不通。但这二点已是最边点了。说明可连
                {
                    return true;
                }
            }

            //向下检查查
            //因为item1的Y值小,所以从item1开始向下延伸
            for (int i = (int)item1.Location.Y + 1; i < CurLevel.BackGridSplits.Y; i++)
            {
                Point p1 = new Point(item1.Location.X, i);
                Point p2 = new Point(item2.Location.X, i);

                //如果下点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果下点下方与item2垂直的点不为空,则此线不行。进行下一个
                if (p2 != item2.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item2的下边了,则此方向不可以再连通了
                    if (i > item2.Location.Y)
                    {
                        break;
                    }
                    continue;
                }
                //如果p2与item2之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item2.Location, ItemDer.LineY) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineX))
                {
                    return true;
                }
                //如果虽然这二边连不通。但这二点已是最边点了。说明可连
                else if (i == CurLevel.BackGridSplits.Y - 1)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 判断左下右上的二点
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        private static bool CheckLBRT(POJO.ImgItem item1, POJO.ImgItem item2)
        {
            //如果item1在右边
            //就对换,保持item1在左边
            if (item1.Location.X > item2.Location.X)
            {
                POJO.ImgItem itemtmp = item1;
                item1 = item2;
                item2 = itemtmp;
            }

            //首先向左检查查
            //因为item2的X值大,所以从item2开始向左延伸
            for (int i = (int)item2.Location.X - 1; i >= 0; i--)
            {
                Point p1 = new Point(i, item2.Location.Y);
                Point p2 = new Point(i, item1.Location.Y);
                //如果左点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果左点下方与item1水平的点不为空,则此线不行。进行下一个
                if (p2 != item1.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item1的左边了,则此方向不可以再连通了
                    if (i < item1.Location.X)
                    {
                        break;
                    }
                    continue;

                }
                //如果p2与item1之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item1.Location, ItemDer.LineX) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineY))
                {
                    return true;
                }
                else if (i == 0)//如果虽然这二边连不通。但这二点已是最边点了。说明可连
                {
                    return true;
                }
            }

            //向右检查查
            //因为item1的X值小,所以从item1开始向右延伸
            for (int i = (int)item1.Location.X + 1; i < CurLevel.BackGridSplits.X; i++)
            {
                Point p1 = new Point(i, item1.Location.Y);
                Point p2 = new Point(i, item2.Location.Y);
                //如果右点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果右点下方与item2水平的点不为空,则此线不行。进行下一个
                if (p2 != item2.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item2的右边了,则此方向不可以再连通了
                    if (i > item2.Location.X)
                    {
                        break;
                    }
                    continue;
                }
                //如果p2与item2之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item2.Location, ItemDer.LineX) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineY))
                {
                    return true;
                }
                //如果虽然这二边连不通。但这二点已是最边点了。说明可连
                else if (i == CurLevel.BackGridSplits.X - 1)
                {
                    return true;
                }
            }

            //向上检查查
            //因为item1的Y值大,所以从item2开始向上延伸
            for (int i = (int)item1.Location.Y - 1; i >= 0; i--)
            {
                Point p1 = new Point(item1.Location.X, i);
                Point p2 = new Point(item2.Location.X, i);
                //如果上点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果上点左方与item2垂直的点不为空,则此线不行。进行下一个
                if (p2 != item2.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item2的上边了,则此方向不可以再连通了
                    if (i < item2.Location.Y)
                    {
                        break;
                    }
                    continue;
                }
                //如果p2与item2之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item2.Location, ItemDer.LineY) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineX))
                {
                    return true;
                }
                else if (i == 0)//如果虽然这二边连不通。但这二点已是最边点了。说明可连
                {
                    return true;
                }
            }

            //向下检查查
            //因为item2的Y值小,所以从item2开始向下延伸
            for (int i = (int)item2.Location.Y + 1; i < CurLevel.BackGridSplits.Y; i++)
            {
                Point p1 = new Point(item2.Location.X, i);
                Point p2 = new Point(item1.Location.X, i);

                //如果下点不为空。则此方向不可行
                if (!CheckPointIsEmpty(p1))
                {
                    break;
                }
                //如果下点下方与item1垂直的点不为空,则此线不行。进行下一个
                if (p2 != item1.Location && !CheckPointIsEmpty(p2))
                {
                    //如果i已经到item2的下边了,则此方向不可以再连通了
                    if (i > item1.Location.Y)
                    {
                        break;
                    }
                    continue;
                }
                //如果p2与item1之间的连线为空且p1与p2之间的连线为空
                //则这二点可以连线
                if (CheckInPointsIsEmpty(p2, item1.Location, ItemDer.LineY) && CheckInPointsIsEmpty(p1, p2, ItemDer.LineX))
                {
                    return true;
                }
                //如果虽然这二边连不通。但这二点已是最边点了。说明可连
                else if (i == CurLevel.BackGridSplits.Y - 1)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 判断二点之间是否都为空
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <returns></returns>
        private static bool CheckInPointsIsEmpty(Point p1, Point p2, ItemDer iDer)
        {
            //如果二点为X轴方向
            if (iDer == ItemDer.LineX)
            {
                //如果二点在一起
                if (Math.Abs(p1.X - p2.X) == 1 || (p1.X - p2.X) == 0)
                {
                    return true;
                }

                //得到二点的X轴偏移量
                int offsetx = (int)(p1.X - p2.X);
                //p1在p2的左边
                if (offsetx < 0)
                {
                    for (int i = 1; i < Math.Abs(offsetx); i++)
                    {
                        //如果二点之间有不为空的点
                        if (!CheckPointIsEmpty(new Point(p1.X + i, p1.Y)))
                        {
                            return false;
                        }
                    }
                    return true;
                }
                else//1在2的右边
                {
                    for (int i = 1; i < Math.Abs(offsetx); i++)
                    {
                        //如果二点之间有不为空的点
                        if (!CheckPointIsEmpty(new Point(p2.X + i, p1.Y)))
                        {
                            return false;
                        }
                    }
                    return true;
                }
            }
            else if (iDer == ItemDer.LineY) //如果为Y轴方向
            {
                //如果二点在一起
                if (Math.Abs(p1.Y - p2.Y) == 1 || p1.Y - p2.Y == 0)
                {
                    return true;
                }

                //得到二点的Y轴偏移量
                int offsety = (int)(p1.Y - p2.Y);
                //p1在p2的上边
                if (offsety < 0)
                {
                    for (int i = 1; i < Math.Abs(offsety); i++)
                    {
                        //如果二点之间有不为空的点
                        if (!CheckPointIsEmpty(new Point(p1.X, p1.Y + i)))
                        {
                            return false;
                        }
                    }
                    return true;
                }
                else//1在2的下边
                {
                    for (int i = 1; i < Math.Abs(offsety); i++)
                    {
                        //如果二点之间有不为空的点
                        if (!CheckPointIsEmpty(new Point(p2.X, p2.Y + i)))
                        {
                            return false;
                        }
                    }
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 检查查此位置是否为空
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private static bool CheckPointIsEmpty(Point p)
        {
            //foreach (POJO.ImgItem item in Config.SLConfig.AllItems)
            //{
            //    if (item.Location == p)
            //    {
            //        return false;
            //    }
            //}
            return AllItems[(int)p.X][(int)p.Y] == null;
        }

        /// <summary>
        /// 判断二点的方位
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        private static ItemDer GetItemDer(POJO.ImgItem item1, POJO.ImgItem item2)
        {
            if (item1.Location.X == item2.Location.X)
            {
                return ItemDer.LineY;
            }
            if (item2.Location.Y == item1.Location.Y)
            {
                return ItemDer.LineX;
            }

            if ((item1.Location.X - item2.Location.X) * (item1.Location.Y - item2.Location.Y) > 0)
            {
                return ItemDer.LTRB;
            }
            else
                return ItemDer.LBRT;
        }

        #endregion
    }
}
---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

详细请查看:
http://net.itheima.com/