{{ message }}
This repository has been archived by the owner. It is now read-only.

## freeCodeCamp / arcade-mode Public archive

Switch branches/tags
Nothing to show

Cannot retrieve contributors at this time
354 lines (324 sloc) 12 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 /* eslint spaced-comment: 0 */ /* eslint no-redeclare: 0 */ /* eslint no-unused-vars: 0 */ const assert = require('assert'); /// title: Maze generation /// type: rosetta-code /// categories: /// difficulty: ? /// benchmark: replaceWithActualFunctionHere; /// description: ///
///

///
///

Generate and show a maze, using the simple Depth-first search algorithm.

///
1. Start at a random cell.
2. ///
3. Mark the current cell as visited, and get a list of its neighbors. For each neighbor, starting with a randomly selected neighbor:
4. ///
5. :If that neighbor hasn't been visited, remove the wall between this cell and that neighbor, and then recurse with that neighbor as the current cell.
///

///

/// challengeSeed: function replaceMe (foo) { // Good luck! return true; } /// solutions: function maze(x,y) { var n=x*y-1; if (n<0) {alert("illegal maze dimensions");return;} var horiz =[]; for (var j= 0; j0 && j0 && (j != here[0]+1 || k != here[1]+1)); } while (00 && m.verti[j/2-1][Math.floor(k/4)]) line[k]= ' '; else line[k]= '-'; else for (var k=0; k0 && m.horiz[(j-1)/2][k/4-1]) line[k]= ' '; else line[k]= '|'; else line[k]= ' '; if (0 == j) line[1]= line[2]= line[3]= ' '; if (m.x*2-1 == j) line[4*m.y]= ' '; text.push(line.join('')+'\r\n'); } return text.join(''); } /// rawSolutions: =={{header|JavaScript}}== {{trans|J}} function maze(x,y) { var n=x*y-1; if (n<0) {alert("illegal maze dimensions");return;} var horiz =[]; for (var j= 0; j0 && j0 && (j != here[0]+1 || k != here[1]+1)); } while (00 && m.verti[j/2-1][Math.floor(k/4)]) line[k]= ' '; else line[k]= '-'; else for (var k=0; k0 && m.horiz[(j-1)/2][k/4-1]) line[k]= ' '; else line[k]= '|'; else line[k]= ' '; if (0 == j) line[1]= line[2]= line[3]= ' '; if (m.x*2-1 == j) line[4*m.y]= ' '; text.push(line.join('')+'\r\n'); } return text.join(''); } Variable meanings in function maze: # x,y — dimensions of maze # n — number of openings to be generated # horiz — two dimensional array of locations of horizontal openings (true means wall is open) # verti — two dimensional array of locations of vertical openings (true means wall is open) # here — current location under consideration # path — history (stack) of locations that might need to be revisited # unvisited — two dimensional array of locations that have not been visited, padded to avoid need for boundary tests (true means location needs to be visited) # potential — locations adjacent to here # neighbors — unvisited locations adjacent to here Variable meanings in function display: # m — maze to be drawn # text — lines of text representing maze # line — characters of current line Note that this implementation relies on javascript arrays being treatable as infinite in size with false (null) values springing into existence as needed, to support referenced array locations. (This significantly reduces the bulk of the necessary initialization code.) {{out|Example use}}

produced output:
+   +---+---+---+---+---+---+---+---+---+---+
|                   |                   |   |
+---+---+   +   +---+   +   +---+---+   +   +
|       |   |   |       |   |           |   |
+   +   +   +---+   +---+   +---+---+   +   +
|   |   |               |           |   |   |
+   +---+   +---+---+---+---+---+   +   +   +
|       |   |               |       |       |
+---+   +---+   +---+---+   +   +---+---+   +
|   |   |       |               |       |   |
+   +   +   +---+---+---+---+---+   +   +   +
|       |                   |       |   |   |
+   +---+---+   +---+---+   +   +---+---+   +
|   |       |   |           |       |       |
+   +   +   +---+   +---+---+   +   +   +---+
|       |           |           |
+---+---+---+---+---+---+---+---+---+---+---+
For an animated presentation of the progress of this maze creation process, you can use display in each iteration of the main loop. You would also need to take steps to make sure you could see each intermediate result. For example, change replace the line while (0 with: function step() { if (0 And replace the closing brace for this while loop with: document.getElementById('out').innerHTML= display({x: x, y: y, horiz: horiz, verti: verti, here: here}); setTimeout(step, 100); } } step(); To better see the progress, you might want a marker in place, showing the position being considered. To do that, replace the line which reads if (0 == k%4) { with if (m.here && m.here[0]*2+1 == j && m.here[1]*4+2 == k) line[k]= '#' else if (0 == k%4) { Note however that this leaves the final '#' in place on maze completion, and that the function maze no longer returns a result which represents a generated maze. Note also that this display suggests an optimization. You can replace the line reading path.push(here= next); with: here= next; if (1 < neighbors.length) path.push(here); And this does indeed save a negligible bit of processing, but the maze algorithm will still be forced to backtrack through a number of locations which have no unvisited neighbors. ===HTML Table=== Using HTML, CSS and table cells for maze. Maze maker
Generate
/// tail: const replaceThis = 3; /// tests: assert(typeof replaceMe === 'function', 'message: replaceMe is a function.'); /// id: 5a23c84252665b21eecc7f10