# java课程设计——扫雷

## 一、团队课程设计博客链接：

https://www.cnblogs.com/luomeili/p/10280310.html

## 四、自己负责模块或任务详细说明

1.Minefield类实现

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

生成雷：

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

}
}

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..文件操作

if (file.isNewOne() == false) {
try {
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();
}
}
});

## 五、课程设计感想

posted @ 2019-01-17 09:29  陈玉婷  阅读(2324)  评论(0编辑  收藏  举报