How php generates an example of a maze and a maze addressing algorithm

Source: Internet
Author: User
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 ;? Phpzairwolfz@cot8.comheader (Content-Type: texthtml; charsetutf-8); error_re php generate 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 z@cot8.comfunction 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 = ImageCol OrAllocate ($ 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), $ pathArr) Ima GeString ($ im, 1, $ x * S + S/5, $ y * S + S/5, array_search (array ($ x, $ y), $ pathArr ), $ pathColor); // addressing lattice} ImagePNG ($ im, 'maze.png '); ImageDestroy ($ im);} // addressing function z@cot8.comfunction findPath ($ maze, $ x, $ y, $ fromxy) {global $ _ posArr; if ($ x = M & $ y = M) {// Return array ($ x, $ y);} foreach ($ _ posArr as $ key => $ val) {if ($ maze [$ x] [$ y] [$ key]) continue; // if the value is 1, $ nx = $ x + $ val [0]; $ ny = $ 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 ;}


------ Solution --------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.