js九宫格算法
Posted 逸_风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js九宫格算法相关的知识,希望对你有一定的参考价值。
var grid = [];
var RowCells = 3;
function initGrid() {
for (var nRow = 0; nRow < RowCells; nRow++) {
grid[nRow] = [];
for (var nCol = 0; nCol < RowCells; nCol++) {
grid[nRow][nCol] = -1;
}
}
}
function showGrid() {
for (var nRow = 0; nRow < RowCells; nRow++) {
for (var nCol = 0; nCol < RowCells; nCol++) {
document.write(grid[nRow][nCol] + ‘ ‘);
}
document.write(‘<br/>‘);
}
}
var position = {
nRow: 0,
nCol: 1
}
function fillData() {
for (var nData = 1; nData <= RowCells * RowCells; nData++) {
grid[position.nRow][position.nCol] = nData;
getNextPosition()
}
}
function getNextPosition() {
var newRow = (position.nRow - 1 + RowCells) % RowCells;
var newCol = (position.nCol - 1 + RowCells) % RowCells;
if (grid[newRow][newCol] == -1) {
position.nRow = newRow;
position.nCol = newCol;
}
else {
position.nRow = (position.nRow + 1) % RowCells;
}
}
initGrid();
fillData();
showGrid();
以上是关于js九宫格算法的主要内容,如果未能解决你的问题,请参考以下文章