Help with Knights Tour program
843810Apr 20 2005 — edited Apr 21 2005I am trying to write a program that conducts a Knights Tour - i.e. moves a chess piece randomly around a chessboard 64 times attempting to hit each space only once. The problem is I cant figure out how to make the knight move. I have drawn the chessboard, and 64 different images where the knight is in a different square, yet when I try to re-draw the next image overtop when the knight moves, nothing happens. Is there something wrong with my code? Originally I had an array developed with a random number generator and it worked, but now I am trying to convert the whole program into a GUI and running into trouble. I have pasted my program below.. Any help or guidance would be appreciated. Thanks!
-Mark
// Draw Knights Board
import java.awt.Color;
import javax.swing.JFrame;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;
public class KnightsTour2 extends JPanel
{
// execute application
public static void main( String args[] )
{
int board [][];
board = 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 moveNumber;
int currentRow;
int currentColumn;
Random randomNumbers = new Random();
int key;
// create frame for KnightsBoard
JFrame frame =
new JFrame( "The Knight's Tour" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
KnightsBoard3 knightsBoard3 = new KnightsBoard3();
KnightsBoard4 knightsBoard4 = new KnightsBoard4();
KnightsBoard5 knightsBoard5 = new KnightsBoard5();
knightsBoard3.setBackground( Color.WHITE );
frame.add( knightsBoard3 ); // add panel to frame
frame.add( knightsBoard4 ); //add 2nd knight
//frame.add( knightsBoard5 ); //add 2nd knight
frame.setSize( 425, 450 ); // set frame size
frame.setVisible( true ); // display frame
}
//Move Knight
public static void moveKnight( int board[][] )
{
int horizontal[] = {2, 1, -1, -2, -2, -1, 1, 2 };
int vertical[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int moveNumber;
int currentRow;
int currentColumn;
Random randomNumbers = new Random();
int key;
//Starting Point for Knight
currentRow = 3;
currentColumn = 3;
System.out.println( "\nStarting Point for Knight:\n" );
System.out.printf( "Row: %d\nColumn: %d\n", currentRow + 1, currentColumn + 1 );
for ( int counter3 = 0 ; counter3 < 1; counter3 ++)
board[currentRow] [currentColumn ]= counter3 + 1;
//Get Random Numbers for Random Movement of Knight
for ( int counter = 1; counter < 10; counter++ )
{
moveNumber = randomNumbers.nextInt( 8 );
currentRow += vertical[ moveNumber ];
currentColumn += horizontal[ moveNumber ];
//Check if Knight is still on the Board
if ( currentRow < 0 )
{
//Knight is off the board
currentRow -= vertical [moveNumber ];
currentColumn -= horizontal [moveNumber ];
while ( currentRow < 0 )
{
System.out.printf( "Knight is off board Row too low, location is %d, %d", currentRow, currentColumn);
moveNumber = randomNumbers.nextInt( 8 );
currentRow += vertical[ moveNumber ];
}
//System.out.printf( "New location is %d, %d", currentRow, currentColumn);
//break;
}
if ( currentRow > 7 )
{
//Knight is off the board
currentRow -= vertical [moveNumber ];
currentColumn -= horizontal [moveNumber ];
while ( currentRow > 7 )
{
System.out.printf( "Knight is off board Row too high, location is %d, %d", currentRow, currentColumn);
moveNumber = randomNumbers.nextInt( 8 );
currentRow += vertical[ moveNumber ];
}
//System.out.printf( "New location is %d, %d", currentRow, currentColumn);
//break;
}
if ( currentColumn < 0 )
{
currentRow -= vertical [moveNumber ];
currentColumn -= horizontal [moveNumber ];
while ( currentColumn < 0 )
{
System.out.printf( "Knight is off board Column too low, location is %d, %d", currentRow, currentColumn);
moveNumber = randomNumbers.nextInt( 8 );
currentColumn += vertical[ moveNumber ];
}
//System.out.printf( "New location is %d, %d", currentRow, currentColumn);
//break;
}
if ( currentColumn > 7 )
{
currentRow -= vertical [moveNumber ];
currentColumn -= horizontal [moveNumber ];
while ( currentColumn > 7 )
{
System.out.printf( "Knight is off board Column too high, location is %d, %d", currentRow, currentColumn);
moveNumber = randomNumbers.nextInt( 8 );
currentColumn += vertical[ moveNumber ];
}
//System.out.printf( "New location is %d, %d", currentRow, currentColumn);
//break;
}
}
}
}
Here is the KnightsBoard that is called from above..
// Drawing Knights Board
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class KnightsBoard4 extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // call superclass's paint method
this.setBackground( Color.WHITE );
g.setColor( Color.BLACK );
//First Row of Board
g.drawRect( 5, 5, 50, 49 );
g.fillRect( 50, 5, 50, 50 );
g.drawRect( 100, 5, 50, 49 );
g.fillRect( 150, 5, 50, 50 );
g.drawRect( 200, 5, 50, 49 );
g.fillRect( 250, 5, 50, 50 );
g.drawRect( 300, 5, 50, 49 );
g.fillRect( 350, 5, 50, 50 );
//End of First Row
//Start Second Row
g.fillRect( 5, 55, 45, 50 );
g.drawRect( 50, 55, 50, 49 );
g.fillRect( 101, 55, 50, 50 );
g.drawRect( 150, 55, 49, 49 );
g.fillRect( 200, 55, 50, 50 );
g.drawRect( 250, 55, 50, 49 );
g.fillRect( 300, 55, 50, 50 );
g.drawRect( 350, 55, 49, 49 );
//End of Second Row
//Start Third Row
g.drawRect( 5, 105, 50, 49 );
g.fillRect( 50, 105, 50, 50 );
g.drawRect( 100, 105, 50, 49 );
g.fillRect( 150, 105, 50, 50 );
g.drawRect( 200, 105, 50, 50 );
g.fillRect( 250, 105, 50, 50 );
g.drawRect( 300, 105, 50, 50 );
g.fillRect( 350, 105, 50, 50 );
//End of Third Row
//Start Fourth Row
g.fillRect( 5, 155, 45, 50 );
g.drawRect( 50, 155, 50, 49 );
g.fillRect( 101, 155, 50, 50 );
g.drawRect( 150, 155, 49, 49 );
g.fillRect( 200, 155, 50, 50 );
g.drawRect( 250, 155, 49, 49 );
g.fillRect( 300, 155, 50, 50 );
g.drawRect( 350, 155, 49, 49 );
//End of Fourth Row
//Start Fifth Row
g.drawRect( 5, 205, 50, 49 );
g.fillRect( 50, 205, 50, 50 );
g.drawRect( 100, 205, 50, 49 );
g.fillRect( 150, 205, 50, 50 );
g.drawRect( 200, 205, 50, 49 );
g.fillRect( 250, 205, 50, 50 );
g.drawRect( 300, 205, 50, 49 );
g.fillRect( 350, 205, 50, 50 );
//End of Fifth Row
//Start Sixth Row
g.fillRect( 5, 255, 45, 50 );
g.drawRect( 50, 255, 50, 49 );
g.fillRect( 101, 255, 50, 50 );
g.drawRect( 150, 255, 49, 49 );
g.fillRect( 200, 255, 50, 50 );
g.drawRect( 250, 255, 50, 49 );
g.fillRect( 300, 255, 50, 50 );
g.drawRect( 350, 255, 49, 49 );
//End of Sixth Row
//Start Seventh Row
g.drawRect( 5, 305, 50, 49 );
g.fillRect( 50, 305, 50, 50 );
g.drawRect( 100, 305, 50, 49 );
g.fillRect( 150, 305, 50, 50 );
g.drawRect( 200, 305, 50, 49 );
g.fillRect( 250, 305, 50, 50 );
g.drawRect( 300, 305, 50, 49 );
g.fillRect( 350, 305, 50, 50 );
//End of Seventh Row
//Start Eighth Row
g.fillRect( 5, 355, 45, 50 );
g.drawRect( 50, 355, 50, 49 );
g.fillRect( 101, 355, 50, 50 );
g.drawRect( 150, 355, 49, 49 );
g.fillRect( 200, 355, 50, 50 );
g.drawRect( 250, 355, 50, 49 );
g.fillRect( 300, 355, 50, 50 );
g.drawRect( 350, 355, 49, 49 );
//End of Eighth Row
//Draw Knight in Square 4
g.setColor( Color.WHITE );
g.fillRect( 160, 43, 25, 10 );
g.fillRect( 165, 38, 15, 5 );
g.fillRect( 160, 28, 25, 10 );
g.fillOval( 160, 23, 25, 10 );
g.fillRect( 168, 20, 9, 5 );
g.fillOval( 165, 7, 15, 15 );
} // end Chess Board
}