ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 五彩珠游戏

五彩珠游戏

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-10-22 16:08:15 0 删除 编辑

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

路径查询由原来的广度算法。改为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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5297180