码迷,mamicode.com
首页 > 编程语言 > 详细

js九宫格算法

时间:2019-01-18 23:14:40      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:lda   fill   write   lse   data   function   算法   on()   col   

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九宫格算法

标签:lda   fill   write   lse   data   function   算法   on()   col   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!