Пример #1
0
        ///
        //Inititialize new nodes and construct them accordingly.;
        //Remember that field[X,Y] are bonded with the For-loop X & Y order (In this case: "field[y,x]").
        ///
        Node[,] ConstructNodes(int width, int height, Node[,] field, int[,] layout)
        {
            Node[,] result = field;
            int i = 0;
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Node temp = field[y, x];

                    temp.x = x;
                    temp.y = y;
                    temp.layout = layout[y, x];
                    temp.id = i;

                    result[y, x] = temp;
                    i++;
                }
            }
            return result;
        }
Пример #2
0
        public Program()
        {
            int width = 7;
            int height = 5;
            int[,] layout = new int[,]
                                    {
                        //Remember that X is the after the comma. Y is "first".
                                        {0,0,0,0,0,0,0},
                                        {0,0,0,1,0,0,0},
                                        {0,2,0,1,0,3,0},
                                        {0,0,0,1,0,0,0},
                                        {0,0,0,0,0,0,0}
                                    };
            //Which then means that "width" comes after "height".
            Node[,] field = new Node[height, width];
            //Inititialize new nodes and construct them accordingly.
            Node[,] currField = ConstructNodes(width, height, field, layout);

            Visualize(width, height, Solve(width, height, currField));

            Console.Write("\nDone.");
            Console.ReadLine();
        }
Пример #3
0
        ///
        //Start solving our problem (Which is finding the shortest route from start (2) to the target (3);
        //Remember that field[X,Y] are bonded with the For-loop X & Y order (In this case: "field[y,x]").
        ///
        Node[,] Solve(int width, int height, Node[,] field)
        {
            Node[,] currField = field;
            bool[] circleArr = new bool[8];
            //List<Node> circleNodeList = new List<Node>();
            List<Node> openList = new List<Node>();
            List<Node> closedList = new List<Node>();
            List<Node> parentList = new List<Node>();
            Node currParentNode = new Node();
            int currX = -1;
            int currY = -1;
            int endX = -1;
            int endY = -1;
            int parent = 0;
            int index = 0;
            bool solving = true;

            while (solving)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        Node temp = currField[y, x];

                        switch (index)
                        {
                            case 0:
                                if (temp.layout == 0)
                                    openList.Add(temp);
                                if (temp.layout == 2)
                                    closedList.Add(temp);
                                if (temp.layout == 3)
                                {
                                    endX = temp.x;
                                    endY = temp.y;
                                    openList.Add(temp);
                                }
                                break;
                            case 1:
                                if (temp.layout != 1 && temp.layout != 2 && temp.layout != 5)
                                temp.h = (Math.Abs(x - endX) * 10) + (Math.Abs(y - endY) * 10);
                                break;
                            case 2:
                                currParentNode = closedList[closedList.Count - 1];
                                currX = currParentNode.x;
                                currY = currParentNode.y;
                                parent = currParentNode.id;
                                if (parent != 2 && parent != 3)
                                    currField[currY, currX].layout = 5;
                                x = width - 1;
                                y = height - 1;
                                break;
                            case 3:
                                circleArr[0] = (temp.x == (currX) && temp.y == (currY - 1));
                                circleArr[1] = (temp.x == (currX + 1) && temp.y == (currY - 1));
                                circleArr[2] = (temp.x == (currX + 1) && temp.y == (currY));
                                circleArr[3] = (temp.x == (currX + 1) && temp.y == (currY + 1));
                                circleArr[4] = (temp.x == (currX) && temp.y == (currY + 1));
                                circleArr[5] = (temp.x == (currX - 1) && temp.y == (currY + 1));
                                circleArr[6] = (temp.x == (currX - 1) && temp.y == (currY));
                                circleArr[7] = (temp.x == (currX - 1) && temp.y == (currY - 1));

                                for (int c = 0; c < circleArr.Length; c++)
                                    if (circleArr[c] == true)
                                        if (temp.layout != 1 && temp.layout != 2 && temp.layout != 5)
                                        {
                                            if (temp.layout != 3)
                                                temp.layout = 4;
                                            if (c % 2 == 0)
                                                temp.g += 10;
                                            else
                                                temp.g += 14;

                                            temp.parent = parent;
                                            temp.f = temp.g + temp.h;

                                            parentList.Add(temp);
                                            parentList.Sort((Node a, Node b) => (a.f).CompareTo(b.f));
                                        }

                                break;
                            case 4:
                                if (parent != 3)
                                {
                                    for (int h = parentList.Count - 1; h >= 0; h--)
                                    {
                                        openList.Remove(parentList[h]);
                                        closedList.Add(parentList[h]);
                                    }
                                    parentList.Clear();
                                    index = 1;
                                }
                                x = width - 1;
                                y = height - 1;
                                break;
                            default:
                                solving = false;
                                x = width - 1;
                                y = height - 1;
                                break;
                        }
                        currField[y, x] = temp;
                        Visualize(width, height, currField);
                    }
                }
                index++;
            }
            return currField;
        }
Пример #4
0
        ///
        //Visualizes the solving;
        //Remember that field[X,Y] are bonded with the For-loop X & Y order (In this case: "field[y,x]").
        ///
        void Visualize(int width, int height, Node[,] field)
        {
            Thread.Sleep(50);
            Console.Clear();
            Console.WriteLine("LAYOUT");
            for (int y = 0; y < height; y++)
            {
                string line = "";

                for (int x = 0; x < width; x++)
                    line += field[y, x].layout.ToString() + " ";

                Console.WriteLine(line);
            }
            Console.WriteLine("\nHEURISTICS COST");
            for (int y = 0; y < height; y++)
            {
                string line = "";

                for (int x = 0; x < width; x++)
                    line += field[y, x].h.ToString() + " ";

                Console.WriteLine(line);
            }
            Console.WriteLine("\nMOVEMENT COST");
            for (int y = 0; y < height; y++)
            {
                string line = "";

                for (int x = 0; x < width; x++)
                    line += field[y, x].g.ToString() + " ";

                Console.WriteLine(line);
            }
            Console.WriteLine("\nTOTAL COST");
            for (int y = 0; y < height; y++)
            {
                string line = "";

                for (int x = 0; x < width; x++)
                    line += field[y, x].f.ToString() + " ";

                Console.WriteLine(line);
            }
            Console.WriteLine("\nIDS");
            for (int y = 0; y < height; y++)
            {
                string line = "";

                for (int x = 0; x < width; x++)
                    line += field[y, x].id.ToString() + " ";

                Console.WriteLine(line);
            }
            Console.WriteLine("\nPARENTS");
            for (int y = 0; y < height; y++)
            {
                string line = "";

                for (int x = 0; x < width; x++)
                    line += field[y, x].parent.ToString() + " ";

                Console.WriteLine(line);
            }
        }