Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("==== Console program for knights travails problem =====\n\n");

            Console.WriteLine("Please enter source and destination(algebraic notation) \nseparated by space(i.e a8 b7):\n");
            string inp = Console.ReadLine();

            while (!String.IsNullOrEmpty(inp) && !inp.ToLowerInvariant().Equals("q") )
            {
                string[] inpSquares = inp.Split(' ');
                string src = inpSquares[0].Trim().ToLowerInvariant();
                string dest = inpSquares[1].Trim().ToLowerInvariant();

                var board = new ChessBoard();
                var knightGraph = new KnightGraph(board);
                var shortestPathStrategy = new DijkstraAlgorithm(graph: knightGraph, source: board.Squares[src]);
                var shortestPath = shortestPathStrategy.GetShortestPathTo(board.Squares[dest]);

                Console.WriteLine(String.Format("Shortest Path from {0} to {1} : ", src, dest));
                if(shortestPath==null)
                    Console.WriteLine("unreachable");
                else
                    shortestPath.ForEach(sq=>Console.Write(sq+" "));

                Console.Write("\nWant to continue?(y/n)");
                string cont = Console.ReadLine();
                if(!cont.ToLowerInvariant().Equals("y"))
                    break;

                Console.WriteLine("\nEnter source and destination separated by space");
                inp = Console.ReadLine();
            }

            Console.WriteLine();
        }
Exemplo n.º 2
0
        public void KnightGraph()
        {
            var board = new ChessBoard();
            var knightGraph = new KnightGraph(board);

            Assert.AreEqual(2, knightGraph.EdgesAdjacencyList[board.Squares["a1"]].Count);
            Assert.AreEqual(8, knightGraph.EdgesAdjacencyList[board.Squares["c3"]].Count);
            Assert.AreEqual(3, knightGraph.EdgesAdjacencyList[board.Squares["a2"]].Count);
            Assert.AreEqual(4, knightGraph.EdgesAdjacencyList[board.Squares["b2"]].Count);
            Assert.AreEqual(6, knightGraph.EdgesAdjacencyList[board.Squares["c2"]].Count);
        }
        public void GetShortestPathToTest()
        {
            var board = new ChessBoard();
            var knightGraph = new KnightGraph(board);

            var pathStrategy = new DijkstraAlgorithm(board.Squares["b7"], knightGraph);
            var path = pathStrategy.GetShortestPathTo(board.Squares["a8"]);

            foreach (var square in path)
            {
                Console.WriteLine(square);
            }
        }