Last active
December 10, 2015 12:28
-
-
Save swvitaliy/4434891 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.L = (function () { | |
var u = (function() { | |
return { | |
/* | |
var obj = { a: 1 }, defs = { a: 2, b:3 }; | |
console.log(u.defaults(obj, defs));*/ | |
defaults: function(obj, defs) { | |
var i; for(i in defs) { if (defs.hasOwnProperty(i)) { if (!obj.hasOwnProperty(i)) { obj[i] = defs[i] } } } | |
return obj; | |
}, | |
/* | |
var arr = [1,2,3]; | |
console.log(u.fillArray(arr, 10, 'q')); */ | |
fillArray: function(arr, count, val) { | |
var i = count; for (;i; --i) arr.push(val); | |
return arr; | |
} | |
}; | |
})(); | |
var map = { | |
/* console.log(map.setMargins(strMap.split('\n'), { left: 5, right:5, top:5, bottom:5 })); */ | |
setMargins: function(/* array of string */map, /* array of numbers (default [0,0,0,0]) */ margins) { | |
if (!map || !map.length) { | |
return null; | |
} | |
u.defaults(margins, { | |
top:0, right:0,bottom:0, left:0 | |
}); | |
var mapWid = map[0].length, len = (margins.left) + (margins.right) + mapWid; | |
var emptyStr = u.fillArray([], len, '.').join(''), | |
emptyTop = u.fillArray([], margins.top, emptyStr) | |
emptyBottom = u.fillArray([], margins.bottom, emptyStr), | |
emptyLeft = u.fillArray([], margins.left, '.').join(''), | |
emptyRight = u.fillArray([], margins.right, '.').join(''); | |
var i; for (i = 0;i < map.length; ++i) { map[i] = emptyLeft.concat(map[i].concat(emptyRight)); } | |
return emptyTop.concat(map.concat(emptyBottom)); | |
}, | |
/* | |
пересчитываем новое состояние карты. | |
"." - пустой символ "0" - ячейка */ | |
next: function(map, torusSurface) { | |
var lineWidth = map[0].length; | |
var neighbors = torusSurface ? | |
function (i, j) { | |
var decI = i === 0 ? map.length - 1 : i - 1, | |
incI = i === (map.length - 1) ? 0 : i + 1, | |
decJ = j === 0 ? lineWidth - 1 : j - 1, | |
incJ = j === (lineWidth - 1) ? 0 : j + 1; | |
function forLine(line, middle) { | |
var count = 0; | |
count += line.charAt(decJ) === '0'; | |
if (!middle) { | |
count += line.charAt(j) === '0'; | |
} | |
count += line.charAt(incJ) === '0'; | |
return count; | |
} | |
var line, count = 0; | |
count += forLine(map[decI], false); | |
count += forLine(map[i], true); | |
count += forLine(map[incI], false); | |
return count; | |
} | |
: | |
function (i, j) { | |
function forLine(line, middle) { | |
var count = 0; | |
if (j > 0) { | |
count += line.charAt(j-1) === '0'; | |
} | |
if (!middle) { | |
count += line.charAt(j) === '0'; | |
} | |
if (j < (lineWidth - 1)) { | |
count += line.charAt(j+1) === '0'; | |
} | |
return count; | |
} | |
var line, count = 0; | |
if (i > 0) { | |
count += forLine(map[i-1], false); | |
} | |
count += forLine(map[i], true); | |
if (i < (map.length - 1)) { | |
count += forLine(map[i+1], false); | |
} | |
return count; | |
}; | |
var i,j, line, char, nei, newLine, changes = 0; | |
for (i = 0; i<map.length;++i) { | |
line = map[i]; | |
newLine = []; | |
for(j = 0; j<lineWidth; ++j) { | |
char = line.charAt(j); | |
nei = neighbors(i, j); | |
if (char === '0') { | |
if (nei < 2 || nei > 3) { | |
char = '.'; | |
changes += 1; | |
} | |
} else { | |
if (nei === 3) { | |
char = '0'; | |
changes += 1; | |
} | |
} | |
newLine.push(char); | |
} | |
map[i] = newLine.join(''); | |
} | |
return {map: map, changes: changes}; | |
}, | |
/* | |
var c = document.getElementById('c'); | |
map.render(c.getContext("2d"), strMap.split('\n'), {});*/ | |
render: function(context, map, config) { | |
if (!map || !map.length) { | |
return false; | |
} | |
u.defaults(config, { | |
cellWidth:10, cellHeight:10 | |
}); | |
var i,j, lineWidth = map[0].length, line, char, x = 0, y = 0; | |
// закрашиваем область отрисовки бекграундом | |
context.beginPath(); | |
context.rect(0, 0, config.cellWidth * lineWidth, config.cellHeight * map.length); | |
context.fillStyle = 'white'; | |
context.fill(); | |
for (i = 0; i<map.length;++i) { | |
line = map[i]; | |
for(j = 0; j<lineWidth; ++j) { | |
char = line.charAt(j); | |
if (char === '0') { | |
context.beginPath(); | |
context.rect(x, y, config.cellWidth, config.cellHeight); | |
context.fillStyle = 'yellow'; | |
context.fill(); | |
} | |
x += config.cellWidth; | |
} | |
x = 0; | |
y += config.cellHeight; | |
} | |
return true; | |
} | |
}; | |
var stop = false; | |
function start(context, arMap, renderConfig, delay) { | |
renderConfig = renderConfig || {}; | |
var obj; | |
function loop() { | |
setTimeout(function() { | |
map.render(context, arMap, renderConfig); | |
obj = map.next(arMap, true); | |
arMap = obj.map; | |
if (obj.changes === 0) { | |
stop = true; | |
} | |
if (!stop) { | |
loop(); | |
} | |
}, delay); | |
} | |
loop(); | |
} | |
var strMap = [ | |
'...00........\n'+ | |
'....000......\n'+ | |
'..0....0.....\n'+ | |
'.0.0000.0....\n'+ | |
'.0.0....0..0.\n'+ | |
'00.000..0.0.0\n'+ | |
'.0.0....0..0.\n'+ | |
'.0.0000.0....\n'+ | |
'..0....0.....\n'+ | |
'....000......\n'+ | |
'...00........' | |
// добавьте сюда свою карту | |
// и измените индекс карты тремя строчками ниже | |
]; | |
var c = document.getElementById('c'); | |
start(c.getContext("2d"), strMap[0].split('\n'), {}, 500); | |
// | |
// Usage: | |
// 1. вставьте на страницу элемент | |
// <canvas id="c" width="1000" height="1000"></canvas> | |
// 2. запустите скрипт. | |
// 3. для оставнова выполните L.stopGame() | |
// | |
return { | |
stopGame: function() { | |
stop = true; | |
} | |
}; | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment