Exemplo n.º 1
0
        private Solution CreateNetworkFromMaze(IndexModel model)
        {
            var  network     = new Network();
            Node startNode   = null;
            Node endNode     = null;
            Node domokunNode = null;

            for (var row = 0; row < model.Height; row++)
            {
                for (var column = 0; column < model.Width; column++)
                {
                    var index = GetIndexFromRowAndColumn(row, column, model.Width);

                    var node = network.AddNode(index);
                    if (model.Pony == index)
                    {
                        startNode = node;
                    }
                    if (model.EndPoint == index)
                    {
                        endNode = node;
                    }
                    if (model.Domokun == index)
                    {
                        domokunNode = node;
                    }

                    if (!model.Walls[index].Contains("north"))
                    {
                        // there is a connection to the north
                        // same column, row above
                        var otherIndex = GetIndexFromRowAndColumn((row - 1), column, model.Width);
                        network.MakeLink(index, otherIndex);
                    }

                    if (!model.Walls[index].Contains("west"))
                    {
                        // there is a connection to the west
                        // same row, left column
                        var otherIndex = index - 1;
                        network.MakeLink(index, otherIndex);
                    }
                }
            }

            var path = PathFinderService.FindShortestPath(network, startNode, endNode);

            var moves          = ConvertPathToMoves(path, model.Width);
            var movesToDomokun = path.TakeWhile(n => n.Id != domokunNode.Id).Count();
            var movesToEnd     = path.Count - 1;

            return(new Solution()
            {
                NoMovesToDomokun = movesToDomokun == movesToEnd ? -1 : movesToDomokun,
                NoMovesToEnd = movesToEnd,
                MovesToEnd = moves,
            });
        }
Exemplo n.º 2
0
        public void FindShortestPathValid2()
        {
            // arrange
            var a = Guid.NewGuid();
            var b = Guid.NewGuid();
            var c = Guid.NewGuid();
            var d = Guid.NewGuid();

            var vertices = new Guid[] { a, b, c, d };
            var edges    = new List <Guid[]> {
                new Guid[] { a, b }, new Guid[] { b, c }, new Guid[] { c, d }, new Guid[] { b, d }
            };

            // act
            var finder  = new PathFinderService();
            var results = finder.FindShortestPath(vertices, edges, vertices[0], vertices[3]);

            // assertation
            var resultsExpected = new Guid[] { a, b, d };

            results.Should().BeEquivalentTo(resultsExpected);
        }