扫雷游戏,在点击某点A的时候,并且A是空白区域,那么他会直接打开一片。java的类似如下:
package com.jue.rescursion;
import java.util.HashMap;
import java.util.Map;
public class Box {
private Map<String,Boolean> myMap= new HashMap<String,Boolean>();
private int[][] box = {
{ 0, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 1, 1, 1 },
{ 1, 0, 0, 1, 1, 1 },
{ 1, 0, 0, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 1 },
{ 1, 1, 1, 1, 1, 1 },
};
public void touchCell(int row, int colum) {
if (row < 0 || colum < 0 || row > (box[0].length - 1) || colum > (box.length - 1)) {
return;
}
if (box[row][colum] == 0) {
String index = row + "+" + colum;
if (!myMap.containsKey(index)) {
myMap.put(index, true);
} else {
return;
}
System.out.println("(" + row + "," + colum + ") fired " + box[row][colum]);
touchCell(row - 1, colum);
touchCell(row, colum - 1);
touchCell(row + 1, colum);
touchCell(row, colum + 1);
}
}
}
本文介绍了一个简单的扫雷游戏实现方式,使用Java编程语言通过递归方法展开空白区域。该实现利用了二维数组来存储游戏地图,并使用HashMap来记录已触碰的单元格。
233

被折叠的 条评论
为什么被折叠?



