求解java扫雷递归的时候出现错误 看了2小时也不知道错在哪

baidu_26657439 2015-03-17 10:52:19
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.*;
import java.awt.geom.Dimension2D;
import java.util.*;
import javax.swing.*;
public class mine extends JFrame{
/**
*
*/
private static final long serialVersionUID = 111111;
public static int mine_num,bomb_num;
JButton[] an;
JButton kaishi=new JButton("游戏开始");
JButton an_dia;
int bomb[][]=new int[9][9];
int flag[]=new int[100];
JPanel mb1=new JPanel();
JPanel mb2=new JPanel();
JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("游戏");
JMenu menu2=new JMenu("帮助");
JMenuItem menuitem1=new JMenuItem("新游戏");
JMenuItem menuitem3=new JMenuItem("关于");
JMenuItem menuitem2=new JMenuItem("设置");

//JButton[][] an;

public static void main(String[]args){
mine mine1=new mine();
mine1.setVisible(true);

}
public mine(){
this.setTitle("扫雷");//设置窗口标题
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//所有程序必要的
this.setResizable(false);
menubar.add(menu1);
menubar.add(menu2);
menu1.add(menuitem1);
menu1.add(menuitem2);
menu1.add(menuitem3);
an_dia=new JButton("设置雷的数量");
an_dia.setBounds(10, 10, 50, 50);
mb2.setLayout(new GridLayout(9,9));
mb1.setLayout(new FlowLayout(0));
mb1.setBackground(Color.lightGray);
mb2.setBackground(Color.cyan);
an=new JButton[81];
for( int i=0;i<81;i++){
an[i]=new JButton("");
an[i].setFont(new Font(null,Font.BOLD,25));
an[i].setMargin(new Insets(0,0,0,0));
an[i].addActionListener(new danji());
/*an[i].setText("1");
an[i].setEnabled(false);
*/
mb2.add(an[i]);
}
mb1.add(an_dia);
mb1.add(kaishi);
this.setJMenuBar(menubar);
this.add(mb1,BorderLayout.NORTH);
this.add(mb2,BorderLayout.CENTER);
this.setSize(700,625);//设置大小 顺序很重要
kaishi.addActionListener(new reset());
menuitem1.addActionListener(new reset());
an_dia.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //经常出现的错误,把参数ActionEvent e 写成 ActionListener e

new dialog(mine.this); //注意这里的TestDialog.this
}
});
menuitem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //经常出现的错误,把参数ActionEvent e 写成 ActionListener e
new dialog(mine.this); //注意这里的TestDialog.this
}
});
mine_num=20;
reset a=new reset();
a.reset1();
}
//dialog用于获取雷得数量并将其传递给bomb_num。
class dialog extends JDialog{
/**
*
*/
private static final long serialVersionUID = 1L;

public dialog(mine frame){
super(frame,"设置有多少雷",true);
this.setBounds(200, 50, 300, 150);
JPanel m1,m2,m3;
final JTextField jt=new JTextField("20",20);
m1=new JPanel();
m2=new JPanel();
m3=new JPanel();
this.setLayout(new GridLayout(3,1));
JButton an=new JButton("确定");
m1.add(new JLabel("请输入雷的数量(大于5且小于50)"));
m3.add(an);
m2.add(jt);
this.add(m1);
this.add(m2);
this.add(m3);
an.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mine.mine_num=Integer.valueOf(jt.getText());//整形
Object[] options = { "OK", "CANCEL" };
JOptionPane.showMessageDialog(null, "设置完成", "设置完成", JOptionPane.INFORMATION_MESSAGE);
}
});
this.setVisible(true);//顺序
}
}
class danji implements ActionListener{
int j;
public void kongzhi(int i){
this.j=i;
if(flag[j]==0){
if(bomb[j/9][j%9]!=0){
an[j].setText(""+bomb[j/9][j%9]+"");
flag[j]=1;
an[j].setEnabled(false);
}
else{
if(j==0){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j+9);
}
else if(j==8){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-1);
this.kongzhi(j+9);
}
else if(j==72){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j-9);
}
else if(j==80){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-1);
this.kongzhi(j-9);
}
else if(j>=1&&j<=7){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j-1);
this.kongzhi(j+9);
}
else if(j>=73&&j<=81){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j+1);
this.kongzhi(j-1);
this.kongzhi(j-9);
}
else if(j%9==0){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-9);
this.kongzhi(j+9);
this.kongzhi(j+1);
}
else if(j%9==8){
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-9);
this.kongzhi(j+9);
this.kongzhi(j-1);
}
else{
an[j].setEnabled(false);
flag[j]=1;
this.kongzhi(j-9);
this.kongzhi(j-1);
this.kongzhi(j+1);
this.kongzhi(j+9);
}

}
}
}
public void actionPerformed(ActionEvent e){
for(int i=0;i<81;i++){
if(e.getSource()==an[i]){
if(bomb[i/9][i%9]==9){
JOptionPane.showMessageDialog(null, "你踩到雷了", "踩到雷了", JOptionPane.ERROR_MESSAGE);
}
else
this.kongzhi(i);
}
}
}
}
class reset implements ActionListener{
public void reset1(){
bomb_num=mine_num;
for(int i=0;i<81;i++){
flag[i]=0;
an[i].setEnabled(true);
an[i].setText("");
bomb[i/9][i%9]=0;
}
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
bomb[i][j]=0;
}
for(int i=0;i<mine_num;i++){
int x=(int) (Math.random()*9);
int y=(int) (Math.random()*9);
if(bomb[x][y]!=9)
bomb[x][y]=9;
else
i--;
}
if(bomb[0][0]!=9){
if(bomb[0][1]==9)
bomb[0][0]++;
if(bomb[1][1]==9)
bomb[0][0]++;
if(bomb[1][0]==9)
bomb[0][0]++;
}
if(bomb[0][8]!=9){
if(bomb[0][7]==9)
bomb[0][8]++;
if(bomb[1][8]==9)
bomb[0][8]++;
if(bomb[1][7]==9)
bomb[0][8]++;
}
if(bomb[8][0]!=9){
if(bomb[7][0]==9)
bomb[8][0]++;
if(bomb[8][1]==9)
bomb[8][0]++;
if(bomb[7][1]==9)
bomb[8][0]++;
}
if(bomb[8][8]!=9){
if(bomb[7][7]==9)
bomb[8][8]++;
if(bomb[7][8]==9)
bomb[8][8]++;
if(bomb[8][7]==9)
bomb[8][8]++;
}
for(int i=0;i<9;i++)
for(int j=0;j<9;j++){
if(i>=1&&j>=1&&i<=7&&j<=7){
if(bomb[i][j]!=9){
if(bomb[i-1][j-1]==9)
bomb[i][j]++;
if(bomb[i-1][j+1]==9)
bomb[i][j]++;
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i+1][j-1]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j+1]==9)
bomb[i][j]++;
if(bomb[i-1][j]==9)
bomb[i][j]++;
}
}
else if(i==0&&j!=0&&j!=8&&bomb[i][j]!=9){
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i+1][j-1]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j+1]==9)
bomb[i][j]++;
}
else if(i==8&&j!=0&&j!=8&&bomb[i][j]!=9){
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i-1][j-1]==9)
bomb[i][j]++;
if(bomb[i-1][j]==9)
bomb[i][j]++;
if(bomb[i-1][j+1]==9)
bomb[i][j]++;
}
else if(j==0&&i!=0&&i!=8&&bomb[i][j]!=9){
if(bomb[i-1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i-1][j+1]==9)
bomb[i][j]++;
if(bomb[i][j+1]==9)
bomb[i][j]++;
if(bomb[i+1][j+1]==9)
bomb[i][j]++;
}
else if(j==8&&i!=0&&i!=8&&bomb[i][j]!=9){
if(bomb[i-1][j]==9)
bomb[i][j]++;
if(bomb[i+1][j]==9)
bomb[i][j]++;
if(bomb[i-1][j-1]==9)
bomb[i][j]++;
if(bomb[i][j-1]==9)
bomb[i][j]++;
if(bomb[i+1][j-1]==9)
bomb[i][j]++;
}
}
}
public void actionPerformed(ActionEvent e){
this.reset1();
}
}
}



[color=#FF0000]错误如下Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
at mine$danji.kongzhi(mine.java:129)
at mine$danji.kongzhi(mine.java:193)
at mine$danji.kongzhi(mine.java:190)
at mine$danji.kongzhi(mine.java:190)
at mine$danji.kongzhi(mine.java:190)
at mine$danji.kongzhi(mine.java:190)

...全文
175 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
飏飏一蝶 2015-03-18
  • 打赏
  • 举报
回复
编译运行没问题……
三仙半 2015-03-18
  • 打赏
  • 举报
回复
这么多代码,也没有格式,看的好晕啊
baidu_26657439 2015-03-18
  • 打赏
  • 举报
回复
你把雷设置成0 点击就出现错误了 没有一次全部清除

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧