- 论坛徽章:
- 0
|
八皇后问题,请帮帮忙!!
下面的源代码是我的老师给我的,不过编译不能通过,有警告,请高手帮帮忙,改一改,有劳了!!!
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class main extends Applet {
private final static int num=5;
int m=0,n=1;
Button button1,button2;
queen lastqueen=null;
int[][] now=new int[2][8];
int[] seg=new int[8];
int[] before=new int[8];
public void init(){
for (int i=0;i<=7;i++) {now[0]=40+i*40;now[1]=360;}
setLayout(null);
Font f=new Font("",Font.BOLD,30);
Label label1=new Label("八皇后问题" ;
label1.setFont(f);
label1.reshape(410,50,220,50);
add(label1);
f=new Font("",Font.BOLD,1 ;
Label label2=new Label("制作人:" ;
label2.setFont(f);
label2.reshape(400,110,100,50);
add(label2);
Label label3=new Label("徐激 9097302" ;
label3.setFont(f);
label3.reshape(440,150,140,40);
add(label3);
button1=new Button("下一种解" ;
button1.reshape(450,220,80,50);
add(button1);
button1.disable();
button2=new Button("棋子复位" ;
button2.reshape(450,300,80,50);
add(button2);
}
private void drawframe(Graphics g) {
g.setColor(Color.white);
for(int i=0;i<=3;i++) {
for(int j=0;j<=3;j++) {
g.fillRect(70+i*80,30+j*80,40,40);
g.fillRect(30+i*80,70+j*80,40,40);
}
}
g.setColor(Color.red);
g.drawRect(30,30,320,320);
if(now[1][7]!=360){g.drawRect(now[0][7]-8,now[1][7]-8,36,36);
g.drawRect(now[0][7]-9,now[1][7]-9,38,3 ;}
}
public void paint(Graphics g) {
g.drawRect(29,29,322,323);
g.fillRect(25,378,330, ;
drawframe(g);
for (int i=1;i<=7;i++) {
g.drawLine(30,30+i*40,350,30+i*40);
g.drawLine(30+i*40,30,30+i*40,350);
}
for (int i=0;i<=7;i++) {seg=(now[1]-before)/num;}
for (int t=1;t<num;t++) {
g.clearRect(30,30,320,350);
drawframe(g);
for (int i=0;i<=7;i++){
g.drawLine(30,30+i*40,350,30+i*40);
g.drawLine(30+i*40,30,30+i*40,350);
before+=seg;
g.fillOval(now[0],before,20,20);
}
try{ Thread.sleep(200);}
catch(InterruptedException exp) {}
}
g.clearRect(30,30,320,350);
drawframe(g);
for (int i=0;i<=7;i++) {
g.drawLine(30,30+i*40,350,30+i*40);
g.drawLine(30+i*40,30,30+i*40,350);
g.fillOval(now[0],now[1],20,20);
}
}
public boolean action(Event e,Object o) {
int i;
if(e.target==button1){
if(!lastqueen.advance())
{button1.disable();showStatus("已完成,请用鼠标点击,重新开始." ;
return false;}
}
queen tmpq=lastqueen;
for(i=0;i<=7;i++){
before=now[1];
now[0]=40+(tmpq.getCol())*40;
now[1]=40+(tmpq.getRow()-1)*40;
tmpq=tmpq.getNgh();
}
if(e.target==button2){
showStatus("请用鼠标点击,重新开始." ;
for (i=0;i<=7;i++) {
before=now[1];now[0]=40+i*40;now[1]=360;
}
button1.disable();
}
repaint();
return true;
}
public boolean mouseDown(Event e,int x,int y) {
if(y<=350 && y>;=30 && x<=350 && x>;=30) {
m=(x-30)/40;
n=(y-30)/40+1;
int temp=m+1;
showStatus("Mouse at: "+temp+" "+n);
lastqueen=null;
int tmpcol=0;
for(int i=m;i<=m+7;i++) {
tmpcol=i % 8 ;
if (i==m) lastqueen=new queen(tmpcol,n,lastqueen);
else lastqueen=new queen(tmpcol,1,lastqueen);
if(!lastqueen.findSolution())
{button1.disable();showStatus("已完成" ;return false;}
}
queen tmpq=lastqueen;
button1.enable();
for(int i=0;i<=7;i++){
before=now[1];
now[0]=40+(tmpq.getCol())*40;
now[1]=40+(tmpq.getRow()-1)*40;
tmpq=tmpq.getNgh();
}
repaint();
return true;
}
return false;
}
}
class queen {
private int row;
private int column;
private queen neighbour;
private boolean canAttack(int testRow,int testColumn) {
if (row==testRow) return true;
int columnDifference=Math.abs(testColumn-column);
if(Math.abs(testRow-row)==columnDifference)
return true;
else
if (neighbour!=null) {
return neighbour.canAttack(testRow,testColumn);
}
return false;
}
queen(int col,int rw,queen ngh) {
column=col;
neighbour=ngh;
row=rw;
}
public boolean findSolution() {
if (neighbour!=null){
while(neighbour.canAttack(row,column)) {
if (!advance()) return false;
}
}
return true;
}
public boolean advance() {
if (neighbour==null) return false;
if(row< {
row++;
return findSolution();
}
else {
if (neighbour.advance()) {
row=1;
return findSolution();}
}
return false;
}
public int getCol(){return column;}
public int getRow(){return row;}
public queen getNgh(){return neighbour;}
public void print(){
if(neighbour!=null) {
neighbour.print();}
System.out.print("("+column+" "+row+" " ;
}
/* public static void main(String args[])throws IOException {
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
String line1=dis.readLine();
int m=Integer.parseInt(line1);
line1=dis.readLine();
int n=Integer.parseInt(line1);
queen lastqueen=null;
int tmpcol=0;
for(int i=m;i<=m+7;i++) {
tmpcol=i % 8 ;
if (i==m) lastqueen=new queen(tmpcol,n,lastqueen);
else lastqueen=new queen(tmpcol,1,lastqueen);
if(!lastqueen.findSolution())
{System.out.println("no solution");}
}
lastqueen.print();
}*/
} |
|