(function () {
h_lei = function (obj) {
obj = obj == undefined ? {} : obj;
var _this = this;
_this.dataH = [];
_this.dataS = [];
_this.big = obj.big == undefined ? 10 : obj.big;
_this.num = obj.num == undefined ? 10 : obj.num;
_this.shengyu = _this.big * _this.big - _this.num;
_this.dataH = new Array(_this.big);
_this.dataS = new Array(_this.big);
for (var i = 0; i < _this.big; i++) {
_this.dataH[i] = new Array(_this.big).fill(0);
_this.dataS[i] = new Array(_this.big).fill(-2);
}
_this.setlei = function (leishu) {
var shengyu = 0;
for (var i = 0; i < _this.big; i++) {
for (var j = 0; j < _this.big; j++) {
if (_this.dataH[i][j] != -1) {
shengyu += 1;
}
}
}
if (shengyu < leishu) {
alert("剩餘數量不夠了")
return;
}
if (leishu > 0) {
var a = Math.floor(Math.random() * _this.big);
var b = Math.floor(Math.random() * _this.big);
if (_this.dataH[a][b] != -1) {
_this.dataH[a][b] = -1;
leishu -= 1;
}
_this.setlei(leishu)
}
}
_this.setnunm = function () {
for (var i = 0; i < _this.big; i++) {
for (var j = 0; j < _this.big; j++) {
if (_this.dataH[i][j] == 0) {
var count = 0;
is = i - 1;
id = i + 1;
js = j - 1;
jd = j + 1;
if (is >= 0 && _this.dataH[is][js] == -1) count += 1;
if (is >= 0 && _this.dataH[is][j] == -1) count += 1;
if (is >= 0 && jd < _this.big && _this.dataH[is][jd] == -1) count += 1;
if (js >= 0 && _this.dataH[i][js] == -1) count += 1;
if (_this.dataH[i][j] == -1) count += 1;
if (jd < _this.big && _this.dataH[i][jd] == -1) count += 1;
if (id < _this.big && js >= 0 && _this.dataH[id][js] == -1) count += 1;
if (id < _this.big && _this.dataH[id][j] == -1) count += 1;
if (id < _this.big && jd < _this.big && _this.dataH[id][jd] == -1) count += 1;
_this.dataH[i][j] = count;
}
}
}
return _this.dataH;
}
_this.setlei(_this.num);
_this.setnunm();
_this.kailei = function (a, b) {
if (a < 0 || b < 0 || a >= _this.big || b >= _this.big) {
return;
}
var jg = _this.dataH[a][b]
if (jg == -1) {
_this.dataS = _this.dataH;
console.log('雷');
return -1;
} else {
_this.dataS[a][b] = jg
}
_this.shuaxin()
console.log(_this.shengyu)
return 0;
}
_this.shuaxin = function () {
var b = false;
var shenyu = 0;
for (var i = 0; i < _this.big; i++) {
for (var j = 0; j < _this.big; j++) {
if (_this.dataH[i][j] != -1 && _this.dataS[i][j] != _this.dataH[i][j]) {
shenyu += 1;
}
if (_this.dataS[i][j] == -2) {
is = i - 1;
id = i + 1;
js = j - 1;
jd = j + 1;
if ((is >= 0 && _this.dataS[is][js] == 0)
|| (is >= 0 && _this.dataS[is][j] == 0)
|| (is >= 0 && jd < _this.big && _this.dataS[is][jd] == 0)
|| (js >= 0 && _this.dataS[i][js] == 0)
|| (jd < _this.big && _this.dataS[i][jd] == 0)
|| (id < _this.big && js >= 0 && _this.dataS[id][js] == 0)
|| (id < _this.big && _this.dataS[id][j] == 0)
|| (id < _this.big && jd < _this.big && _this.dataS[id][jd] == 0)) {
_this.dataS[i][j] = _this.dataH[i][j]
b = true;
}
}
}
}
if (b) {
_this.shuaxin()
} else {
_this.shengyu = shenyu
}
return shenyu;
}
}
})(jQuery);
标签:脚本,游戏,big,扫雷,&&,var,id,dataS,dataH
From: https://www.cnblogs.com/mufuhu/p/18397016