⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfind.cs

📁 一个用java设计的推箱子的程序。很不错的。呵呵
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace PushBox
{
    public class ObjectPoint
    {
        public int X;
        public int Y;
        public ObjectPoint()
        {
            X = 0;
            Y = 0;
        }
        public ObjectPoint(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
    public class Node
    {
        private double g = 0;//父点到当前点的耗费
        private double h = 0;//当前点到终点的耗费
        private int x = 0;  //坐标
        private int y = 0;
        public double F //总耗费
        {
            get
            {
                return g + h;
            }
        }
        public double G
        {
            get
            {
                return g;
            }
            set
            {
                g = value;
            }
        }
        public double H
        {
            get
            {
                return h;
            }
            set
            {
                h = value;
            }
        }
        public int X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
        public int Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }
        private bool canMove = true;
        public bool CanMove
        {
            get
            {
                return canMove;
            }
            set
            {
                canMove = value;
            }
        }
        protected Node parentNode = null;
        public Node ParentNode
        {
            get
            {
                return parentNode;
            }
            set
            {
                parentNode = value;
            }
        }
    }
    public class Finder
    {
        private System.Collections.ArrayList openLine = new System.Collections.ArrayList();
        private System.Collections.ArrayList closeLine = new System.Collections.ArrayList();
        private Node[] fn;
        private ObjectPoint startOP = null;
        private ObjectPoint endOP = null;
        public Finder(Node[] afn)
        {
            fn = afn;
        }
        //设置所有节点的H值;
        private void SetH()
        {
            for (int i = 0; i < fn.Length; i++)
            {
                fn[i].H = Math.Abs(endOP.X - fn[i].X) + Math.Abs(endOP.Y - fn[i].Y);
            }
        }
        public Node GetNode(int ax, int ay)
        {
            for (int i = 0; i < fn.Length; i++)
            {
                if ((fn[i].X == ax) && (fn[i].Y == ay))
                {
                    return fn[i];
                }
            }
            return null;
        }
        public bool CanFind()
        {
            Node bn = GetNode(this.endOP.X, endOP.Y);
            if (bn == null) return false;
            for (int i = 0; i < openLine.Count; i++)
            {
                if (bn == openLine[i]) return true;
            }
            return false;
        }
        protected bool IsInOpenLine(Node afn)
        {
            for (int i = 0; i < this.openLine.Count; i++)
            {
                if (afn == openLine[i]) return true;
            }

            return false;
        }
        protected bool IsInCloseLine(Node afn)
        {
            for (int i = 0; i < this.closeLine.Count; i++)
            {
                if (afn == closeLine[i]) return true;
            }
            return false;
        }
        protected void AddToOpenLine(Node afn)
        {
            if (!afn.CanMove) return;  //如果不能移动的就不加了。
            for (int i = 0; i < this.openLine.Count; i++)
            {
                if (afn == openLine[i]) return;
            }
            for (int i = 0; i < this.closeLine.Count; i++)
            {
                if (afn == closeLine[i]) return;
            }
            //排序
            for (int i = 0; i < this.openLine.Count; i++)
            {
                if (afn.F <= ((Node)openLine[i]).F)
                {
                    openLine.Insert(i, afn);
                    return;
                }
            }
            openLine.Add(afn);
        }
        protected void AddToCloseLine(Node afn)
        {
            for (int i = 0; i < this.closeLine.Count; i++)
            {
                if (afn == closeLine[i]) return;
            }
            closeLine.Add(afn);
        }
        //找出并设置相领的节点.
        protected void FindNearNode(Node afn)
        {
            int mx, my;
            mx = afn.X;
            my = afn.Y;
            FindNearNodeXY(afn, mx - 1, my);
            FindNearNodeXY(afn, mx + 1, my);
            FindNearNodeXY(afn, mx, my - 1);
            FindNearNodeXY(afn, mx, my + 1);
            this.openLine.Remove(afn);
            this.AddToCloseLine(afn);
        }
        protected void FindNearNodeXY(Node afn, int ax, int ay)
        {
            Node newNode = null;
            newNode = GetNode(ax, ay);
            if (newNode != null)
            {
                if (!newNode.CanMove)
                {
                    AddToCloseLine(newNode);
                    return;
                }
                if ((!IsInOpenLine(newNode)) && (!IsInCloseLine(newNode)))
                {
                    newNode.ParentNode = afn;
                    newNode.G = afn.G + 10;
                    AddToOpenLine(newNode);
                }
                else
                {
                    if ((!IsInCloseLine(newNode)) && (newNode.F > afn.F + 10))
                    {
                        newNode.ParentNode = afn;
                        newNode.G = afn.G;
                    }
                }
            }
        }
        public string Find(ObjectPoint aStartOP, ObjectPoint aEndOP)
        {
            this.startOP = new ObjectPoint(aStartOP.X, aStartOP.Y);
            this.endOP = new ObjectPoint(aEndOP.X, aEndOP.Y);

            Node SP = GetNode(startOP.X, startOP.Y);

            SP.G = 0;

            this.SetH();
            AddToOpenLine(GetNode(startOP.X, startOP.Y));
            while ((!CanFind()) && openLine.Count > 0)
            {
                this.FindNearNode((Node)(openLine[0]));
            }
            //找出了以后
            Node EP = GetNode(endOP.X, endOP.Y);
            string str = "";
            if (EP.ParentNode == null) return "false";
            while (EP.ParentNode != SP)
            {
                str = (EP.ParentNode.Y * 15 + EP.ParentNode.X).ToString() + "F" + str;
                EP = EP.ParentNode;
            }
            str += (endOP.Y * 15 + endOP.X).ToString() + "F";
            return str;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -