2017.12.16 扫雷小游戏未完成

package saolei;
/*
 * 设计一个二维数组,用于存放雷和周围八个格子雷的个数,
 * 再定义两个一维数组分别存放雷的X和Y坐标,
 * 布雷,记录周围八个格子雷的个数。
 */

import java.util.Random;

public class Block {
    protected int[][] Array;//用于存放雷和周围雷的个数
    protected int[] ArrayTestX;//用于存放雷的X坐标
    protected int[] ArrayTestY;//用于存放雷的Y坐标
    protected static int m;//用于记录已产生雷的个数,并对即将要产生的雷做限制条件
    protected static int s;//用于记录要产生雷的个数
    private static int t = 0;//记录周围雷的个数
    public Block(int x,int y){
        m = 0;
        s = 0;//此处必须置零
        Array = new int[x][y];
        switch(Array.length)
        {
        case 9:
            ArrayTestX = new int[10];
            ArrayTestY = new int[10];
            s = 10;
            break;
        case 16:
            ArrayTestX = new int[40];
            ArrayTestY = new int[40];
            s = 40;
            break;
        case 30:
            ArrayTestX = new int[99];
            ArrayTestY = new int[99];
            s = 99;
            break;
        }//选则游戏不同难度所对应的不同数组大小
        for(int i = 0;i < Array.length;i++){
            for(int j = 0;j < Array.length;j++){
                Array[i][j] = 0;
            }
        }//产生平面数组
        Random a = new Random();
        for(;m < s;m++){
            Array[(ArrayTestX[m] = a.nextInt(Array.length))][(ArrayTestY[m] = a.nextInt(Array.length))] = -1;//随机产生雷点,并赋值-1
            if((Judge() && m > 0)){
                m--;
            }//判断雷点是否重复
        }//产生雷点
        for (int i = 0; i < Array.length; i++) {
            for (int j = 0; j < Array.length; j++) {
                t = 0;
                if (Array[i][j] == -1) {
                } else {
                    if ((i - 1 >= 0) && (j - 1 >= 0)) {
                        if (Array[i - 1][j - 1] == -1) {
                            t++;
                        }
                    }
                    if (i - 1 >= 0) {
                        if (Array[i - 1][j] == -1) {
                            t++;
                        }
                    }
                    if ((i - 1 >= 0) && (j + 1 < Array.length)) {
                        if (Array[i - 1][j + 1] == -1) {
                            t++;
                        }
                    }
                    if (j - 1 >= 0) {
                        if (Array[i][j - 1] == -1) {
                            t++;
                        }
                    }
                    if (j + 1 < Array.length) {
                        if (Array[i][j + 1] == -1) {
                            t++;
                        }
                    }
                    if ((i + 1 < Array.length) && (j - 1 >= 0)) {
                        if (Array[i + 1][j - 1] == -1) {
                            t++;
                        }
                    }
                    if (i + 1 < Array.length) {
                        if (Array[i + 1][j] == -1) {
                            t++;
                        }
                    }
                    if ((i + 1 < Array.length) && (j + 1 < Array.length)) {
                        if (Array[i + 1][j + 1] == -1) {
                            t++;
                        }
                    }
                    Array[i][j] = t;
                }
            }
        }//遍历周围八个格子,记录雷的个数并赋值给当前位置
    }
    private boolean Judge(){
        for(int i = 0;i < m;i++){
            for(int j = i;j < m;j++){
                if((ArrayTestX[j] == ArrayTestX[j+1]) && (ArrayTestY[j] == ArrayTestY[j+1])){
                    return true;
                }
            }
        }
        return false;
    }//此方法用于判断已产生的雷的雷点是否重复。
    public void fun(){
        for(int i = 0;i < Array.length;i++){
            for(int j = 0;j < Array.length;j++){
                System.out.print(Array[i][j]);
            }
        }
    }//此方法打印出平面数组
}

//根据不同难度设置不同顶层容器大小

package saolei;
/*
 * 添加顶层容器
 * 设置雷区
 * 设置选择难度按钮,及其触发事件。
 * 设置根据难度不同调整容器大小。
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import java.util.LinkedList;
import java.util.Queue;

import boom.Block;

import javax.swing.*;

public class SuperJpanel extends JFrame{
    private static int X = 9;
    private static int Y = 9;//用与对选择难度时做中间变量

    private JPanel JpSouth = new JPanel();
    private Block block = new Block(X,Y);
    private CenterJpanel JpCenter;//初始化一个CenterJpanel对象,为雷区
    private JPanel JpMain ;//在顶层容器中添加一个JPanel
    private JButton JbRestart;//新建一个重新开始按钮
    private JRadioButton[] jrb = {
            new JRadioButton("初级",true),new JRadioButton("中级"),new JRadioButton("高级")
    };//新建选择按钮
    private ButtonGroup bg = new ButtonGroup();
    public SuperJpanel(){
        String UI = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
        try {
            UIManager.setLookAndFeel(UI);
        } catch(Exception e){
            e.printStackTrace();
        }//设置整个面板显示格式
        JbRestart = new JButton("重新开始") ;
        JpCenter = new CenterJpanel(block.Array, block.ArrayTestX, block.ArrayTestY);
        JpMain = new JPanel();
        JpMain.setLayout(new BorderLayout());//设置布局方式为BorderLayout布局
        JbRestart.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根
                if (e.getSource() == JbRestart) {
                    JpCenter.clear(block.Array);
                    JpMain.remove(JpCenter);//先移除Center区域,
                    block = new Block(block.Array.length,block.Array.length);//新生成一个不同的数组作为雷区
                    JpCenter = new CenterJpanel(block.Array, block.ArrayTestX, block.ArrayTestY);//将雷区添加到Center中
                    JpCenter.first(block.Array);//让雷区显示第一张卡片
                    JpMain.add(JpCenter,BorderLayout.CENTER);//将Center添加到定层容器中
                    JpMain.revalidate();//刷新整个面板
                } 

            }
        });//重新开始按钮触发事件处理方式
        for(int i = 0;i < 3;i ++){
            JpSouth.add(jrb[i]);
            bg.add(jrb[i]);
            jrb[i].addItemListener(new ItemListener() {

                @Override
                public void itemStateChanged(ItemEvent e) {
                    // TODO 自动生成的方法存根
                    if(e.getSource() == jrb[0]){//初级按钮
                        X = 9;
                        Y = 9;//设置对应雷区的X,Y轴的长度
                        JpCenter.clear(block.Array);
                        JpMain.remove(JpCenter);//任然移除Center
                        block = new Block(X,Y);//新建一个初级的雷区
                        JpCenter = new CenterJpanel(block.Array, block.ArrayTestX, block.ArrayTestY);
                        JpCenter.first(block.Array);//翻到第一块卡片
                        choose(X);//调用选择方法来设置整个面板的大小
                        JpMain.add(JpCenter,BorderLayout.CENTER);
                        JpMain.revalidate();
                    }
                    if(e.getSource() == jrb[1]){
                        X = 16;
                        Y = 16;
                        JpCenter.clear(block.Array);
                        JpMain.remove(JpCenter);
                        block = new Block(X,Y);
                        JpCenter = new CenterJpanel(block.Array, block.ArrayTestX, block.ArrayTestY);
                        JpCenter.first(block.Array);
                        choose(X);
                        JpMain.add(JpCenter,BorderLayout.CENTER);
                        JpMain.revalidate();
                    }
                    if(e.getSource() == jrb[2]){
                        X = 30;
                        Y = 30;
                        JpCenter.clear(block.Array);
                        JpMain.remove(JpCenter);
                        block = new Block(X,Y);
                        JpCenter = new CenterJpanel(block.Array, block.ArrayTestX, block.ArrayTestY);
                        JpCenter.first(block.Array);
                        choose(X);
                        JpMain.add(JpCenter,BorderLayout.CENTER);
                        JpMain.revalidate();
                    }
                }
            });//难度选择按钮触发事件
        }
        JpMain.add(JpCenter,BorderLayout.CENTER);
        JpMain.add(JbRestart,BorderLayout.NORTH);
        JpMain.add(JpSouth,BorderLayout.SOUTH);//将三个区域添加进主面板中
        this.add(JpMain);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭结束
        this.setTitle("扫雷");
        this.setBounds(100, 100, 200, 300);//设置初始时顶层容器大小
        this.setResizable(false);//设置用户不可自己调整大小
        this.setVisible(true);
    }
    public void choose(int x){
        switch(x){
        case 9:
            this.setBounds(100, 100, 200, 300);
            break;
        case 16:
            this.setBounds(100, 100, 300, 380);
            break;
        case 30:
            this.setBounds(100, 100, 500, 580);
            break;
        }
    }
}

未完成 待续

原文地址:https://www.cnblogs.com/qichunlin/p/8047958.html