Php generates a maze and a maze addressing algorithm instance, which is better than the previous one. The algorithm used to generate a maze is the same as the addressing algorithm. Only one uses traversal and the other uses recursion. Refer to the MikeGold algorithm on the Internet. PHPcode & lt ;? Phpzairwolf [email & #160; protected] header (Content-Type: texthtml; charsetutf-8); e php generates examples of maze and Maze addressing algorithms
It is better than the previous one. The algorithm used to generate a maze is the same as the addressing algorithm. Only one uses traversal and the other uses recursion. Refer to the online Mike Gold algorithm.
PHP code
0) {$ tmpArr = array (); foreach ($ _ posArr as $ val) {$ nx = $ x + $ val [0]; $ ny = $ y + $ val [1]; if (isset ($ maze [$ nx] [$ ny]) {// A grid without a broken wall $ tmpArr [] = array ($ nx, $ ny) ;}} if ($ tmpArr) {// A grid without a broken wall, randomly generate a list of broken walls ($ nx, $ ny) = $ tmpArr [array_rand ($ tmpArr)]; $ maze2 [$ nx] [$ ny] = $ maze [$ nx] [$ ny]; if (empty ($ maze2 [$ x] [$ y]) $ maze2 [$ x] [$ y] = $ maze [$ x] [$ y]; $ pos = array ($ nx-$ x, $ ny-$ y ); foreach ($ _ posArr as $ key => $ val) {// cycle Find the target Wall. if ($ pos = $ val) {$ maze2 [$ x] [$ y] [$ key] = 0; // original lattice wall breaking $ maze2 [$ nx] [$ ny] [($ key + 2) % 4] = 0; // New grid wall breaking} // after setting the new current grid, the system returns to continue the while loop $ x = $ nx; $ y = $ ny; $ mazeOrder [] = array ($ x, $ y); unset ($ maze [$ x] [$ y]); // remove the grid if (empty ($ maze [$ x]) with broken walls. unset ($ maze [$ x]);} else {// no non-broken grid exists around the current xy. The last grid is returned to continue to crack the array_pop ($ mazeOrder ); if ($ mazeOrder) list ($ x, $ y) = $ mazeOrder [count ($ mazeOrder)-1] ;}// leave an exit $ maze = $ maze2; $ Maze [0] [0] [3] = 0; $ maze [M] [M] [1] = 0; // addressing $ pathArr = findPath ($ maze, 0, 0, false); printMaze ($ maze, $ pathArr); echo "refresh"; // Print the maze and addressing results by [email protected] function printMaze ($ maze, $ pathArr) {$ im = ImageCreate (M + 1) * S + 1, (M + 1) * S + 1); $ bg = ImageColorAllocate ($ im, 236,233,216); $ pathColor = ImageColorAllocate ($ im, 255, 0, 0); $ exitColor = ImageColorAllocate ($ im, 134,255, 0); $ borderColor = I MageColorAllocate ($ im, 0, 0, 0); ImageRectangle ($ im, 0, 0, (M + 1) * S, (M + 1) * S, $ borderColor); // package side ImageLine ($ im, 0, 0, 0, S, $ bg); // open ImageLine on the upper right side ($ im, (M + 1) * S, M * S, (M + 1) * S, (M + 1) * S, $ bg ); // foreach ($ maze as $ x => $ xarr) {// Generate a grid foreach ($ xarr as $ y => $ unit) {if ($ unit [0]) ImageLine ($ im, $ x * S, $ y * S, ($ x + 1) * S, $ y * S, $ borderColor); // Wired if ($ unit [1]) ImageLine ($ im, ($ x + 1) * S, $ y * S, ($ x + 1) * S, ($ y + 1) * S, $ borderColor ); // right-wired if ($ unit [2]) ImageLine ($ im, $ x * S, ($ y + 1) * S, ($ x + 1) * S, ($ y + 1) * S, $ borderColor); // if ($ unit [3]) ImageLine ($ im, $ x * S, $ y * S, $ x * S, ($ y + 1) * S, $ borderColor); // left wired // if (in_array (array ($ x, $ y ), $ pathArr) ImageFilledEllipse ($ im, $ x * S + S/2, $ y * S + S/2, S, S, $ pathColor ); // addressing lattice if (in_array (array ($ x, $ y), $ pathAr R) ImageString ($ im, 1, $ x * S + S/5, $ y * S + S/5, array_search (array ($ x, $ y ), $ pathArr), $ pathColor); // addressing grid} ImagePNG ($ im, 'maze.png '); ImageDestroy ($ im );} // addressing function [email protected] function findPath ($ maze, $ x, $ y, $ fromxy) {global $ _ posArr; if ($ x = M & $ y = M) {// Return array ($ x, $ y) at the exit ));} foreach ($ _ posArr as $ key => $ val) {if ($ maze [$ x] [$ y] [$ key]) continue; // if the value is 1, $ nx = $ x + $ val [0]; $ n Y = $ y + $ val [1]; if (! Isset ($ maze [$ nx] [$ ny]) | $ fromxy = array ($ nx, $ ny) continue; // if ($ pathArr = findPath ($ maze, $ nx, $ ny, array ($ x, $ y) {array_unshift ($ pathArr, array ($ x, $ y); Return $ pathArr; // exit reachable} Return false ;}