博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js九宫格算法
阅读量:5090 次
发布时间:2019-06-13

本文共 1122 字,大约阅读时间需要 3 分钟。

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('
'); } } 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();

转载于:https://www.cnblogs.com/zxk5625/p/10290029.html

你可能感兴趣的文章