1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| new CellLife({ width: 100, height: 100, initialNumber: 500, cycleTime: 200, box: document.getElementById('root') });
class CellLife { constructor(options) { const { width, height, cycleTime, initialNumber, box} = options; this.width = width; this.height = height; this.cycleTime = cycleTime; this.initialNumber = initialNumber; this.box = box;
this.life = this.createCellByRandom(); this.createCheckerboard(); } tick (callback) { var newLife = this.updateCell(); callback(newLife); if (newLife.length === 0) { callback([]); } }
updateCell () { const neighbor = this.findNeighbor(this.life); const frequency = this.countFrequency(neighbor, this.life); const newLife = this.filterCountIsThree(frequency); const countIsTwo = this.filterCountIsTwo(frequency); const nowlife = this.mergeNewLifeAndSurvive(newLife, countIsTwo, this.life); this.life = nowlife; return nowlife; }
createCellByRandom () { const life = new Set(); while (life.size < this.initialNumber) { life.add({ x: Math.floor(Math.random() * this.width), y: Math.floor(Math.random() * this.height) }); } return [...life]; }
findNeighbor (life) { const allNeighbor = []; life.forEach(cell => allNeighbor.push(...this.neighborCell(cell.x, cell.y))); return allNeighbor; }
neighborCell (self_x, self_y) { const neighbor = []; const that = this; [-1, 0, 1].forEach(function(i) { [-1, 0, 1].forEach(function(j) { const x = self_x + i; const y = self_y + j; if (that.isValidCell(x, y, self_x, self_y)) { neighbor.push({ x, y }); } }); }); return neighbor; }
isValidCell (x, y, self_x, self_y) { return x >= 0 && y >= 0 && x < this.width && y < this.height && (self_x !== x || self_y !== y); }
countFrequency (neighbor) { var mapCell = {}; const frequency = []; var len = neighbor.length; for (var i = 0; i < len; i++) { var ele = neighbor[i]; var sameEle = mapCell[ele.x+''+ele.y]; if (sameEle) { sameEle.count += 1; } else { sameEle = { x: ele.x, y: ele.y, count: 1 }; mapCell[ele.x+''+ele.y] = sameEle; frequency.push(sameEle); } } mapCell = null; return frequency; }
filterCountIsThree (frequency) { return frequency.filter(cell => cell.count === 3); }
filterCountIsTwo (frequency) { return frequency.filter(cell => cell.count === 2); }
mergeNewLifeAndSurvive (newLife, countIsTwo, life) { var len, ele, mapCell = {}, survive = [];
len = life.length; for (var i = 0; i < len; i++) { ele = life[i]; mapCell[ele.x+''+ele.y] = ele; }
len = countIsTwo.length; for (var i = 0; i < len; i++) { ele = countIsTwo[i]; if (mapCell[ele.x+''+ele.y]) { survive.push(ele); } }
return [...newLife, ...survive]; } createCheckerboard(){ var td = new Array(this.height+5).join( '<tr>'+(new Array(this.width+5)).join('<td></td>')+'</tr>' ); var table = '<table><tbody>'+td+'</tbody></table>'; this.box.innerHTML = table;
var table = this.box.querySelector('tbody'); var tds = table.querySelectorAll('td'); var trs = table.querySelectorAll('tr');
clearInterval(this.timer); this.timer = setInterval(()=> { this.tick(life =>{ tds.forEach((ele, i)=>{ ele.className = ''; }); for (var i = 0, len = life.length; i < len; i++) { var cell = life[i]; trs[cell.y+2].children[cell.x+2].className = 'life'; } if (life.length === 0) { clearInterval(this.timer); } }); }, this.cycleTime); }
destroy() { this.box.innerHTML = ''; clearInterval(this.timer); } }
|