-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic.js
More file actions
56 lines (51 loc) · 758 Bytes
/
magic.js
File metadata and controls
56 lines (51 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// (c) copyright - Vasian Cepa
function CreateMatrix(n)
{
if(n <= 0) return null;
if((n % 2) == 0) return null;
var m = new Array(n);
for(var i = 0; i < n; i++)
{
m[i] = new Array(n);
}
for(var i = 0; i < n; i++)
{
for(var j = 0; j < n; j++)
{
m[i][j] = 0;
}
}
return m;
}
function FillTable(n)
{
var i = 0, k = 0, j = 0;
var n2 = n*n;
var m = CreateMatrix(n);
j = (n-1)/2;
while(k++ < n2){
//SetElement(i, j, k);
m[i][j] = k;
if(i==0){
if(j == n - 1) i++;
else{
i = n - 1;
j++;
}
} else {
if(j == n - 1){
i--;
j = 0;
} else {
if(m[i-1][j+1] == 0)
{
i--;
j++;
} else {
i++;
}
}
}
}
return m;
}