Esempio 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();
        }
Esempio 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);
            }
        }
Esempio n. 4
0
        public void ChessBoard()
        {
            var board = new ChessBoard();

            foreach (var sq in board.Squares)
            {
                Console.WriteLine(sq.ToString() + String.Format("  r: {0}, c: {1}", sq.Value.RowPosition, sq.Value.ColPosition));
            }
            Assert.AreEqual("a1", board.Squares["a1"].ToString());
            Assert.AreEqual("a2", board.Squares["a2"].ToString());

            var square = board.GetSquare(5, 6);
            Assert.AreEqual(5, square.RowPosition);
            Assert.AreEqual(6, square.ColPosition);

            Assert.IsNull(board.GetSquare(0, 8));
            Assert.IsNotNull(board.GetSquare(8, 8));
            Assert.IsNotNull(board.GetSquare(1, 1));
        }
Esempio n. 5
0
        public KnightGraph(ChessBoard board)
            : base(board)
        {
            AdjacencyList=new Dictionary<Square, List<Square>>();

            //initialize graph edges for knights moves
            foreach (var sq in Board.Squares)
            {
                var sqList = new List<Square>();
                var square = sq.Value;

                //add edges to eight possible moves for the knight
                var moveSq = Board.GetSquare(square.RowPosition + 2, square.ColPosition + 1);
                if(moveSq!=null) sqList.Add(moveSq);

                moveSq = Board.GetSquare(square.RowPosition + 2, square.ColPosition - 1);
                if (moveSq != null) sqList.Add(moveSq);

                moveSq = Board.GetSquare(square.RowPosition - 2, square.ColPosition + 1);
                if (moveSq != null) sqList.Add(moveSq);

                moveSq = Board.GetSquare(square.RowPosition - 2, square.ColPosition - 1);
                if (moveSq != null) sqList.Add(moveSq);

                moveSq = Board.GetSquare(square.RowPosition + 1, square.ColPosition + 2);
                if (moveSq != null) sqList.Add(moveSq);

                moveSq = Board.GetSquare(square.RowPosition + 1, square.ColPosition - 2);
                if (moveSq != null) sqList.Add(moveSq);

                moveSq = Board.GetSquare(square.RowPosition - 1, square.ColPosition + 2);
                if (moveSq != null) sqList.Add(moveSq);

                moveSq = Board.GetSquare(square.RowPosition - 1, square.ColPosition - 2);
                if (moveSq != null) sqList.Add(moveSq);

                //add the entry to adjacency list
                AdjacencyList.Add(square, sqList);
            }
        }
 protected ChessBoardPieceGraph(ChessBoard board)
 {
     Board = board;
 }