cell.js 785 B

12345678910111213141516171819202122232425262728293031
  1. /** Depends on globals: rule, w, h, mode. */
  2. function cell() {
  3. var d = pv.range(h).map(function() {
  4. return pv.range(w).map(function() { return 0; });
  5. }),
  6. r = pv.range(8).map(function(i) {
  7. return rule >> i & 1;
  8. });
  9. if (start == "point") {
  10. d[0][w >> 1] = 1;
  11. } else {
  12. for (var x = 0; x < w; x++) {
  13. d[0][x] = cell.random(x);
  14. }
  15. }
  16. for (var y = 1; y < h; y++) {
  17. var p = d[y - 1], c = d[y];
  18. for (var x = 0; x < w; x++) {
  19. c[x] = r[p[x - 1] << 2 | p[x] << 1 | p[x + 1]];
  20. }
  21. }
  22. return d;
  23. }
  24. cell.$random = {};
  25. /** Caches random output to make exploration deterministic. */
  26. cell.random = function(i) {
  27. return i in cell.$random ? cell.$random[i]
  28. : (cell.$random[i] = Math.random() < .5 ? 0 : 1);
  29. };