初学自制,如有错误请斧正。 package lesson; import java.util.Random; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class _1 extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; int a=0; Random r=new Random(); int c[]=new int[100];//存储雷 JPanel ps=new JPanel(); JPanel pe=new JPanel(); int w[]=new int[100];//判断格子是否消耗 JButton xi=new JButton("洗盘"); JButton de=new JButton("确定"); JButton re=new JButton ("restart"); JComboBox<String> mode=new JComboBox<String>(); JButton []b = new JButton[100];//存储格子 public _1(){ super("扫雷"); setLayout(new BorderLayout()); ps.setLayout(new GridLayout(10,10,0,0)); pe.setLayout(new GridLayout(1,8,10,10)); for(int i=0;i<10;i++){//添加格子 for(int j=9;j>=0;j--) { b[a]=new JButton(a+""); ps.add(b[a]); b[a].addActionListener(this); a++; } } de.addActionListener(this); re.addActionListener(this); xi.addActionListener(this); mine(c,10);//默认为"normal" mode.addItem("normal"); mode.addItem("easy"); mode.addItem("hard"); pe.add(de);pe.add(re);pe.add(mode);pe.add(xi); this.add("Center",ps); this.add("North",pe); this.setSize(800, 800); } public void mine(int c[],int d){ for(int i=0;i<100;i++){ w[i]=1; c[i]=-1; } for(int j=0;j<d;j++){ c[j]=r.nextInt(100); } } public void type(){//模式选择 if(mode.getSelectedItem().equals("hard"))mine(c,99); if(mode.getSelectedItem().equals("easy"))mine(c,1); if(mode.getSelectedItem().equals("normal"))mine(c,10); } public void xi(){//排雷 for (int i = 0; i < 100; i++) {//第一层遍历查询格子号码 for (int j = 0; c[j] != -1; j++) {//第二层遍历判断是否埋雷 if (i == c[j]) {//雷 b[i].setBackground(new Color(255, 50, 50)); b[i].setText(""); w[i] = 0; break; } } if(w[i]!=0) { b[i].setBackground(new Color(255, 255, 255)); b[i].setText(""+detect(i)); };//空 } } public int detect(int x){ int y=x-10,z=0; int d[]=new int[9]; for(int i=0;i<3;i++){//收纳周围格子 if(y%10==9) d[z++]=-1; else d[z++]=y+1; d[z++]=y; if (y%10==0) d[z++]=-1; else d[z++]=y-1; y+=10; } y=0; for(x=0;x<9;x++){//判断雷的数目 for(z=0;c[z]!=-1;z++){ if(c[z]==d[x]) { y++; break; } } } return y; } public void clear(int x){ int i=0,z=0; w[x]=0; if(detect(x)==0){ if(x%10==9){z=2;i=x-11;}//收纳周围格子 else if(x%10==0){z=2;i=x-10;} else {z=3;i=x-11;} for(int c=0;c<3;c++){//遍历周围格子清空 for(int g=0;g<z;g++){ if ((i+g<100)&(i+g>=0)) { b[i + g].setBackground(new Color(255, 255, 255)); b[i + g].setText("" + detect(i + g)); if(i+g!=x&&w[i+g]==1) clear(i+g);//防止莫比乌斯 } } i+=10; } } else { b[x].setBackground(new Color(255, 255, 255)); b[x].setText(""+detect(x)); } } public void restart(){ Color c=de.getBackground(); for(int i=0;i<100;i++){ b[i].setText(""+i); b[i].setBackground(c); } } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("洗盘"))xi(); if (e.getActionCommand().equals("确定")) type(); if (e.getActionCommand().equals("restart")) restart(); for (int i = 0; i < 100; i++) {//第一层遍历查询格子号码 if (e.getActionCommand().equals(i + "")) { for (int j = 0; c[j] != -1; j++) {//第二层遍历判断是否埋雷 if (i == c[j]) {//雷 xi(); /*b[i].setBackground(new Color(255, 50, 50)); b[i].setText(""); w[i] = 0;*/ } } if(w[i]!=0) clear(i);//空 break; } } } public static void main(String[] rags) { _1 x=new _1(); x.setVisible(true); } }
扫雷但是简单(java)
最新推荐文章于 2024-09-02 22:29:33 发布