WPS表格VB宏编程入门:从录制宏到编写智能自动化脚本
2026/7/30 3:57:34
在「猫猫大作战」中,每次猫咪着陆或新猫出现后,需要检测是否有三只以上同级猫咪相邻。合并检测是游戏逻辑的核心环节。
privatedetectMerges():void{consttoMerge:{x:number;y:number}[]=[];// 遍历棋盘所有位置for(lety=0;y<GameConfig.BOARD_HEIGHT;y++){for(letx=0;x<GameConfig.BOARD_WIDTH;x++){constcat=this.board[y][x];if(!cat||cat.falling)continue;// 查找同级邻居constneighbors=this.findSameLevelNeighbors(x,y,cat.level);if(neighbors.length>=2){toMerge.push({x,y},...neighbors.slice(0,2));}}}this.processMergeBatch(toMerge);}privatefindSameLevelNeighbors(x:number,y:number,level:CatLevel):{x:number;y:number}[]{constdirections=[[-1,0],[1,0],// 左右[0,-1],[0,1],// 上下];constneighbors:{x:number;y:number}[]=[];for(const[dx,dy]ofdirections){constnx=x+dx;constny=y+dy;// 边界检查if(nx<0||nx>=5||ny<0||ny>=8)continue;constcat=this.board[ny][nx];if(cat&&!cat.falling&&cat.level===level){neighbors.push({x:nx,y:ny});}}returnneighbors;}privateprocessMergeBatch(cats:{x:number;y:number}[]):void{// 去重constunique=newSet(cats.map(c=>`${c.x},${c.y}`));if(unique.size<3)return;// 取前三个consttoRemove=Array.from(unique).slice(0,3).map(k=>{const[x,y]=k.split(',').map(Number);return{x,y};});// 执行合并constfirst=toRemove[0];constcat=this.board[first.y][first.x];if(!cat)return;// 移除this.removeCats(toRemove);// 升级constnewLevel=Math.min(cat.level+1,CatLevel.LEGENDARY)asCatLevel;this.createCatAt(first.x,first.y,newLevel);// 递归this.tryMergeAt(first.x,first.y);}| 时机 | 触发 | 说明 |
|---|---|---|
| 猫咪着陆后 | 立即检测 | 新停落的猫 |
| 合并升级后 | 递归检测 | 新生成的猫 |
| 新猫投放后 | 检测新猫位置 | 防止重叠 |
MAX_CHAIN防止无限递归合并检测扫描棋盘查找同级相邻猫咪,触发合并升级。核心要点:四方向邻居检测、 批量合并去重、 递归链式响应。
如果这篇文章对你有帮助,欢迎点赞👍、收藏⭐、关注🔔,你的支持是我持续创作的动力!
相关资源: