PrefaceWhat is Huarong Road? Everyone has played this kind of number puzzle game, I believe. It is a typical example of Huarong Road. Huarongdao is an ancient Chinese folk puzzle game. With its many variations and never-boring features, it is called "three wonders in the world of intellectual games" by foreign intelligence experts along with the Rubik's Cube and Independent Diamond Chess. Today we will learn about Huarong Road. textToday we will mainly use a 3*3 layout. A rookie wrote a simple demo using cocos creator. Let's talk about it step by step. 1. PanelFirst, we randomly generate a panel arrangement 2. Huarongdao SolutionIdeas: Exhaustive method: Everyone knows how to play this game. Slide the sliding squares and arrange the shuffled squares in order from small to large according to the numbers on them to pass the level. Here, the novice uses the exhaustive method to find the optimal solution in every possible situation. In the exhaustive method, we often see:
Here we use breadth-first search, we only need to get the optimal solution, that is, the one with the least number of steps. Let’s take the first three steps as an example.
After understanding, we can apply it to the demo to test whether it can pass the level. Click on the automatic arrangement in the demo 3. Code//Loop through the solution while (true) { let steps: Array<any> = []; let lastGrad: Array<any> = this.mMapData[this.mMapData.length - 1]; console.log(lastGrad.length); //Traverse all the results in the last gradient and solve the next step 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:", result); resolve(result); return; } } if(steps.length<=0){ console.log("Query result failed, "); 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) { //Not an empty space, check whether it can be moved, and get the movable result //Check whether it can be moved up, down, left, and right, 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; //Out of bounds let targetIndex: number = row * this.mLine + (line + 1); if ( matrix[targetIndex] != -1) return null; //Not movable //Move //Move //Copy the new array for modification let newMatrix: Array<number> = JSON.parse(JSON.stringify(matrix)); let newAnswer: Array<any> = JSON.parse(JSON.stringify(answer)); //Exchange positions 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); } /** * Check if it is cleared*/ 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; } /** * Check if it is repeated */ private checkIsExist(matrix): boolean { if (this.mMapMatrixS[JSON.stringify(matrix)]) { return true; } this.mMapMatrixS[JSON.stringify(matrix)] = "1"; return false; } 4. NoteThe demo shows a 3 * 3 arrangement, which can be run using a browser, but 4 * 4 or 5 * 5 cannot be run because there are too many branches. Later, I will use Python scripts to implement 4 * 4, 5 * 5 or more arrangements, and finally export JSON level information. The above is a detailed explanation of the CocosCreator Huarongdao digital puzzle. For more information about CocosCreator Huarongdao, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed installation tutorial of Mysql5.7.19 under Centos7
1. Property List Copy code The code is as follows:...
For a website, usability refers to whether users c...
<br />I just saw the newly revamped ChinaUI....
<br />Scientifically Design Your Website: 23...
The main function of the brower module is to dete...
For record, it may be used in the future, and fri...
Encapsulate a navigation bar component in Vue3, a...
MySQL Advanced SQL Statements use kgc; create tab...
vmware vsphere 6.5 is the classic version of vsph...
Definition of Generics // Requirement 1: Generics...
Before I start, let me emphasize that process.env...
This article describes the MySQL single table que...
1. Upper and lower list tags: <dl>..</dl...
When faced with a SQL statement that is not optim...
Note: sg11 Our company only supports self-install...