(field, x, y)
| 1 | function generateSquareMaze(dimension) { |
| 2 | |
| 3 | function iterate(field, x, y) { |
| 4 | field[x][y] = false; |
| 5 | while(true) { |
| 6 | directions = []; |
| 7 | if(x > 1 && field[x-2][y] == true) { |
| 8 | directions.push([-1, 0]); |
| 9 | } |
| 10 | if(x < field.dimension - 2 && field[x+2][y] == true) { |
| 11 | directions.push([1, 0]); |
| 12 | } |
| 13 | if(y > 1 && field[x][y-2] == true) { |
| 14 | directions.push([0, -1]); |
| 15 | } |
| 16 | if(y < field.dimension - 2 && field[x][y+2] == true) { |
| 17 | directions.push([0, 1]); |
| 18 | } |
| 19 | if(directions.length == 0) { |
| 20 | return field; |
| 21 | } |
| 22 | dir = directions[Math.floor(Math.random()*directions.length)]; |
| 23 | field[x+dir[0]][y+dir[1]] = false; |
| 24 | field = iterate(field, x+dir[0]*2, y+dir[1]*2); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // Initialize the field. |
| 29 | var field = new Array(dimension); |
no outgoing calls
no test coverage detected