Java编程

java扫雷教程,javaee黑马28期全套教程

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

二、个人负责模块或任务说明:模块:文件操作  Minefield类实现三、自己的代码提交记录截图

四、自己负责模块或任务详细说明1.Minefield类实现

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

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

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

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

生成map数组其他位置的数字:

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 (m

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注