import java.io.*; import java.text.DecimalFormat; public class Lab26b { public static void main (String args[]) throws IOException { System.out.println("\nLab26b 90/100 Point Version\n"); Knight knight = new Knight(); knight.getStart(); knight.solveTour(); knight.displayBoard(); } } class Knight { private int board[][]; // stores the sequence of knight moves private boolean used[][]; private int startRow; // row location where the knight starts private int startCol; // col location where the knight starts private int rowPos; // current row position of the knight private int colPos; // current col position of the knight private int moves; // number of location visited by the knight BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); DecimalFormat f = new DecimalFormat("00"); boolean debug = true; /*// USED WITH THE 100 POINT VERSION final private int ACCESS[][] = {{0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,2,3,4,4,4,4,3,2,0,0}, {0,0,3,4,6,6,6,6,4,3,0,0}, {0,0,4,6,8,8,8,8,6,4,0,0}, {0,0,4,6,8,8,8,8,6,4,0,0}, {0,0,4,6,8,8,8,8,6,4,0,0}, {0,0,4,6,8,8,8,8,6,4,0,0}, {0,0,3,4,6,6,6,6,4,3,0,0}, {0,0,2,3,4,4,4,4,3,2,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0}}; */ public Knight() // constructor used to initializes the data attributes { board = new int[8][8]; used = new boolean [8][8]; startRow = 0; startCol = 0; moves = 1; } public void getStart() throws IOException // input method to get starting row and col from keyboard entry { boolean invalid = true; while(invalid) { System.out.print("Enter row start ==>> "); startRow = Integer.parseInt(b.readLine()); if(startRow < 1 || startRow > 8) invalid = true; else invalid = false; } invalid = true; while(invalid) { System.out.print("Enter col start ==>> "); startCol = Integer.parseInt(b.readLine()); if(startCol < 1 || startCol > 8) invalid = true; else invalid = false; } rowPos = startRow-1; colPos = startCol-1; System.out.println(); } public void displayBoard() // displays the chessboard after the tour is concluded { for(int r=0; r<8;r++) { for(int c=0; c<8;c++) System.out.print(f.format(board[c][r]) + " "); System.out.println(); } System.out.println("\nThe knight made " + moves + " moves"); } private boolean getMove() // computes the next available knight's move. Alters RowPos and ColPos and // returns true if move is possible, otherwise returns false { if(colPos+2 < 8 && rowPos+1 < 8) // if the knight can go RIGHT and DOWN...return true { // GROUP 1 colPos +=2; rowPos +=1; if(debug) System.out.println("R D"); return true; //check } if(colPos+1 < 8 && rowPos+2 < 8) // if the knight can go DOWN and RIGHT...return true { // GROUP 2 colPos +=1; rowPos +=2; if(debug) System.out.println("D R"); return true; //check } if(colPos+1 < 8 && rowPos-2 >= 0) // if the knight can go UP and RIGHT...return true { // GROUP 3 colPos +=1; rowPos -=2; if(debug) System.out.println("U R"); return true; // check } if(colPos+2 < 8 && rowPos-1 >= 0) // if the knight can go RIGHT and UP...return true { // GROUP 4 colPos +=2; rowPos -=1; if(debug) System.out.println("R U"); return true; //check } else if(colPos-2 >= 0 && rowPos-1 >= 0) // if the knight can go LEFT and UP...return true { // GROUP 1 colPos -=2; rowPos -=1; if(debug) System.out.println("L U"); return true; } else if(colPos-1 >= 0 && rowPos-2 >= 0) // if the knight can go UP and LEFT...return true { // GROUP 2 colPos -=1; rowPos -=2; if(debug) System.out.println("U L"); return true; } else if(colPos-1 >= 0 && rowPos+2 < 8) // if the knight can go DOWN and LEFT...return true { // GROUP 3 colPos -=1; rowPos +=2; if(debug) System.out.println("D L"); return true; //check } else if(colPos-2 >= 0 && rowPos+1 < 8) // if the knight can go LEFT and DOWN...return true { // GROUP 4 colPos -=2; rowPos +=1; if(debug) System.out.println("L D"); return true; } return false; // default return } public void solveTour() // primary method that drives the knight's tour solution { board[colPos][rowPos] = moves; do { moves++; board[colPos][rowPos] = moves; used[colPos][rowPos] = true; } while(getMove()); } }