Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class trialagainKT extends Frame {
public static void main(String[] args){
int x;
int y;
String Number1;
String Number2;
Number1 =JOptionPane.showInputDialog("enter x: ");
Number2 =JOptionPane.showInputDialog("enter Y: ");
x = Integer.parseInt(Number1);
y = Integer.parseInt(Number2);
PerformKT(x, y);
}
public static void PerformKT(int currentRow, int currentColumn) {
if (currentRow > 7 || currentRow < 0 || currentColumn > 7 || currentColumn < 0) {
JOptionPane.showMessageDialog(null,"Invalid Start Square ","Try Again",JOptionPane.PLAIN_MESSAGE);
}
//initialization of board
int chessBoard[][] = new int[8][8];
int horizontal[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int counter = 1;
boolean legalMove[] = new boolean[8];
int moveNumber = 0;
int accessibility[][] =
{{2,3,4,4,4,4,3,2},{3,4,6,6,6,6,3,4},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},
{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},{3,4,6,6,6,6,3,4},{2,3,4,4,4,4,3,2}};
chessBoard[currentRow][currentColumn] = counter;
for(counter = 2; counter <= 64; counter++) {
for(int i = 0; i < 8; i++) {
legalMove[i] = false; //reset leaglMove array to false
}
moveNumber = 0; //reset moveNumber to 0
//check the possible moves for the knight
while (moveNumber < 8) {
int checkR = currentRow + vertical[moveNumber];
int checkC = currentColumn + horizontal[moveNumber];
if (checkR >= 0 && checkR <= 7 && checkC >= 0 && checkC <= 7) {
//check if legal move is occupied
if (chessBoard[checkR][checkC] == 0) {
accessibility[checkR][checkC]--;
legalMove[moveNumber] = true;
} else {
legalMove[moveNumber] = false;
}
} else {
legalMove[moveNumber] = false;
}
++moveNumber;
}
//what is the best move?
int bestAccess = 8;
int bestMove = -1;
for(int i = 0; i < 8; i++) {
if(legalMove[i] == true) {
int checkR = currentRow + vertical[i];
int checkC = currentColumn + horizontal[i];
if (accessibility[checkR][checkC] <= bestAccess) {
bestAccess = accessibility[checkR][checkC];
bestMove = i; //Store first legal move with lowest accessibility
}
}
}
//move the knight
currentRow += vertical[bestMove];
currentColumn += horizontal[bestMove];
chessBoard[currentRow][currentColumn] = counter;
}
//this is what i want to put in the frame
System.out.println();
System.out.println(" 00 01 02 03 04 05 06 07 ");
System.out.println("*---*---*---*---*---*---*---*---*");
for(int i=0; i < chessBoard.length; i++) {
for(int j=0; j < chessBoard[i].length; j++) {
System.out.printf("|%3d", chessBoard[i][j]);
}
System.out.println("|");
System.out.println("| | | | | | | | |");
System.out.println("*---*---*---*---*---*---*---*---*");
}
//this is for the frame
JTextArea outputArea = new JTextArea (15,30);
outputArea.setFont(new Font("Courier",Font.BOLD,16));
JScrollPane scroller = new JScrollPane(outputArea);
//no particular output yet
outputArea.setText();
String title="Complete Knight's Tour";
if(counter<64){title="Knight's Tour with "+counter+" Moves";
}
else {
title="Knight's Tour Complete";
}
JOptionPane.showMessageDialog(null,scroller,title,JOptionPane.INFORMATION_MESSAGE);
}
}
Bookmarks