Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Knight\'s Tour is a well-known classic problem. The objective is to move a k

ID: 3748842 • Letter: T

Question

The Knight's Tour is a well-known classic problem. The objective is to move a knight, starting from any square on a chessboard, to every other square once. Note that the knight makes only L- shape moves (two spaces in one direction and one space in a perpendicular direction). As shown in figure on the left, the knight can move to eight squares. The figure on the right contains a solution of the Knight's Tour starting from the original location of (0, 4). Write a Java program which reads the length of the square and generate all the knight's moves on the square board (the figures below show the strategy and a solution for a 8-length square, that is, a chessboard). Hint. To solve the Knight's Tour problem for the 8x8 board, create a graph with 64 vertices representing all the squares in the chess connected if a knight can move between the two vertices. A solution starting from the point (0 0) is illustrated in the below figure on the right. board (the below figure on the left). Two vertices are

Explanation / Answer

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace KnightTourChallenge
{
class Program
{
public static void Main(string[] args)
{
Console.Write("Width and height of chess board: ");
int width, height;
width = height = int.Parse(Console.ReadLine());
Console.Write("Starting square (format: x,y ; zero-based): ");
string[] coordinateParts = Console.ReadLine().Split(new char[] { ',' }, 2);
Coordinate startingSquare = new Coordinate(int.Parse(coordinateParts[0]), int.Parse(coordinateParts[1]));
Console.Write("Output image? (gif, final, none): ");
string outputImage = Console.ReadLine();
string outputImageFilePath = null;
int imageWidth = -1;
int imageHeight = -1;
if (outputImage == "gif" || outputImage == "final")
{
Console.Write("Output image file path? ");
outputImageFilePath = Console.ReadLine();
if (File.Exists(outputImageFilePath))
{
Console.WriteLine("WARNING! That file already exists, so this application will overwrite it. Quit if you don't want to do this.");
}
Console.Write("Output image width? ");
imageWidth = int.Parse(Console.ReadLine());
Console.Write("Output image height? ");
imageHeight = int.Parse(Console.ReadLine());
}
KnightBoard board = new KnightBoard(width, height, startingSquare);
if (!board.TourExists())
{
Console.WriteLine("There is no tour for this board.");
return;
}
if (!board.MakeTour())
{
Console.WriteLine(string.Join(" ", board.Path.Select(x => x.ToString())));
Console.WriteLine("I'm stuck :(");
return;
}
Console.WriteLine(string.Join(" ", board.Path.Select(x => x.ToString())));
if (outputImage == "gif")
{
Console.WriteLine("Generating GIF...");
try
{
BoardDrawing.CreateGif(board.Path, imageWidth, imageHeight, width, height, outputImageFilePath);
}
catch (ArgumentException)
{
Console.WriteLine("An ArgumentException occured. It looks like this happens when the GIF generation takes too much memory. Sorry!");
}
Process.Start(outputImageFilePath);
}
else if (outputImage == "final")
{
Console.WriteLine("Generating image...");
BoardDrawing.Draw(board.Path, imageWidth, imageHeight, width, height, outputImageFilePath);
Process.Start(outputImageFilePath);
}
}
}
}

Configuring the App using XML :

-----------------------------------------------

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace KnightTourChallenge
{
class Program
{
public static void Main(string[] args)
{
Console.Write("Width and height of chess board: ");
int width, height;
width = height = int.Parse(Console.ReadLine());
Console.Write("Starting square (format: x,y ; zero-based): ");
string[] coordinateParts = Console.ReadLine().Split(new char[] { ',' }, 2);
Coordinate startingSquare = new Coordinate(int.Parse(coordinateParts[0]), int.Parse(coordinateParts[1]));
Console.Write("Output image? (gif, final, none): ");
string outputImage = Console.ReadLine();
string outputImageFilePath = null;
int imageWidth = -1;
int imageHeight = -1;
if (outputImage == "gif" || outputImage == "final")
{
Console.Write("Output image file path? ");
outputImageFilePath = Console.ReadLine();
if (File.Exists(outputImageFilePath))
{
Console.WriteLine("WARNING! That file already exists, so this application will overwrite it. Quit if you don't want to do this.");
}
Console.Write("Output image width? ");
imageWidth = int.Parse(Console.ReadLine());
Console.Write("Output image height? ");
imageHeight = int.Parse(Console.ReadLine());
}
KnightBoard board = new KnightBoard(width, height, startingSquare);
if (!board.TourExists())
{
Console.WriteLine("There is no tour for this board.");
return;
}
if (!board.MakeTour())
{
Console.WriteLine(string.Join(" ", board.Path.Select(x => x.ToString())));
Console.WriteLine("I'm stuck :(");
return;
}
Console.WriteLine(string.Join(" ", board.Path.Select(x => x.ToString())));
if (outputImage == "gif")
{
Console.WriteLine("Generating GIF...");
try
{
BoardDrawing.CreateGif(board.Path, imageWidth, imageHeight, width, height, outputImageFilePath);
}
catch (ArgumentException)
{
Console.WriteLine("An ArgumentException occured. It looks like this happens when the GIF generation takes too much memory. Sorry!");
}
Process.Start(outputImageFilePath);
}
else if (outputImage == "final")
{
Console.WriteLine("Generating image...");
BoardDrawing.Draw(board.Path, imageWidth, imageHeight, width, height, outputImageFilePath);
Process.Start(outputImageFilePath);
}
}
}
}

Co-ordinates :

--------------------

using System;

namespace KnightTourChallenge
{
public class Coordinate : IEquatable<Coordinate>
{
public int X
{
get;
private set;
}

public int Y
{
get;
private set;
}

public Coordinate(int x, int y)
{
X = x;
Y = y;
}

public override string ToString()
{
return string.Format("({0},{1})", X, Y);
}

public override bool Equals(object obj)
{
if (obj == null || !(obj is Coordinate))
{
return false;
}
return Equals(obj as Coordinate);
}

public bool Equals(Coordinate other)
{
return other != null && this.X == other.X && this.Y == other.Y;
}

public override int GetHashCode()
{
return new { X = X, Y = Y }.GetHashCode();
}
}
}

Knight Board :

---------------------

using System;

namespace KnightTourChallenge
{
public class Coordinate : IEquatable<Coordinate>
{
public int X
{
get;
private set;
}

public int Y
{
get;
private set;
}

public Coordinate(int x, int y)
{
X = x;
Y = y;
}

public override string ToString()
{
return string.Format("({0},{1})", X, Y);
}

public override bool Equals(object obj)
{
if (obj == null || !(obj is Coordinate))
{
return false;
}
return Equals(obj as Coordinate);
}

public bool Equals(Coordinate other)
{
return other != null && this.X == other.X && this.Y == other.Y;
}

public override int GetHashCode()
{
return new { X = X, Y = Y }.GetHashCode();
}
}
}

Board Drawing :

-------------------------------

using System;

namespace KnightTourChallenge
{
public class Coordinate : IEquatable<Coordinate>
{
public int X
{
get;
private set;
}

public int Y
{
get;
private set;
}

public Coordinate(int x, int y)
{
X = x;
Y = y;
}

public override string ToString()
{
return string.Format("({0},{1})", X, Y);
}

public override bool Equals(object obj)
{
if (obj == null || !(obj is Coordinate))
{
return false;
}
return Equals(obj as Coordinate);
}

public bool Equals(Coordinate other)
{
return other != null && this.X == other.X && this.Y == other.Y;
}

public override int GetHashCode()
{
return new { X = X, Y = Y }.GetHashCode();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote