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;
        }
    }
}

未完成 待续

时间: 2024-08-15 04:25:02

2017.12.16 扫雷小游戏未完成的相关文章

一款JavaScript开发的扫雷小游戏

<style><!-- #FLM_main { margin:50px auto; padding:20px; background:#EEE; zoom:1; width:650px; } #FLM_main table { border-collapse:collapse; background:#EEE; float:left; } #FLM_main table td { border:1px solid #CCC; font-size:20px; width:38px; hei

练手WPF(三)——扫雷小游戏的简易实现(中)

原文:练手WPF(三)--扫雷小游戏的简易实现(中) 八.随机布雷 /// <summary> /// 随机布地雷 /// </summary> /// <param name="mineNum">地雷数</param> private void SetRndMine(int mineNum) { for (int k = 0; k < mineNum; k++) { int nullnum = 0; for (int j = 0;

结对博客(扫雷小游戏)

结对题目:基于一款课下娱乐的小游戏—扫雷 一.需求分析: 1.背景:在现在社会,人们的工作和学习压力不断增大,空余时间时间日益减少,一些大型的娱乐游戏又耗费大量的娱乐时间, 又不一定对身体有益,因此不耗费大量时间的小游戏,方便有趣,是大势所趋. 2.目的:编译此系统是为了在工作和学习之余得到适当的放松,既不浪费大量时间,又可以锻炼自己的思维,缓解疲劳. 3.应用人群:本游戏是基于windows下的小游戏,对人基本没有害处,因此能接触computer的人都可以玩这款游戏. 4.设计概述: 本游戏界

扫雷小游戏

1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 6 #define ROW 9 7 #define COL 9 8 #define MINE_COUNT 10 9 10 int Menu() 11 { 12 printf("=======================\n"); 13 printf(&q

练手WPF(三)——扫雷小游戏的简易实现(上)

一.创建项目1.创建WPF项目,设置初始化窗口大小(初级难度):高x宽为430x350.2.添加文件夹Images,并添加相关图片. 3.xaml中引入图片资源. <Window.Resources> <BitmapImage x:Key="ImgSpace" UriSource="/Images/space.bmp"/> <BitmapImage x:Key="ImgMine" UriSource="/I

C语言写扫雷小游戏2

这是下午写的,这次的修改增加了鼠标的事件,需要的是windows的消息机制,WinAPI函数,以下是新添加的定义 struct { int num;//格子当前状态,1表示有雷,0表示无雷或已经显示数字 int roundnum;//统计格子周围的雷数 int flag;//右键按下显示红旗标志,0表示没有,1表示有 }Mine[10][10]; POINT Mouse;//定义鼠标事件 int MouseX, MouseY;//鼠标的x,y坐标 int mineNUM;//统计处理过的格子数

练手WPF(三)——扫雷小游戏的简易实现(下)

十四.响应鼠标点击事件    (1)设置对应坐标位置为相应的前景状态 /// <summary> /// 设置单元格图样 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="state"></param> private vo

C语言写扫雷小游戏1

用的ide是vs 2013,自己学习都是参考网上的代码,由于vs不带tc的graphics.h,所以下载easyx库,将include和lib复制到vc的目录. easyx的graphics.h和tc的还是有区别的,百科上的示例 使用上,基本和 Turbo C没太大区别.启动Visual C++,创建一个控制台项目(Win32 Console Application),然后引用 graphics.h 头文件就可以了.看一个画圆的例子: #include <graphics.h> // 就是需要

一款扫雷小游戏

扫雷 简单 一般 困难 地雷总数:   剩余方块: Coded by HerveyHall|查看源码 颇有不足,望大家指点