import java.awt.*;
import Demo.Demo;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.*;
class KnightTourPanel extends JPanel {
public KnightTourPanel(){
super();
setSize(550,550);
setVisible(true);
}
}
class PanelDesign extends KnightTourPanel{
JPanel p1,p2,p3;
ScrollPane p4;
public PanelDesign(){
setLayout(null);
setBackground(Color.gray);
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new ScrollPane();
p4=new ScrollPane();
p2.setBackground(Color.darkGray);
p1.setBounds(45,0,400,40);
p1.setBackground(Color.gray);
p2.setBounds(45,40,400,400);
p2.setLayout(null);
p3.setBounds(45,440,400,60);
p3.setBackground(Color.gray);
p4.setBounds(445,40,70,400);
add(p1);
add(p2);
add(p3);
add(p4);
}
}
class ComponentDesign extends PanelDesign implements ActionListener
{
JLabel lx,ly,lb,lh;
JTextField fx,fy;
JTextArea AA;
ImageIcon icon1,icon2,icon3,icon4;
JButton 演示,游戏开始,重置,后退,提示;
JButton []bt=new JButton[64];
public ComponentDesign(){
icon1=new ImageIcon("1.GIF");
icon2=new ImageIcon("2.GIF");
icon3=new ImageIcon("3.GIF");
icon4=new ImageIcon("4.GIF");
lx=new JLabel("X初始坐标");
ly=new JLabel("Y初始坐标");
lb=new JLabel("BACK~");
lh=new JLabel("HELP!");
fx=new JTextField("0",5);
fy=new JTextField("0",5);
AA=new JTextArea("温馨提示:",4,8);
AA.setBackground(Color.gray);
AA.setForeground(Color.orange);
p4.add(AA);
lb.setBounds(0,175,45,20);
add(lb);
后退=new JButton(icon4);
后退.setFont(new Font("楷体",Font.BOLD,8));
后退.setBounds(0,200,45,45);
后退.addActionListener(this);
add(后退);
lh.setBounds(0,245,45,20);
add(lh);
提示=new JButton(icon3);
提示.setBounds(0,265,45,45);
提示.setFont(new Font("楷体",Font.BOLD,8));
提示.addActionListener(this);
add(提示);
演示=new JButton("演示");
演示.addActionListener(this);
游戏开始=new JButton("游戏开始");
游戏开始.addActionListener(this);
重置=new JButton("重置");
重置.addActionListener(this);
for(int i=0;i<64;i++)
{
bt[i]=new JButton();
bt[i].setFont(new Font("楷体",Font.BOLD,12));
bt[i].setForeground(Color.orange);
bt[i].setMargin(new Insets(0,0,0,0));
bt[i].addActionListener(this);
}
/////////////////////////////////////////////////////////设置P1
p1.setLayout(new FlowLayout(FlowLayout.CENTER,1,0));
p1.add(lx);
p1.add(fx);
p1.add(ly);
p1.add(fy);
/////////////////////////////////////////////////////////////设置P3
p3.setLayout(new FlowLayout(FlowLayout.CENTER,1,0));
p3.add(演示);
p3.add(游戏开始);
p3.add(重置);
///////////////////////////////////////////////////////////////设置P4
for (int row = 0; row < 8; row++){
for (int column = 0;column < 8; column++) {
if((column+row)%2==0)
bt[row*8+column].setBackground(Color.darkGray);//设置棋盘格子的颜色
else
bt[row*8+column].setBackground(Color.white); //设置棋盘格子的颜色
bt[row*8+column].setBounds((column+1)*40,(row+1)*40,40,40);
p2.add(bt[row*8+column]);
}
}
}
////////////////////////////////////////////////////////////处理事件定义
int cur,back,record=0;
int judge[]=new int[64];
public void actionPerformed(ActionEvent e){
////////////////////////////////////////////////////////////////////////////重置
if(e.getSource()==重置)
{
游戏开始.setEnabled(true);
游戏开始.setText("游戏开始");
演示.setEnabled(true);
for(int j=0;j<64;j++)
{bt[j].setIcon(null); bt[j].setText(null); judge[j]=0;}
fx.setText("0");
fy.setText("0");
AA.setText("温馨提示");
cur=0;
back=0;
record=0;
}
/////////////////////////////////////////////////////////////////////////////游戏开始
if(e.getSource()==游戏开始)
{
if(Integer.parseInt(fy.getText())<0||Integer.parseInt(fy.getText())>=8||Integer.parseInt(fx.getText())<0||Integer.parseInt(fx.getText())>=8)//如果坐标输入错误,则弹出提示
JOptionPane.showMessageDialog(null,"笨蛋,坐标写错啦~~~","你个笨笨",JOptionPane.INFORMATION_MESSAGE);
else//开始游戏,设置游戏开始和演示按钮不可编辑,并获取坐标将第一步记录
{
游戏开始.setText("游戏中");
游戏开始.setEnabled(false);
演示.setEnabled(false);
bt[Integer.parseInt(fy.getText())*8+Integer.parseInt(fx.getText())].setIcon(icon1);
cur=Integer.parseInt(fy.getText())*8+Integer.parseInt(fx.getText());
back=cur;
judge[cur]=1;
record++;
}
}
///////////////////////////////////////////////////////////////////////////////////演示
if(e.getSource()==演示)
{
AA.append("\n\n这是示范哈!!!");
游戏开始.setEnabled(false);
Demo demo=new Demo(Integer.parseInt(fx.getText()),Integer.parseInt(fy.getText()));//用当前坐标实例化Demo函数,通过贪婪算法计算路径
for(int j=0;j<64;j++)//按获得的Demo路劲修改按钮文本,显示演示效果
bt[demo.end[j]].setText("S"+j);
}
////////////////////////////////////////////////////////////////////////////////////////棋子
for(int k=0,a,b;k<64;k++)
{
a=cur%8;b=cur/8;//获取前一个点的坐标
if((e.getSource()==bt[k])&&judge[k]!=1&&(游戏开始.getText()=="游戏中")
&&(((b+1)*8+a-2==k)||((b+2)*8+a-1==k)||((b+2)*8+a+1==k)||((b+1)*8+a+2==k)
||((b-1)*8+a+2==k)||((b-2)*8+a+1==k)||((b-2)*8+a-1==k)||((b-1)*8+a-2==k)))///判断当前点是前一个点的后八个位置之一,并且没有被标记选过的点
{
back=cur;
cur=k;
bt[k].setIcon(icon1);
bt[back].setIcon(icon2);
judge[k]=1;
record++;
if(record==64)//如果棋盘已经走满就弹出提醒
{
JOptionPane.showMessageDialog(null,"嘿嘿,崽儿多厉害的嘛~~","成功罗",JOptionPane.INFORMATION_MESSAGE);
AA.append("\n\n恭喜~~~~~");
}
else//判断还有没有路可走,没有则弹出提醒
{
a=cur%8;b=cur/8;
if(((b-1)>7||(b-1)<0||(a+2)<0||(a+2)>7||judge[(b-1)*8+a+2]==1)
&& ((b-2)>7||(b-2)<0||(a+1)<0||(a+1)>7||judge[(b-2)*8+a+1]==1)
&& ((b-2)>7||(b-2)<0||(a-1)<0||(a-1)>7||judge[(b-2)*8+a-1]==1)
&& ((b-1)>7||(b-1)<0||(a-2)<0||(a-2)>7||judge[(b-1)*8+a-2]==1)
&& ((b+1)>7||(b+1)<0||(a-2)<0||(a-2)>7||judge[(b+1)*8+a-2]==1)
&& ((b+2)>7||(b+2)<0||(a-1)<0||(a-1)>7||judge[(b+2)*8+a-1]==1)
&& ((b+2)>7||(b+2)<0||(a+1)<0||(a+1)>7||judge[(b+2)*8+a+1]==1)
&& ((b+1)>7||(b+1)<0||(a+2)<0||(a+2)>7||judge[(b+1)*8+a+2]==1))
{
AA.append("\n\n失败罗,\n下次努力点噻~");
JOptionPane.showMessageDialog(null,"你完成了"+record+"步,\n加油哦~","失败了啊",JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////后退
if(e.getSource()==后退)
{
if((游戏开始.getText()!="游戏中"))
JOptionPane.showMessageDialog(null,"call我就给你一次悔过的机会~\n每步有且仅有一次哈!!