【发布时间】:2014-07-07 19:51:47
【问题描述】:
我正在使用下划线模板构建数独板。我在计算推导表结构的数学时遇到了一些麻烦。
我的底层结构是一个一维数组(大小为 81)。我相信解决这个问题的最好方法是用 3x3 的小立方体来构建它。
[1,2,3,4....] -> [1,2,3 [1,2,3 .... 4,5,6 4,5,6 7,8,9] 7,8,9]
但我不知道如何获取第一个立方体的值,然后是下一个,然后是下一个。
应该是这样的
<%
//Defining each cube
for (var x = 0; x < 9; x++) {
%>
<div class="parentCube">
<%
//Defining each cell in cube
for (var y = 0; y < 9; y++) {
console.log('x: ' + x + ' y: ' + y);
%>
<div class="childCube">
<%=
//Heres where im having trouble, how do you look up the 1d array index using the outlying for loop
matrix.indexOf(??);
%>
</div>
<%
}
%>
</div>
<%
}
%>
//should follow this equation
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8
[0,0], [1,0], [2,0], [0,1], [1,1], [2,1], [0,2], [1,2], [2,2]
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8
[3,0], [4,0], [5,0], [3,1], [4,1], [5,1], [3,2], [4,2], [5,2]
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8
[6,0], [7,0], [8,0], [6,1], [7,1], [8,1], [6,2], [7,2], [8,2]
3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7 3,8
[0,3], [1,3], [2,3], [0,4], [1,4], [2,4], [0,5], [1,5], [2,5]
// Or this structure of X/Y's
X
0,1,2,0,1,2,0,1,2, 3,4,5,3,4,5,3,4,5, 6,7,8,6,7,8,6,7,8,
0,1,2,0,1,2,0,1,2, 3,4,5,3,4,5,3,4,5, 6,7,8,6,7,8,6,7,8,
0,1,2,0,1,2,0,1,2, 3,4,5,3,4,5,3,4,5, 6,7,8,6,7,8,6,7,8,
Y
0,0,0,1,1,1,2,2,2, 0,0,0,1,1,1,2,2,2, 0,0,0,1,1,1,2,2,2,
3,3,3,4,4,4,5,5,5, 3,3,3,4,4,4,5,5,5, 3,3,3,4,4,4,5,5,5,
6,6,6,7,7,7,8,8,8, 6,6,6,7,7,7,8,8,8, 6,6,6,7,7,7,8,8,8,
我已经解出了 x 方程,(y%3 + (x*3)) % 9;我现在正在研究 y,因为它的包装方式有点困难。
【问题讨论】:
标签: javascript templates underscore.js