//************************************************************************ // Freeman Lo // // This program called Knights Tour allowed any user to start at any point // on a 8 X 8 board. i think the chess piece will automatically locate // empty space as it continues to fill the entire board only once without // any kind of overlapping. I was unable to go back to work on this program // since graduate school (Dez 2005). This program will find all the possible empty space // moving forward but yet not able to backtrack at this point once it starts // to overlap. My algorithm is as follows, this algorithem is a bit messy due // to the relative complex nature of this problem. // // There are four class files together in this one program. // This Driver.java, Direction.java, Square.java and Stack.java // // I wrote this entire program sometime Sep 2005, except Stack.java // Driver.java // *********************************************************************** import java.io.*; import java.util.*; class Overflow extends Exception { } class Underflow extends Exception { //Underflow() {} } public class Driver { private static int d=0; public static void main (String[] args) throws IOException { Scanner scan = null; String line; int xc=0, yc=0; scan = new Scanner(System.in); // manually creating the 1D Direction obj Direction [] dir = new Direction[8]; // manually creating the 2D Board obj Square [][] square = new Square[8][8]; // manually creating the stack obj Stack stak = new Stack(); // max_size // inserting the Direction Values dir[0] = new Direction(-1,-2); dir[1] = new Direction(1,-2); dir[2] = new Direction(2,-1); dir[3] = new Direction(2,1); dir[4] = new Direction(-1,2); dir[5] = new Direction(1,2); dir[6] = new Direction(2,-1); dir[7] = new Direction(-2,1); // instantiating it with 0s for ( int y=0; y<8; y++) for ( int x=0; x<8; x++) square[y][x] = new Square(0, x, y,false, 0); // making sure the numbers are in it (Testing by printing) /* for ( int y=0; y<8; y++) { System.out.println(); for ( int x=0; x<8; x++) System.out.print(square[y][x].getBoolean()+" "); } */ System.out.print("Enter a x coordinate on a Chess board: (0-7)>> "); line = scan.nextLine(); try { xc = Integer.parseInt(line); } catch (NumberFormatException ex) { System.out.println("Please enter a number instead of this: "+ line); System.exit(0); } System.out.print("Enter a y coordinate on a Chess board: (0-7)>> "); line = scan.nextLine(); try { yc = Integer.parseInt(line); } catch (NumberFormatException ex) { System.out.println("Please enter a number instead of this: "+ line); System.exit(0); } if ( !(( xc >= 0 && xc < 8) && ( yc >= 0 && yc < 8)) == true ) { System.out.println("Enter in the proper range for x and y value !"); } else { // method which will place stuff on board square[yc][xc].setBoolPostDirNum(true,1,0); try { // method which will push stuff on stack stak.push(square[yc][xc]); } catch (Overflow er) { System.out.println( "Holy Cow !!! Out of Lower Bounds Exception" ); } // end catch System.out.println("Before the input of numbers."); for ( int y=0; y<8; y++) { System.out.println(); for ( int x=0; x<8; x++) System.out.print(square[y][x].getPosition()+" "); } int sqcount=2,dx=0,dy=0,origx=xc,origy=yc; boolean move=false; while ( sqcount < 65 ) { // square move = findLegalMove(square,dir,xc,yc); if (move==true) { dx = dir[d].getXinc(); dy = dir[d].getYinc(); xc = xc + dx; yc = yc + dy; square[yc][xc].setBoolPostDirNum(true,sqcount,d); try { stak.push(square[yc][xc]); } catch (Overflow er) { System.out.println( "Monkeyballs, Out of Upper Bounds Exception" ); } // end catch } // end if /* else { while (move==false) { square[yc][xc] =(Square)stak.topAndPop(); sqcount--; //System.out.println(square[yc][xc].getPosition()); move=true; } // end while } // end else */ sqcount++; } // end while System.out.println("\n\n"); for (int a=0;a<8;a++){ System.out.println(); for (int b=0;b<8;b++) System.out.print(square[a][b].getPosition()+"\t"); } } // end else (way up there) System.out.println(); } // end main // A Sweet Locating empty square Algorithm public static boolean findLegalMove(Square [][] sqtemp, Direction [] dirtemp, int xc, int yc) { int count=0,x1=0,y1=0; boolean found=false; while ( count<8 ) { x1 = dirtemp[count].getXinc(); y1 = dirtemp[count].getYinc(); x1 = xc + x1; y1 = yc + y1; if ( ((x1>=0 && x1 <=7) && (y1>=0 && y1<=7)) == true && sqtemp[y1][x1].getBoolean()!=true) { d=count; found = true; count=8; break; } // end if count++; }// end while return found; } // end method } // end class