五彩珠游戏

对以前写的一个 游戏,改动了一下.

路径查询由原来的广度算法。改为a星算法.

//a*算法,比广度多一个参数,来评估队列中每个点到目的地的估计消耗,来提高性能。
        public Queue<point3> path3(point3 start, point3 end, int[,] migonga)
        {
            int[,] migong = (int[,])migonga.Clone();
            Queue<point3> myqueue = new Queue<point3>();
            myqueue = new Queue<point3>();
            myqueue.Enqueue(start);
            start.level = 1;
            start.prepoint = null;
            start.visited = true;
            start.g = 0;
            start.f = 99;
            migong[start.py, start.px] = 1;
            int step = 0;
            while (myqueue.Count != 0 && (myqueue.Peek().px == end.px && myqueue.Peek().py == end.py) == false)
            {
                step += 1;
                //myqueue = (Queue<point3>)myqueue.OrderByDescending(p => p.f);

               //对评估直排序,先搜索评估直最小的。

                List<point3> tem = new List<point3>(myqueue);
                myqueue.Clear();
                for (int yy = 1; yy < tem.Count; yy++)
                {
                    if (tem[yy].f < tem[0].f)
                    {
                        point3 tempp = tem[0];
                        tem[0] = tem[yy];
                        tem[yy] = tempp;
                    }
                }
                for (int yy = 0; yy < tem.Count; yy++)
                {
                    myqueue.Enqueue(tem[yy]);
                }

                point3 p = myqueue.Dequeue();
                if (migong[p.py + 1, p.px] < 1)
                {

                    point3 pa = new point3(p.px, p.py + 1, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
                    migong[pa.py, pa.px] = 1;
                    myqueue.Enqueue(pa);
                    if (pa.px == end.px && pa.py == end.py)
                    {
                        break;
                    }
                }

                if (migong[p.py - 1, p.px] < 1)
                {
                    point3 pa = new point3(p.px, p.py - 1, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
                    migong[pa.py, pa.px] = 1;
                    myqueue.Enqueue(pa);
                    if (pa.px == end.px && pa.py == end.py)
                    {
                        myqueue.Enqueue(pa);
                        break;
                    }
                }

                if (migong[p.py, p.px + 1] < 1)
                {
                    point3 pa = new point3(p.px + 1, p.py, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
                    myqueue.Enqueue(pa);
                    migong[pa.py, pa.px] = 1;
                    if (pa.px == end.px && pa.py == end.py)
                    {
                        myqueue.Enqueue(pa);
                        break;
                    }
                }

                if (migong[p.py, p.px - 1] < 1)
                {
                    point3 pa = new point3(p.px - 1, p.py, p.level + 1, true, p, 1 + p.g, getFValue(p, end));
                    myqueue.Enqueue(pa);
                    migong[pa.py, pa.px] = 1;
                    if (pa.px == end.px && pa.py == end.py)
                    {
                        myqueue.Enqueue(pa);
                        break;
                    }
                }
            }
            Console.WriteLine(step.ToString());
            return myqueue;
        }

        //得到某点到目的点的消耗评估
        public int getFValue(point3 current, point3 end)
        {
            return Math.Abs(current.px - end.px) + Math.Abs(current.py - end.py);
        }

这里因为评估很简单,其实复杂就是评估难算.这个很简单了。

有耐心的朋友,可以看下代码.顺便有性能上的建议,欢迎指正.谢谢。

代码地址:http://download.csdn.net/source/1754605

原文地址:https://www.cnblogs.com/lsfv/p/1586932.html