首页 > 编程语言 > 详细

js九宫格算法

时间:2019-01-18 22:57:43      阅读:224      评论:0      收藏:0      [点我收藏+]
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九宫格算法

原文:https://www.cnblogs.com/zxk5625/p/10290029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!