knight's tour problem java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.ArrayList;
public class KT extends Applet
{
public final static int DELAY = 500;
private Thread thd;
public void init ()
{
Label lblTitle = new Label ("Knight's Tour", Label.CENTER);
lblTitle.setFont (new Font ("Arial", Font.BOLD, 18));
add (lblTitle);
final ChessBoard cb = new ChessBoard (this);
// Add the ChessBoard object to the applet's Panel container.
add (cb);
Panel pnl = new Panel ();
pnl.add (new Label ("Choose starting position:"));
final Choice c = new Choice ();
c.add ("Upperleft corner");
c.add ("Upperright corner");
c.addItemListener (new ItemListener ()
{
public void itemStateChanged (ItemEvent e)
{
Choice c = (Choice) e.getSource ();
if (c.getSelectedIndex () == 0)
cb.moveKnight (1);
else
cb.moveKnight (8);
cb.reset ();
} });
pnl.add (c);
add (pnl);
final Button btn = new Button ("Take the Tour");
ActionListener al;
al = new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
Runnable r;
r = new Runnable ()
{
int [] boardPositions1 =
{
1, 18, 33, 50, 60, 54, 64, 47, 32, 15, 5, 11, 17, 34, 49, 59,
53, 63, 48, 31, 16, 6, 12, 2, 19, 25, 42, 57, 51, 61, 55, 40,
23, 8, 14, 4, 10, 27, 44, 38, 21, 36, 46, 29, 35, 41, 58, 52,
62, 56, 39, 24, 7, 13, 3, 9,26, 43, 37, 22, 28, 45, 30, 20
};
int [] boardPositions2 =
{

8, 23, 40, 55, 61, 51, 57, 42, 25, 10, 4, 14, 24, 39, 56, 62,
52, 58, 41, 26, 9, 3, 13, 7, 22, 32, 47, 64, 54, 60, 50, 33,
18, 1, 11, 5, 15, 30, 45, 35,0, 37, 43, 28, 38, 48, 63, 53,
59, 49, 34, 17, 2, 12, 6, 16, 31, 46, 36, 19, 29, 44, 27, 21
};
public void run ()
{
cb.reset ();
for (int i = 0; i < boardPositions1.length &&
thd != null; i++)
{
if (c.getSelectedIndex () == 0)
cb.moveKnight (boardPositions1 [i]);
else
cb.moveKnight (boardPositions2 [i]);
try
{
Thread.sleep (DELAY);
}
catch (InterruptedException e2)
{
}
}
c.setEnabled (true);
btn.setEnabled (true);
}
};
c.setEnabled (false);
btn.setEnabled (false);
thd = new Thread (r);
thd.start ();
}
};
btn.addActionListener (al);
add (btn);
}
public void stop ()
{
thd = null;
}
}
final class ChessBoard extends Canvas
{
private final static Color SQUARECOLOR = new Color (195, 214, 242);
private final static int SQUAREDIM = 40;
private final static int BOARDDIM = 8 * SQUAREDIM + 2;
private int boardx;
private int boardy;
private int width;
private Image imBuffer;
private Graphics imG;
private Image imKnight;
private int knightWidth;
private int knightHeight;
private ArrayList trail;
private int ox;
private int oy;
Applet a;
public Dimension getPreferredSize ()
{
return new Dimension (width, height);
}
public void moveKnight (int boardPosition)
{
if (boardPosition < 1 || boardPosition > 64)
throw new IllegalArgumentException ("invalid board position: " +
boardPosition);
int rebasedBoardPosition = boardPosition-1;
int col = rebasedBoardPosition % 8;
int row = rebasedBoardPosition / 8;
ox = boardx + col * SQUAREDIM + (SQUAREDIM - knightWidth) / 2 + 1;
oy = boardy + row * SQUAREDIM + (SQUAREDIM - knightHeight) / 2 + 1;
trail.add (new Point (ox + knightWidth / 2, oy + knightHeight / 2));
repaint ();
}
public void paint (Graphics g)
{
paintChessBoard (imG, boardx, boardy);
paintKnight (imG, ox, oy);
paintTrail (imG);
g.drawImage (imBuffer, 0, 0, this);
}
void paintChessBoard (Graphics g, int x, int y)
{
g.setColor (Color.black);
g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1);
void paintKnight (Graphics g, int x, int y)
{
g.drawImage (imKnight, x, y, a);
}
void paintTrail (Graphics g)
{
g.setColor (Color.black);
int len = trail.size ();
if (len == 0)
return;
Point pt = (Point) trail.get (0);
int ox = pt.x;
int oy = pt.y;
for (int i = 1; i < len; i++)
{
pt = (Point) trail.get (i);
g.drawLine (ox, oy, pt.x, pt.y);
ox = pt.x;
oy = pt.y;

}

}
public void reset ()
{
trail.clear ();
}
public void update (Graphics g)
{
paint (g);
}
}
knight's tour problem java OUTPUT :

knight's tour problem java

knight's tour problem java

Facing difficulties in understanding the program ?

Ask Your Queries in Comment Box

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *