java课程设计——扫雷

1、团队课程设计博客连接:

 

http://www.javashuo.com/article/p-bsqdkoti-dv.htmlhtml

 

2、我的负责模块或任务说明:

模块:文件操做  Minefield类实现

3、本身的代码提交记录截图

4、本身负责模块或任务详细说明

1.Minefield类实现算法

Minefield是咱们主要的算法实现模块。在正式开始扫雷游戏时,看见的是以下界面:一格一格的蓝色格子表示可点击区域。数组

点击后,分三个状况:1)标记该区域为雷(右击)2)该区域是点击到雷,游戏结束 3)该区域无雷,显示区域九宫格范围内的雷的个数。(此处有9种状况)4)游戏提早结束,显示全部未被点击的雷。下图囊括四种状况:dom

选项区域:ide

这里涉及到的保存进度,下条中讲解。函数

对于以上游戏功能,咱们是这样实现的。学习

先根据所选等级,初始化一个map数组,数组的行数和列数取决于等级。而后用Math.random()数结合循环语句和判断语句生成,将类的区域置9。其他位置元素值属于0~8,分别表示以当前位置为中心的九宫格中雷的数目。至此,map数组生成完毕。因为游戏界面中,真正显示出来的状态有13种(0~8九个数字九种,标记该位置为雷、该位置是被点击的雷、游戏成功后未被点击的雷、当前可点击区域各一种),用map数组来控制游戏界面的显示会有冲突,因此此处引入hiddenmap数组,元素数值范围为0~12,分别表示上述13种状况。对于游戏界面,实际上是一个JButton数组,因为界面的美观性,用hiddenmap数组值为每一个按钮分配图片,根据hiddenmap数组值分配对应的功能图片。ui

 生成雷:this

 

生成map数组其余位置的数字:spa

for (int i = 0; i < getWidth(); i++)
            for (int j = 0; j < getLength(); j++) {
                if (map[i][j] != 9) // 只对不为雷的区域进行雷数判断
                {
                    int number = 0;
                    if (i == 0) {
                        if (j == 0) {
                            if (map[i][j + 1] == 9)
                                number++;
                            if (map[i + 1][j] == 9)
                                number++;
                            if (map[i + 1][j + 1] == 9)
                                number++;
                        } else if (j == getLength() - 1) {
                            if (map[i][j - 1] == 9)
                                number++;
                            if (map[i + 1][j] == 9)
                                number++;
                            if (map[i + 1][j - 1] == 9)
                                number++;
                        } else {
                            if (map[i][j - 1] == 9)
                                number++;
                            if (map[i][j + 1] == 9)
                                number++;
                            if (map[i + 1][j - 1] == 9)
                                number++;
                            if (map[i + 1][j] == 9)
                                number++;
                            if (map[i + 1][j + 1] == 9)
                                number++;
                        }

                    }
                    if (i == getWidth() - 1) {
                        if (j == 0) {
                            if (map[i][j + 1] == 9)
                                number++;
                            if (map[i - 1][j] == 9)
                                number++;
                            if (map[i - 1][j + 1] == 9)
                                number++;
                        } else if (j == getLength() - 1) {
                            if (map[i][j - 1] == 9)
                                number++;
                            if (map[i - 1][j] == 9)
                                number++;
                            if (map[i - 1][j - 1] == 9)
                                number++;
                        } else {
                            if (map[i][j - 1] == 9)
                                number++;
                            if (map[i][j + 1] == 9)
                                number++;
                            if (map[i - 1][j - 1] == 9)
                                number++;
                            if (map[i - 1][j] == 9)
                                number++;
                            if (map[i - 1][j + 1] == 9)
                                number++;
                        }
                    }
                    if (i != 0 && i != (getWidth() - 1)) {
                        if (j == 0) {
                            if (map[i - 1][j + 1] == 9)
                                number++;
                            if (map[i][j + 1] == 9)
                                number++;
                            if (map[i + 1][j + 1] == 9)
                                number++;
                            if (map[i - 1][j] == 9)
                                number++;
                            if (map[i + 1][j] == 9)
                                number++;
                        }

                        if (j == getLength() - 1) {
                            if (map[i - 1][j - 1] == 9)
                                number++;
                            if (map[i][j - 1] == 9)
                                number++;
                            if (map[i + 1][j - 1] == 9)
                                number++;
                            if (map[i - 1][j] == 9)
                                number++;
                            if (map[i + 1][j] == 9)
                                number++;
                        }

                    }

                    if ((i != 0) && (j != 0) && (i != getWidth() - 1) && (j != getLength() - 1)) { // 不在边缘的状况
                        // 单位九宫格内的雷数
                        for (int n = i - 1; n <= i + 1; n++)
                            for (int m = j - 1; m <= j + 1; m++)
                                if (map[n][m] == 9)
                                    number++;

                    }
                    map[i][j] = number;
                }

            }
    }

 

全部过程当中咱们的操做都是对咱们的hiddenmap作修改,咱们的map是咱们的真实雷区不作改动,接下来说讲核心的具体实现。
因此咱们的hiddenmap刚开始都是初始化为10,当进行第一次点击的时候,若是hiddenmap下面对应的map对应的数字是0,则须要展开全部为0的区域,因此咱们这里对hiddenmap进行了递归搜索为0的区域并为hiddenmap标注上去,以显示空白区域,咱们的作法是对当前为0的方块检查它的上下左右不为9的方块,给它标注出来,而后对上下左右递归,直到遍历整个区域,由于咱们直对当前为0的方块进行递归,因此不会使咱们的递归遍历整个图,只会遍历当前区域。这个是findzero方法的具体思想。

public void findZero(int i, int j) {
        if (hiddenmap[i][j] != 0) {
            if (map[i][j] == 0) {
                hiddenmap[i][j] = 0;

                if (i == 0) {
                    if (j == 0) {
                        if (map[i][j + 1] != 0 && map[i][j + 1] != 9)
                            hiddenmap[i][j + 1] = map[i][j + 1];
                        if (map[i + 1][j] != 0 && map[i + 1][j] != 9)
                            hiddenmap[i + 1][j] = map[i + 1][j];
                    } 
                    else if (j == length - 1) {
                        if (map[i][j - 1] != 0 && map[i][j - 1] != 9)
                            hiddenmap[i][j - 1] = map[i][j - 1];
                        if (map[i + 1][j] != 0 && map[i + 1][j] != 9)
                            hiddenmap[i + 1][j] = map[i + 1][j];
                    } 
                    else {
                        if (map[i][j - 1] != 0 && map[i][j - 1] != 9)
                            hiddenmap[i][j - 1] = map[i][j - 1];
                        if (map[i + 1][j] != 0 && map[i + 1][j] != 9)
                            hiddenmap[i + 1][j] = map[i + 1][j];
                        if (map[i][j + 1] != 0 && map[i][j + 1] != 9)
                            hiddenmap[i][j + 1] = map[i][j + 1];
                    }
                }

                if (i == width - 1) {
                    if (j == 0) {
                        if (map[i][j + 1] != 0 && map[i][j + 1] != 9)
                            hiddenmap[i][j + 1] = map[i][j + 1];
                        if (map[i - 1][j] != 0 && map[i - 1][j] != 9)
                            hiddenmap[i - 1][j] = map[i - 1][j];
                    } else if (j == length - 1) {
                        if (map[i - 1][j] != 0 && map[i - 1][j] != 9)
                            hiddenmap[i - 1][j] = map[i - 1][j];
                        if (map[i][j - 1] != 0 && map[i][j - 1] != 9)
                            hiddenmap[i][j - 1] = map[i][j - 1];
                    } else {
                        if (map[i][j + 1] != 0 && map[i][j + 1] != 9)
                            hiddenmap[i][j + 1] = map[i][j + 1];
                        if (map[i - 1][j] != 0 && map[i - 1][j] != 9)
                            hiddenmap[i - 1][j] = map[i - 1][j];
                        if (map[i][j - 1] != 0 && map[i][j - 1] != 9)
                            hiddenmap[i][j - 1] = map[i][j - 1];
                    }
                }

                if (j == 0) {
                    if (i != 0 && i != width - 1) {
                        if (map[i - 1][j] != 0 && map[i - 1][j] != 9)
                            hiddenmap[i - 1][j] = map[i - 1][j];
                        if (map[i + 1][j] != 0 && map[i + 1][j] != 9)
                            hiddenmap[i + 1][j] = map[i + 1][j];
                        if (map[i][j + 1] != 0 && map[i][j + 1] != 9)
                            hiddenmap[i][j + 1] = map[i][j + 1];
                    }
                }
                if (j == length - 1) {
                    if (i != 0 && i != width - 1) {
                        if (map[i - 1][j] != 0 && map[i - 1][j] != 9)
                            hiddenmap[i - 1][j] = map[i - 1][j];
                        if (map[i + 1][j] != 0 && map[i + 1][j] != 9)
                            hiddenmap[i + 1][j] = map[i + 1][j];
                        if (map[i][j - 1] != 0 && map[i][j - 1] != 9)
                            hiddenmap[i][j - 1] = map[i][j - 1];
                    }
                }
                if (i != 0 && i != width - 1 && j != 0 && j != length - 1) {
                    if (map[i][j + 1] != 0 && map[i][j + 1] != 9)
                        hiddenmap[i][j + 1] = map[i][j + 1];
                    if (map[i + 1][j] != 0 && map[i + 1][j] != 9)
                        hiddenmap[i + 1][j] = map[i + 1][j];
                    if (map[i][j - 1] != 0 && map[i][j - 1] != 9)
                        hiddenmap[i][j - 1] = map[i][j - 1];
                    if (map[i - 1][j] != 0 && map[i - 1][j] != 9)
                        hiddenmap[i - 1][j] = map[i - 1][j];
                }

                if (j >= 1)
                    findZero(i, j - 1);
                if (i >= 1)
                    findZero(i - 1, j);
                if (j <= getLength() - 2)
                    findZero(i, j + 1);
                if (i <= getWidth() - 2)
                    findZero(i + 1, j);
            }
        }
    }

 

setButton函数为JButton[][]数组每一个位置放置图片:

public void setButton(JButton button, int i, int j) {
        if (minefield.getHiddenMap()[i][j] == 0)
            button.setIcon(new ImageIcon("whilt.png"));
        if (minefield.getHiddenMap()[i][j] == 1)
            button.setIcon(new ImageIcon("whilt-1.png"));
        if (minefield.getHiddenMap()[i][j] == 2)
            button.setIcon(new ImageIcon("whilt-2.png"));
        if (minefield.getHiddenMap()[i][j] == 3)
            button.setIcon(new ImageIcon("whilt-3.png"));
        if (minefield.getHiddenMap()[i][j] == 4)
            button.setIcon(new ImageIcon("whilt-4.png"));
        if (minefield.getHiddenMap()[i][j] == 5)
            button.setIcon(new ImageIcon("whilt-5.png"));
        if (minefield.getHiddenMap()[i][j] == 6)
            button.setIcon(new ImageIcon("whilt-6.png"));
        if (minefield.getHiddenMap()[i][j] == 7)
            button.setIcon(new ImageIcon("whilt-7.png"));
        if (minefield.getHiddenMap()[i][j] == 8)
            button.setIcon(new ImageIcon("whilt-8.png"));
        if (minefield.getHiddenMap()[i][j] == 9)
            button.setIcon(new ImageIcon("boom.png"));
        if (minefield.getHiddenMap()[i][j] == 10)
            button.setIcon(new ImageIcon("blue.png"));
        if (minefield.getHiddenMap()[i][j] == 11)
            button.setIcon(new ImageIcon("red.png"));
        if (minefield.getHiddenMap()[i][j] == 12)
            button.setIcon(new ImageIcon("redboom.png"));
    }

2..文件操做

咱们引入了文件保存的机制,为了避免用保存过多的参数,并且不但愿一个一个量地保存,咱们把全部的操做须要用到的数据都保存到了minefield类里面,包含咱们整个扫雷模块的数据,由于学习过文件处理,咱们以object的形式能够把全部类都保存起来,再以一样的方式读取,并强制转换类型为minefield子类,就能够恢复咱们以前保存的数据,因此咱们就引入了文件保存机制,能使用户保存他的上一局未完成的游戏,咱们会以用户名的形式建立一个同名的file来保存,文件保存在当前目录下。固然,若是没有上一局记录的话,就找不到咱们的记录文件,咱们会默认打开一个初始化的界面。

读取上局文件:

if (file.isNewOne() == false) {
                try {
                    readFile = new ObjectInputStream(new FileInputStream(file.getFileName()));
                    this.minefield = (Minefield) readFile.readObject();
                    readFile.close();
                    if (minefield.isBoom() == true) {
                        boom.play();
                        upset.play();
                    } else {
                        playGame.play();
                    }
                } catch (FileNotFoundException e) {   //不存在上局时,自动生成一局
                    this.minefield = new Minefield(file.getWidth(), file.getLength(), file.getLambnumber());
                    playGame.play();
//                        JOptionPane.showMessageDialog(null, "您还未开始游戏,不存在上局哦,即将为您自动生成上局!");
//                        e.printStackTrace();
                } catch (IOException e) {
//                        e.printStackTrace();
                }
                
            }

 

 保存进度:

item3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    ObjectOutputStream dateSave = new ObjectOutputStream(new FileOutputStream(getFileName()));
                    dateSave.writeObject(minefield);
                    dateSave.close();
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

 

 

 

5、课程设计感想

经过此次课程设计,我大大提升了本身的自主学习能力,俗话说“师傅领进门,修行在我的”,要完成如此复杂的课程设计,仅靠老师上课教授的知识是远远不够的,须要咱们本身去多加学习。在学习中,还应学会提问的方法,遇文图时不要慌张,要仔细往根源去找问题,不要一有问题就寻求老师同窗帮忙,要有自主解决问题的能力。

相关文章
相关标签/搜索