cocos create 计算扫雷3BV
程序员文章站
2024-03-25 19:08:40
...
循环每一个格子
/**
* 计算扫雷的3BV
*/
Calculate3BV(){
let n=0;
for(let i=0;i<this.rowNum;i++){
for(let j=0;j<this.colNum;j++){
n+=this.Check3BV(this.pieceMap[i][j])?1:0;
}
}
console.log("3BV:"+n);
}
检测每一个块 其中Piece为 每个块的代码脚本
Check3BV(piece:Piece){
console.log("type:",piece.type);
if(piece.HadCheck3BV){
return false;
}
//如果是雷 不计数
if(piece.type===PIECE_TYPE.BOMB){
piece.HadCheck3BV=true;
return false;
}
//如果是数字1-8 看是否周围有数字0 有则不计数 没有就是island 计数
if(piece.type>=PIECE_TYPE.OPEN1&&piece.type<=PIECE_TYPE.OPEN8){
let is3BV = true;
let roundPieces = this.getRoundPieces(piece.x, piece.y);
roundPieces.forEach(p => {
if (p.type === PIECE_TYPE.OPEN0) {
is3BV = false;
}
});
if(is3BV){
console.log("island++");
}
return is3BV;
}
if(piece.type===PIECE_TYPE.OPEN0){
let testPieces: Array<Piece> = [piece];
while (testPieces.length > 0) {
//console.log("1231");
let testPiece = testPieces.pop();
console.log("长度:",testPieces.length);
if(testPiece.HadCheck3BV){
}else{
console.log(testPiece.x," ",testPiece.y);
testPiece.HadCheck3BV=true;
if (testPiece.type === PIECE_TYPE.OPEN0) {
let roundPieces = this.getRoundPieces(testPiece.x, testPiece.y);
roundPieces.forEach(p => {
if(!p.HadCheck3BV&&testPieces.indexOf(p)==-1){
console.log("push:",p.x," ",p.y);
testPieces.push(p);
}
});
}
}
}
console.log("top ++");
return true;
}
}
推荐阅读