CocosCreator实现华容道的思路和代码是什么
Admin 2022-10-28 群英技术资讯 643 次浏览
华容道是啥玩意?
这种数字拼图游戏大家都玩过吧,他就是典型的华容道之一。
华容道是古老的中国民间益智游戏,以其变化多端、百玩不厌的特点与魔方、独立钻石棋一起被国外智力专家并称为“智力游戏界的三个不可思议”。
今天咱们就来了解一下这个华容道。
今天咱们主要以3*3的布局来进行,菜鸟用cocos creator 写了一个简单的demo,下面咱们逐步说一下
首先咱们随机生成一个面板排列
思路:
穷举法:大家都知道这种游戏的玩法,滑动其中可以滑动的方格,将打乱的方格按照上边数字从小到大的顺序依次排列即可通关。在这里菜鸟利用了穷举法,在每种可能的情况中去查找最优解。
在穷举法中我们常见的有:
在这里我们用的是广度优先搜索,我们只需要拿到最优解,也就是步数最少的。
具体操作如图:
我们以前三步为例,
得到了解,我们可以应用到demo,检测是否可以通关
点击demo中的自动排列
//循环遍历求解 while (true) { let steps: Array<any> = []; let lastGrad: Array<any> = this.mMapData[this.mMapData.length - 1]; console.log(lastGrad.length); //遍历作最后一个梯度中所有的结果,求解下一步 for (let i = 0; i < lastGrad.length; i++) { let matrix = lastGrad[i]["matrix"]; let answer = lastGrad[i]["answer"]; let result: Array<any> = this.move(matrix, answer, steps); if (result) { console.log("结果:", result); resolve(result); return; } } if(steps.length<=0){ console.log("查询结果失败,"); resolve(null); return; } this.mMapData.push(steps); }
private move(matrix: Array<number>, answer: Array<any>, steps: Array<any>): Array<any> { for (let i = 0; i < matrix.length; i++) { if (matrix[i] != -1) { //不是空位,检测是否可移动,获取可移动结果 //检测上下左右是否可以移动, let result0: Array<any> = this.moveUp(i, matrix, answer, steps); let result1: Array<any> = this.moveDown(i, matrix, answer, steps); let result2: Array<any> = this.moveLeft(i, matrix, answer, steps); let result3: Array<any> = this.moveRight(i, matrix, answer, steps); if (result1) { return result1; } if (result2) { return result2; } if (result0) { return result0; } if (result3) { return result3; } } } return null; }
private moveRight(i: number, matrix: Array<number>, answer: Array<any>, steps: Array<any>): Array<any> { let line: number = i % this.mLine; let row: number = Math.floor(i / this.mLine); if (line + 1 >= this.mLine) return null; //超出边界 let targetIndex: number = row * this.mLine + (line + 1); if ( matrix[targetIndex] != -1) return null; //不可移动 //移动 //移动 //复制新的数组进行修改 let newMatrix: Array<number> = JSON.parse(JSON.stringify(matrix)); let newAnswer: Array<any> = JSON.parse(JSON.stringify(answer)); //互换位置 let temp: number = newMatrix[i]; newMatrix[i] = newMatrix[targetIndex]; newMatrix[targetIndex] = temp; newAnswer.push({ "index": i, "dic": 3 }); if (this.checkIsExist(newMatrix)) { return null; } if (this.checkPass(newMatrix)) { return newAnswer; } let step: any = {}; step["matrix"] = newMatrix; step["answer"] = newAnswer; steps.push(step); } /** * 检测是否通关 */ private checkPass(matrix: Array<number>): boolean { if (matrix[this.mRow * this.mLine - 1] != -1) return false; for (let i = 0; i < this.mRow * this.mLine - 1; i++) { if (matrix[i] != i + 1) { return false; } } console.log(matrix) return true; } /** * 检测是否重复 */ private checkIsExist(matrix): boolean { if (this.mMapMatrixS[JSON.stringify(matrix)]) { return true; } this.mMapMatrixS[JSON.stringify(matrix)] ="1"; return false; }
demo中是3 * 3的排列,使用浏览器勉强可以跑出结果,但是4 * 4或者5 * 5的就不行了,因为分支太多。后续有时间菜鸟会用python脚本实现4 * 4,5 * 5或更多的排列,最终导出json关卡信息。
原文链接:https://blog.csdn.net/carlos13207/article/details/108663593
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
今天给大家分享的是有关vue实现dom批量更新的内容,下文有具体实例代码供大家参考,有这方面学习需要的朋友可以借鉴参考,下面跟随小编来学习一下吧。
jquery怎么修改data-optionsHtml5 data-* 属性定义和用法:data-* 属性用于存储页面或应用程序的私有自定义数据。data-* 属性赋予我们在所有 HTML 元素上嵌入自定义 data 属
目录一.前言二.想法设计/实现过程三.基本样式四.时间函数控制器五,时,分,秒占位六.时间动态填充一.前言今天看到某网站的时间特别的丑陋,所以就诞生了写一个看时间的炫酷的时钟前端页面。 特点就是炫酷,特效好,个人以心情愉快的感觉。 对于时间的变化,我打算使用翻页的特效来完成,色系的话采用黑色以主题,给人一种神秘的感觉。
配置文件:RedisOptions.jsconstoptions={host:'208.167.233.104',port:15001,password:'123456',detect_buffers:true//传入buffer返回也是buffer否则会转换成String}module.exports=options 封装r
本文主要介绍了Vue3实现刷新页面局部内容的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008