Print an order of all the knight moves such that it fills up an 8 by 8 chess board

Print an order of all the knight moves such that it fills up an 8 by 8 chess board. The moves should be such that no block that has been stepped on is visited again.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public static int solution = 0;
        static void Main(string[] args)
        {
            int[,] chessBoard = new int[8, 8];
            for (int i = 0; i <= 0; i++)
            {
                for (int j = 0; j <= 0; j++)
                {
                    string path = "(" + i.ToString() + "," + j.ToString() + ");";
                    //Check for all possible starting position
                    ChessBoardTraversal(chessBoard, path, i, j, 1);
                }
            }
        }

        public static void ChessBoardTraversal(int[,] chessBoard, string path, int row, int col, int counter)
        {
            if (counter == (chessBoard.GetLength(0) * chessBoard.GetLength(1)))
            {
                Regex reg = new Regex(";");
                string[] coordinates = reg.Split(path);
                Console.WriteLine("Solution No. " + (++solution));
                //Display the traversal path
                Console.WriteLine("============================");
                for (int k = 0, count = 1; k < coordinates.Length - 1; k++)
                {
                    Regex r = new Regex(",");
                    string[] xy = r.Split(coordinates[k].Substring(1, coordinates[k].Length - 2));
                    for (int i = 0; i < chessBoard.GetLength(0); i++)
                    {
                        for (int j = 0; j < chessBoard.GetLength(1); j++)
                        {
                            if (int.Parse(xy[0]) == i && int.Parse(xy[1]) == j)
                            {
                                chessBoard[i, j] = count;
                                count++;
                            }
                        }
                    }
                }
                for (int i = 0; i < chessBoard.GetLength(0); i++)
                {
                    for (int j = 0; j < chessBoard.GetLength(1); j++)
                    {
                        Console.Write(chessBoard[i, j].ToString().PadLeft(2) + " ");
                        chessBoard[i, j] = 0;
                    }
                    Console.WriteLine();
                }
                return;
            }

            //Move4
            if (row + 1 < chessBoard.GetLength(0) && col + 2 < chessBoard.GetLength(1) && !path.Contains("(" + (row + 1).ToString() + "," + (col + 2).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row + 1).ToString() + "," + (col + 2).ToString() + ");", row + 1, col + 2, counter + 1);
            }

            //Move5
            if (row + 2 < chessBoard.GetLength(0) && col + 1 < chessBoard.GetLength(1) && !path.Contains("(" + (row + 2).ToString() + "," + (col + 1).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row + 2).ToString() + "," + (col + 1).ToString() + ");", row + 2, col + 1, counter + 1);
            }

            //Move6
            if (row + 2 < chessBoard.GetLength(0) && col - 1 >= 0 && !path.Contains("(" + (row + 2).ToString() + "," + (col - 1).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row + 2).ToString() + "," + (col - 1).ToString() + ");", row + 2, col - 1, counter + 1);
            }

            //Move7
            if (row + 1 < chessBoard.GetLength(0) && col - 2 >= 0 && !path.Contains("(" + (row + 1).ToString() + "," + (col - 2).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row + 1).ToString() + "," + (col - 2).ToString() + ");", row + 1, col - 2, counter + 1);
            }

            //Move8
            if (row - 1 >= 0 && col - 2 >= 0 && !path.Contains("(" + (row - 1).ToString() + "," + (col - 2).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row - 1).ToString() + "," + (col - 2).ToString() + ");", row - 1, col - 2, counter + 1);
            }

            //Move1
            if (row - 2 >= 0 && col - 1 >= 0 && !path.Contains("(" + (row - 2).ToString() + "," + (col - 1).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row - 2).ToString() + "," + (col - 1).ToString() + ");", row - 2, col - 1, counter + 1);
            }

            //Move2
            if (row - 2 >= 0 && col + 1 < chessBoard.GetLength(1) && !path.Contains("(" + (row - 2).ToString() + "," + (col + 1).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row - 2).ToString() + "," + (col + 1).ToString() + ");", row - 2, col + 1, counter + 1);
            }

            //Move3
            if (row - 1 >= 0 && col + 2 < chessBoard.GetLength(1) && !path.Contains("(" + (row - 1).ToString() + "," + (col + 2).ToString() + ")") && solution == 0)
            {
                ChessBoardTraversal(chessBoard, path + "(" + (row - 1).ToString() + "," + (col + 2).ToString() + ");", row - 1, col + 2, counter + 1);
            }
        }
    }
}

Output
=======
Solution No. 1
============================
 1 38 59 36 43 48 57 52
60 35  2 49 58 51 44 47
39 32 37 42  3 46 53 56
34 61 40 27 50 55  4 45
31 10 33 62 41 26 23 54
18 63 28 11 24 21 14  5
 9 30 19 16  7 12 25 22
64 17  8 29 20 15  6 13
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s