Created
October 24, 2013 08:08
-
-
Save liunian/7133112 to your computer and use it in GitHub Desktop.
A to Z in 10x10
This file contains hidden or 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
| /* | |
| 10 x 10 | |
| A-Z ( 65 -> 90) | |
| output something like this | |
| * * * * * * * * * * | |
| * * * * * * * * * * | |
| * * * * * * * * * * | |
| * J I * * * * * * * | |
| * K H * * * * * * * | |
| M L G F E * * * * * | |
| N A B C D * * * * * | |
| O V U * * * * * * * | |
| P W T * * * * * * * | |
| Q R S * * * * * * * | |
| */ | |
| var n = 10; | |
| // start point | |
| var x = getRandomNum(n), | |
| y = getRandomNum(n); | |
| var s = 65, | |
| e = 90; | |
| var matrix = []; | |
| for(var ni=0; ni<n; ni++) { | |
| matrix[ni] = new Array(n); | |
| } | |
| matrix[x][y] = String.fromCharCode(s); | |
| s++; | |
| while ( s <= e) { | |
| var next = getNextLocation(x, y); | |
| //console.log(next); | |
| if (next === null) break; | |
| x = next[0]; | |
| y = next[1]; | |
| matrix[x][y] = String.fromCharCode(s++); | |
| } | |
| for (var i=0; i<n; i++) { | |
| var str = ''; | |
| for (var j=0; j<n; j++) { | |
| if (matrix[i][j]) { | |
| str += matrix[i][j] + ' '; | |
| } else { | |
| str += '* '; | |
| } | |
| } | |
| console.log(str); | |
| } | |
| function getRandomNum(max) { | |
| return Math.round(Math.random() * (max - 1)); | |
| } | |
| function getNextLocation(x, y) { | |
| var alternates = []; | |
| if ((x-1 >= 0) && !matrix[x-1][y]) alternates.push([x-1, y]); | |
| if ((x+1 < n) && !matrix[x + 1][y]) alternates.push([x+1, y]); | |
| if ((y-1 >= 0) && !matrix[x][y-1]) alternates.push([x, y-1]); | |
| if ((y+1 < n) && !matrix[x][y+1]) alternates.push([x, y+1]); | |
| if (alternates.length == 0) return null; | |
| return alternates[getRandomNum(alternates.length)]; | |
| } | |
| //@ sourceURL=A_Z_in_10x10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment