Пример #1
0
        private string TryGo(string dir, MazeNode node)
        {
            switch (dir)
            {
            case "up":
                if (node.directions["up"])
                {
                    posx -= 2;
                    return("up");
                }
                if (node.directions["right"])
                {
                    posy += 2;
                    return("right");
                }
                if (node.directions["left"])
                {
                    posy -= 2;
                    return("left");
                }
                if (node.directions["down"])
                {
                    posx += 2;
                    return("down");
                }
                return("dead");

            case "down":
                if (node.directions["down"])
                {
                    posx += 2;
                    return("down");
                }
                if (node.directions["right"])
                {
                    posy += 2;
                    return("right");
                }
                if (node.directions["left"])
                {
                    posy -= 2;
                    return("left");
                }
                if (node.directions["up"])
                {
                    posx -= 2;
                    return("up");
                }
                return("dead");

            case "left":
                if (node.directions["left"])
                {
                    posy -= 2;
                    return("left");
                }
                if (node.directions["up"])
                {
                    posx -= 2;
                    return("up");
                }
                if (node.directions["down"])
                {
                    posx += 2;
                    return("down");
                }
                if (node.directions["right"])
                {
                    posy += 2;
                    return("right");
                }
                return("dead");

            case "right":
                if (node.directions["right"])
                {
                    posy += 2;
                    return("right");
                }
                if (node.directions["up"])
                {
                    posx -= 2;
                    return("up");
                }
                if (node.directions["down"])
                {
                    posx += 2;
                    return("down");
                }
                if (node.directions["left"])
                {
                    posy -= 2;
                    return("left");
                }
                return("dead");
            }
            return("er");
        }
Пример #2
0
        private void SolveMaze()
        {
            MazeNode currentNode = new MazeNode(null, posx, posy);

            char[,] maze = mazes[mazeId];
            List <string> path = new List <string>();

            CheckDirections(currentNode, maze);
            while (posx != finishx || posy != finishy)
            {
                if (path.Count > 0)
                {
                    if (path.Last() == "up")
                    {
                        currentNode.directions["down"] = false;                      //dont go back
                    }
                    if (path.Last() == "down")
                    {
                        currentNode.directions["up"] = false;
                    }
                    if (path.Last() == "left")
                    {
                        currentNode.directions["right"] = false;
                    }
                    if (path.Last() == "right")
                    {
                        currentNode.directions["left"] = false;
                    }
                }
                if (posx > finishx)
                {
                    string dir = TryGo("up", currentNode);
                    if (dir == "dead")
                    {
                        path.RemoveAt(path.Count - 1);
                        posx        = currentNode.previousNode.posx;
                        posy        = currentNode.previousNode.posy;
                        currentNode = currentNode.previousNode;
                        continue;
                    }
                    path.Add(dir);
                    currentNode.directions[dir] = false;
                    currentNode = new MazeNode(currentNode, posx, posy);
                    CheckDirections(currentNode, maze);
                    continue;
                }
                if (posx < finishx)
                {
                    string dir = TryGo("down", currentNode);
                    if (dir == "dead")
                    {
                        path.RemoveAt(path.Count - 1);
                        posx        = currentNode.previousNode.posx;
                        posy        = currentNode.previousNode.posy;
                        currentNode = currentNode.previousNode;
                        continue;
                    }
                    path.Add(dir);
                    currentNode.directions[dir] = false;
                    currentNode = new MazeNode(currentNode, posx, posy);
                    CheckDirections(currentNode, maze);
                    continue;
                }
                if (posy > finishy)
                {
                    string dir = TryGo("left", currentNode);
                    if (dir == "dead")
                    {
                        path.RemoveAt(path.Count - 1);
                        posx        = currentNode.previousNode.posx;
                        posy        = currentNode.previousNode.posy;
                        currentNode = currentNode.previousNode;
                        continue;
                    }
                    path.Add(dir);
                    currentNode.directions[dir] = false;
                    currentNode = new MazeNode(currentNode, posx, posy);
                    CheckDirections(currentNode, maze);
                    continue;
                }
                if (posy < finishy)
                {
                    string dir = TryGo("right", currentNode);
                    if (dir == "dead")
                    {
                        path.RemoveAt(path.Count - 1);
                        posx        = currentNode.previousNode.posx;
                        posy        = currentNode.previousNode.posy;
                        currentNode = currentNode.previousNode;
                        continue;
                    }
                    path.Add(dir);
                    currentNode.directions[dir] = false;
                    currentNode = new MazeNode(currentNode, posx, posy);
                    CheckDirections(currentNode, maze);
                    continue;
                }
            }
            string finished = "";

            foreach (string move in path)
            {
                finished += move + ", ";
            }
            TextSynthesizer.Speak(finished);
        }