CMPS 144 Fall 2023
Prog. Assg. #3: Knight Distances on a Chessboard
Due: 11:59pm, November 5

  +---+---+---+---+---+---+---+---+
0 |   |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+---+
1 |   |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+---+
2 |   | * |   | * |   |   |   |   |
  +---+---+---+---+---+---+---+---+
3 | * |   |   |   | * |   |   |   |
  +---+---+---+---+---+---+---+---+
4 |   |   | K |   |   |   |   |   |
  +---+---+---+---+---+---+---+---+
5 | * |   |   |   | * |   |   |   |
  +---+---+---+---+---+---+---+---+
6 |   | * |   | * |   |   |   |   |
  +---+---+---+---+---+---+---+---+
7 |   |   |   |   |   |   |   |   |
  +---+---+---+---+---+---+---+---+
    0   1   2   3   4   5   6   7
In the game of chess, a knight moves in a unique way: either two squares horizontally and one square vertically or else one square horizontally and two squares vertically. As an example, consider the chessboard shown to the right. The square at location (4,2) (i.e., row 4, column 2) is marked with a K to indicate the presence of a knight. The squares marked with *'s indicate those squares to which that knight can move.

For two locations (i,j) and (k,m), define the distance between them to be the minimum number of moves that a knight would have to make to get from one to the other.

The problem addressed in this assignment is this:

Given a starting location (i,j), compute, for each location (k,m) on the chessboard, the distance between (i,j) and (k,m).

Actually, the problem addressed here is a generalization of that, because we are going to take not only the starting location to be a parameter of the problem, but also the dimensions of the chessboard (i.e., # of rows and # of columns).

You should recognize this problem as being nothing more than a variation of a problem that we already have studied in this course, namely the Uniform-cost Single-source Shortest Paths problem. Here, the vertices of the graph are identified by ordered pairs (indicating a row and column) rather than by single numbers. Another difference is that here the edges are not represented explicitly (in, say, adjacency lists), but rather are determined by a rule. (Namely, that rule says that there are edges (in both directions) connecting vertices (i,j) and (k,m) iff either |k-i|=1 and |m-j|=2 or else |k-i|=2 and |m-j|=1.)

Despite these superficial differences, the algorithm for solving the problem remains the same.

Provided are these Java artifacts, one of which is left for you to complete.

An example execution of KD_Tester is shown below. It prompts the user to enter the dimensions of the board and the starting location. In response, it displays the board, with each square showing its distance from the starting location.

$ java KD_Tester
Enter # rows on the board: 6
Enter # columns on the board: 5
Enter row coordinate of starting location: 4
Enter column coordinate of starting location: 2
  2  3  2  3  2
  3  2  3  2  3
  4  1  2  1  4
  1  2  3  2  1
  2  3  0  3  2
  1  2  3  2  1

Extra Credit: For a few bonus points, make whatever enhancements are necessary so as to complete the pathTo() method in the KnightDistances class. Any reasonable approach will involve the introduction of a new instance variable. Notice that the KD_Tester application has a commented out call to a method that displays the results of calling pathTo().