扫雷(Swing)

/*
2011-9-20
author:BearFly1990
*/
package swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Mine extends MouseAdapter {
    private JFrame mainFrame;
    private int[][] data;
    private JButton[][] buttons;
    private JButton startJB;
    private JLabel l;
    private int row;
    private int col;
    private int mineNumber;
    private int mineCount;
    private boolean isOver;
    public Mine(){
        row = 15;
        col = 15;
        mainFrame = new JFrame("扫雷咯");
        data = new int[row][col];
        buttons = new JButton[row][col];
        startJB = new JButton("start");
        l = new JLabel("welecome to mine!");
        mineNumber = row * col / 7 ;
    }
    public void init(){
        JPanel north = new JPanel();
        JPanel center = new JPanel();
        JPanel south = new JPanel();
        north.setLayout(new FlowLayout());
        center.setLayout(new FlowLayout());
        south.setLayout(new GridLayout(row, col, 4,4));
        mainFrame.setLayout(new BorderLayout());
        mainFrame.add(north,BorderLayout.NORTH);
        mainFrame.add(center,BorderLayout.CENTER);
        mainFrame.add(south,BorderLayout.SOUTH);
        north.add(l);
        startJB.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i = 0; i < row; i++){
                    for(int j = 0; j < col; j++){
                        buttons[i][j].setText(" ");
                        buttons[i][j].setBackground(Color.WHITE);
                        data[i][j] = 0;
                        isOver = false;
                    }
                }
                hashMine();
                mineCount = 0;
                l.setText("let's go!");
            }
            
        });
        center.add(startJB);
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                buttons[i][j] = new JButton(" ");
                buttons[i][j].setName((i + ":" + j));
                buttons[i][j].setBackground(Color.WHITE);
                buttons[i][j].addMouseListener(this);
                south.add(buttons[i][j]);
            }
        }
        hashMine();
    }
    public void start(){
        mainFrame.setSize(800,600);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    }
    private void hashMine() {
        for(int i = 0; i < mineNumber; i++){
            data[(int)(Math.random()*row)][(int)(Math.random() * col)] = -1;
        }
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(data[i][j] == -1)continue;
                int sum = 0; 
                for(int m = -1; m <= 1; m++){
                    for(int n = -1; n <= 1; n++){
                        if( i + m >=0 && j + n >= 0 && i + m < row && j + n < col){
                            if(data[i + m][j + n] == -1){
                                sum ++;
                            }
                        }
                    }
                }
                data[i][j] = sum;
            }
        }
    }
    private void gameOver(boolean over){
        if(over == true){
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                    if(data[i][j] == -1){
                        buttons[i][j].setText("M");
                        buttons[i][j].setBackground(Color.RED);
                    }
                }
            }
            l.setText("-_-");
            isOver = true;
            return;
        }
        int sumPress = 0;
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(!buttons[i][j].getText().equals(" "))
                    sumPress++;
            }
        }
        if(sumPress == row * col){
            int sum = 0;
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j ++){
                    if(data[i][j] == -1 && buttons[i][j].getText().equals("M")){
                        sum++;
                    }
                }
            }
            if(sum >= mineNumber){
                System.out.println(mineNumber);
                l.setText("^_^");
            }
        }
    }
    public void mousePressed(MouseEvent e){
        try{
            if(isOver)return;
            if(e.getButton() == MouseEvent.BUTTON3){
                JButton jb = (JButton)e.getSource();
                if(jb.getText().equals("M")){
                    jb.setText(" ");
                    mineCount--;
                    jb.setBackground(Color.WHITE);
                }else{
                    if(mineCount < mineNumber){
                        jb.setText("M");
                        jb.setBackground(Color.BLUE);
                        mineCount++;
                    }else{
                        l.setText("the mine flag is over!");
                    }
                }
            }else{
                JButton jb = (JButton)e.getSource();
                mousePress(jb);
            }
        }catch(Exception ee){
            ee.printStackTrace();
        }
    }
    private void mousePress(JButton jb) {
        String str[] = jb.getName().split(":");
        int i = Integer.parseInt(str[0]);
        int j = Integer.parseInt(str[1]);
        if(data[i][j] == -1){
            gameOver(true);
            return;
        }else{
            jb.setText((data[i][j] + ""));
            jb.setBackground(Color.YELLOW);
            if(data[i][j] == 0){
                for(int m = -1; m <= 1; m++){
                    for(int n = -1; n <= 1; n++){
                        if(i + m >=0 && j + n >= 0 && i + m < row && j + n < col){
                            if(buttons[i+ m][j+n].getText().equals(" ")){
                                mousePress(buttons[i+m][j+n]);
                            }
                        }
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        Mine mine = new Mine();
        mine.init();
        mine.start();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值