简单扫雷

package p;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class lc implements ActionListener{
	JFrame jf=new JFrame("Search 4 Mines");
	
	JPanel p=new JPanel();
	JPanel p1=new JPanel();
	JPanel p2=new JPanel();
	JPanel p3=new JPanel();
	
	JLabel l1=new JLabel("已点击方格数: ");
	JLabel l2=new JLabel("未点击方格数: ");
	JLabel l3=new JLabel();
	
	int a[][]=new int [11][11];
	int a1[]=new int[10];
	
	JButton b[]=new JButton[81];
	
	JTextArea tf1=new JTextArea(1,15);
	JTextArea tf2=new JTextArea(1,15);
	
	JMenuBar mb=new JMenuBar();
	
	JMenu m=new JMenu("Help");
	JMenu m1=new JMenu("Options");
	
	JMenuItem mi1=new JMenuItem("Introduction");
	JMenuItem mi=new JMenuItem("Quit");
	JMenuItem mi2=new JMenuItem("Restart");
	JMenuItem mi3=new JMenuItem("How 2 play?");
	
	static int i,j,flag=0;
	int count1=0;
	
	public static void main(String[] args) {
		lc that=new lc();
		that.setMine();
		that.go();	
	}
	
	public void setMine(){//设置地雷
		int j,k=0,n,m;
		for(j=0;j<11;j++)//让方格扩大为11*11,会让确定周围有多少地雷时变得简单
			for(k=0;k<11;k++){
				a[j][k]=0;
			}
		
		for(j=0;j<10;j++){
			n=(int)(Math.random()*8+1);//在1~9之间随机设置地雷,由于随机数可能重复,所以每次开启游戏地雷数不一定是一样的.
			m=(int)(Math.random()*8+1);
			a[n][m]=-1;
		}
		for(n=1;n<10;n++)//判断点击的方格周围有多少地雷.
			for(m=1;m<10;m++){
				if(a[n][m]!=-1){
					int count = 0;
		            a[n][m] = findMine(n,m,count);
				}
			}
	}
	public int howmanyMines(){//因为随机数可能会重复, 所以用这个方法来求出一共有多少地雷
		int count=0;
		for(i=1;i<10;i++)
			for(j=1;j<10;j++){
				if(a[i][j]==-1)
					count++;
			}
		return count;
	}
	public int findMine(int n,int m,int count){//查找点击的方格周围地雷数
		if (a[n-1][m-1]==-1)
			count++;
		if (a[n-1][m]==-1)
			count++;
		if (a[n-1][m+1]==-1)
			count++;
		if (a[n][m-1]==-1)
			count++;
		if (a[n][m+1]==-1)
			count++;
		if (a[n+1][m-1]==-1)
			count++;
		if (a[n+1][m]==-1)
			count++;
		if (a[n+1][m+1]==-1)
			count++;
		return count;
	}
	public void showMines(){//显示地雷位置
		for(i=1;i<10;i++)
			for(j=1;j<10;j++){
				if(a[i][j]==-1){
					b[(i-1)*9+j-1].setText("★");
				}
			}
	}
	
	public void initializeButton(){//对button[]初始化
		for(i=0;i<81;i++){
			b[i]=new JButton();
			b[i].setName(String.valueOf(i+1));
			if(i%2==0)
				b[i].setBackground(Color.yellow);
			else
				b[i].setBackground(Color.green);
			p.add(b[i]);
			b[i].addActionListener(al1());
			b[i].addMouseListener(ml1());
		}
	}

	public void setbuttonDisable(){//点击过后的button不能再点击
		for(i=0;i<81;i++){
			b[i].setEnabled(false);
		}
	}
	
	public void go(){
		p.setLayout(new GridLayout(9,9));
		p1.setLayout(new BorderLayout());
		p2.setLayout(new FlowLayout());
		p3.setLayout(new GridLayout(1,0));
		
		mi1.addActionListener(al2());
		mi.addActionListener(al2());
		mi2.addActionListener(al2());
		mi3.addActionListener(al2());
		
		m.add(mi1);
		m.add(mi3);
		
		
		m1.add(mi2);
		m1.add(mi);
		
		mb.add(m1);
		mb.add(m);
		
		jf.setJMenuBar(mb);
		
		p2.add(l1);
		p2.add(tf1);
		p2.add(l2);
		p2.add(tf2);
		
		l3.setText(" 地雷数:"+howmanyMines());
		
		p3.add(l3);


		initializeButton();
		
		p.setPreferredSize(new Dimension(500,500));
		
		p1.add(p,"South");
		p1.add(p3,"Center");
		p1.add(p2,"North");
		
		jf.getContentPane().add(p1);
		
		jf.pack();
		jf.setVisible(true);
	}
	

	
	public void actionPerformed(ActionEvent e){
		al1();
		al2();
		ml1();
	}
	
	
	public ActionListener al1(){//对button的监听
			ActionListener a1=new ActionListener(){
				public void actionPerformed(ActionEvent e){
					JButton tb=(JButton)e.getSource();
				
					int bn=Integer.parseInt(tb.getName());//一维下标和二维坐标的转换.
					
					int m=(bn-1)%9;
					int n=(bn-m)/9;
					if(a[n+1][m+1]==-1){
						JOptionPane.showMessageDialog(jf, "U Lose! Please Restat The Game!");
						showMines();
						setbuttonDisable();
					}
					
					else{
						tb.setBackground(Color.pink);
						tb.setFont(new Font("",Font.BOLD,30));
						tb.setText(String.valueOf(a[n+1][m+1]));
						tb.setEnabled(false);
						count1++;
						tf1.setText(""+count1);
						tf2.setText(""+(81-count1));
					}
					
					if(count1==81-howmanyMines()){
						JOptionPane.showMessageDialog(jf, "U WIN! ^0^ Congratulations! ^0^");
						showMines();
					}
				}
			};
			return a1;
	
	}

	public ActionListener al2() {
		ActionListener a2=new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JMenuItem mit=(JMenuItem)e.getSource();
				if(mit==mi1){
					JOptionPane.showMessageDialog(jf, "Author :廖超 桂林理工大学 网络12-2");
				}
				else if(mit==mi2){
					lc that=new lc();
					that.setMine();
					that.go();
					
				}
				else if(mit==mi){
					System.exit(0);
				}
				else if(mit==mi3){
					JOptionPane.showMessageDialog(jf, "左击点击,右击标记,再次点击右键取消标记");
				}
			}
		};
		return a2;
		
	}
	
	public MouseListener ml1() {//对右键点击事件进行监听,功能是标记地雷.
		MouseListener m1=new MouseListener(){
		
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				JButton tb=(JButton)arg0.getSource();
				int bn=Integer.parseInt(tb.getName());
				
				if(arg0.getButton()==MouseEvent.BUTTON3&&flag==0){
					b[bn-1].setText("★");
					flag=1;
				}
				else if(arg0.getButton()==MouseEvent.BUTTON3&&flag==1){
					b[bn-1].setText("");
					flag=0;
				}
				
	
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO 自动生成的方法存根
				
			}
			
		};
		return m1;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值