五彩珠遊戲

iDotNetSpace發表於2009-10-22

對以前寫的一個 遊戲,改動了一下.

路徑查詢由原來的廣度演算法。改為a星演算法.

 

//a*演算法,比廣度多一個引數,來評估佇列中每個點到目的地的估計消耗,來提高效能。
        public Queue path3(point3 start, point3 end, int[,] migonga)
        {
            int[,] migong = (int[,])migonga.Clone();
            Queue myqueue = new Queue();
            myqueue = new Queue();
            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)myqueue.OrderByDescending(p => p.f);

               //對評估直排序,先搜尋評估直最小的。

                List tem = new List(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


原文地址:http://www.cnblogs.com/lsfv/archive/2009/10/20/1586932.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-617197/,如需轉載,請註明出處,否則將追究法律責任。

相關文章