package queen;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
class equeen extends Frame implements ActionListener {
// 构造界面和定义数组
Button enter;
Button clean;
Button exit;
int num[][]=new int[93][9];
int i, j, q,cont=0;
int b[] = new int[9];
Label a[][] = new Label[9][9];
equeen(String s) {
GridLayout grid;
grid = new GridLayout(9, 8);
setLayout(grid);
enter = new Button("begin");
clean = new Button("clean");
exit = new Button("esit");
for (int i = 1; i < 9; i++) {
for (int j = 1; j < 9; j++) {
a[i][j] = new Label();
if ((i + j) % 2 == 0)
a[i][j].setBackground(Color.yellow);
else
a[i][j].setBackground(Color.gray);
add(a[i][j]);
}
}
add(enter);
add(clean);
add(exit);
enter.addActionListener(this);
clean.addActionListener(this);
exit.addActionListener(this);
setBounds(100, 100, 300, 300);
// 大小
setVisible(true);
validate();
}
//判断函数
public boolean find(int i,int k){
int j;
j=1;
while(j<k){
if((b[j]==i)||(Math.abs(b[j]-i)==Math.abs(j-k)))
return false;
j++;
}
return true;
}
void place(int k, int n){
if(k>n){
printf(n);
//a[7][7].setText("K");//该88矩形框 是从 00开始的!!
}
else
for(int i=1;i<=n;i++)
if(find(i,k)){
b[k]=i;
place(k+1,n);
}
}
void printf(int n)
{
cont++;
int i;
System.out.println("第"+cont+"个解");
for(i=1;i<=n;i++){
num[cont][i]=b[i];
//q=b[i];
//a[q][i].setText("K");
//clean();
System.out.print("--"+b[i]);
}
System.out.println();
//for(i=1;i<=n;i++)
//System.out.println("dddddd"+num[1][i]);
}
void clean(){
for (int i = 1; i < 9; i++) {
for (int j = 1; j < 9; j++) {
a[i][j].setText("");
}
}
}
void write(){
int w=(int)(Math.random()*93);
for (int i = 1; i < 9; i++) {
int c= num[w][i];
a[c][i].setText("K");
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == enter) {
clean();
//a[7][7].setText("K");
write();
}
if (e.getSource() == clean) {
clean();
}
if (e.getSource() == exit) {
System.exit(0);
}
}
}
class queen {
public static void main(String[] args) {
equeen a = new equeen("bbb");
a.place(1, 8);
}
}