I would like to create a random path from the top to the bottom of the matrix.
Fiddle
Requirements:
- The path can rotate, but it must connect from line 1 to the last line.
- In the end, I would like the colors to be random for each part of the path, but at the moment this can be uniform (I tested below only in red).
- Paths connecting from top to bottom are randomly generated.
- Obviously, the fragments of the path should be connected and should not be fork (aka, give the player 2 choices shown here).
- The path can only go from top to bottom (cannot move backward), but it can rotate left and right

What I thought:
- I can't just check if the row column above is part of the path, because then it will continuously generate fragments of the path when it finds the first true value.
- I am not interested in creating paths manually, as this will require a new matrix indicating 1 and 0, where I want the path to go. And then for each option of the "random" path I will have to build a new matrix. More importantly, manually generating the path in the matrices will make scaling the matrix size much more tedious ... For example, if I changed my 6x6 matrix to 100x100.
So yes, an easy way would be to just do it and go through it:
var matrixPaths = [ [0,1,0,0,0,0], [0,1,1,1,0,0], [0,0,0,1,0,0], [0,0,0,1,1,1], [0,0,0,0,0,1], [0,0,0,0,0,1] ];
On the left is an empty grid on the right that it should generate

My thought was to first create a grid and add gaps in each matrix entry:
function createMyGrid() { //create 6x6 matrix for(var i=1; i<=6; i++) { matrix[i] = []; for(var j=1; j<=6; j++) { var colorIndex = Math.floor(Math.random() * (color.length - 0) + 0); var $span = $('<span />').attr('class', 'colorSquare').html("[" + i + "][" + j + "]"); $("#grid").append($span); matrix[i][j] = $span; } } }
Then generate the first part of the path in random order on line 1. Then, for each next line, make sure that there is a track above it to connect ... then from this part run the following set:
function createPath() { var randomColumn = Math.floor(Math.random() * (matrix[1].length - 0) + 0); matrix[1][randomColumn].data('partOfPath', true); matrix[1][randomColumn].addClass("red"); for (var row = 2; row <= 6; row++) { for (var col = 1; col <= 6; col++) { if (matrix[row-1][col].data('partOfPath')) {
While he adds the initial step, then the line below, but then stops.
