📄 knightstour.java
字号:
import java.applet.Applet;
import java.awt.*;
public class KnightsTour extends Applet
{
public void start()
{
MOUSE = false;
cnt = 1;
brd.resetBoard();
x = 0;
y = 7;
knightx = (int)((double)dim * 0.10000000000000001D);
knighty = dim * y + (int)((double)dim * 0.050000000000000003D);
repaint();
}
public void compute()
{
brd.setCount(y, x, cnt++);
brd.setKnightTrue(y, x);
brd.computeValue();
brd.computeBestMove(c);
repaint();
}
public boolean mouseDown(Event evt, int x2, int y2)
{
if(x2 > dim * 8 || y2 > dim * 8)
return false;
if(!MOUSE)
{
x = x2 / dim;
y = y2 / dim;
if(x >= 8)
x = 7;
if(y >= 8)
y = 7;
knightx = dim * x + (int)((double)dim * 0.10000000000000001D);
knighty = dim * y + (int)((double)dim * 0.050000000000000003D);
if((y == 7 && x == 4 || y == 1 && x == 6 || y == 0 && x == 4) && cnt == 1)
c = 0;
compute();
}
MOUSE = true;
return true;
}
public KnightsTour()
{
c = 1;
}
public void pressNext()
{
if(brd.getBestx(y, x) == -1)
{
return;
} else
{
int tmp = x;
x = brd.getBesty(y, tmp);
y = brd.getBestx(y, tmp);
knightx = dim * x + (int)((double)dim * 0.10000000000000001D);
knighty = dim * y + (int)((double)dim * 0.050000000000000003D);
compute();
return;
}
}
public boolean action(Event evt, Object obj)
{
if((evt.target instanceof Button) && MOUSE)
{
String buttonName = (String)obj;
if(buttonName.equals("Next "))
pressNext();
else
if(buttonName.equals("Solve"))
while(cnt != 65)
pressNext();
else
if(buttonName.equals("Reset"))
start();
}
return true;
}
public void init()
{
resize(304, 365);
Dimension d = size();
setForeground(Color.cyan);
setBackground(Color.white);
setLayout(new BorderLayout());
width = d.width;
height = d.height;
dim = width / 8;
brd = new Board(width >= height ? height : width);
x = 0;
y = 7;
Knight = getImage(getCodeBase(), "images/knight.gif");
knightx = (int)((double)dim * 0.10000000000000001D);
knighty = dim * y + (int)((double)dim * 0.050000000000000003D);
next = new Button("Next ");
solve = new Button("Solve");
reset = new Button("Reset");
Font font = new Font("Times", 1, 14);
next.setForeground(Color.blue);
next.setBackground(Color.white);
solve.setForeground(Color.blue);
solve.setBackground(Color.white);
reset.setForeground(Color.blue);
reset.setBackground(Color.white);
Panel p = new Panel();
p.add(next);
p.add(solve);
p.add(reset);
add("South", p);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
brd.paint(g);
if(Knight != null)
g.drawImage(Knight, knightx - (int)((double)dim * 0.20000000000000001D), knighty - (int)((double)dim * 0.14999999999999999D), (int)((double)dim * 1.3D), (int)((double)dim * 1.2D), this);
}
private int width;
private int height;
private int dim;
private Image Knight;
private int knightx;
private int knighty;
private int x;
private int y;
private Board brd;
private static int cnt = 1;
private boolean MOUSE;
private int c;
private Button next;
private Button solve;
private Button reset;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -