Advertisement
jcomeau_ictx

conway's life fixed for iPod Touch

Aug 5th, 2013
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html
  3. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6. <head>
  7. <title>Conway's Game of Life</title>
  8. <style type="text/css">
  9. body, table, tr, td {margin: 0px; padding: 0px;}
  10. table {border-padding: 0px; border-collapse: collapse;}
  11. </style>
  12. <script type="text/javascript">
  13. //<![CDATA[
  14. // namespace this so as not to conflict with anything else
  15. if (!com) var com = {};
  16. if (!com.jcomeau) com.jcomeau = {};
  17. com.jcomeau.conwaylife = {
  18. // assumes pixels of 1:1 aspect ratio
  19. CELLSIZE: /\biPod\b/.test(navigator.userAgent) ? 50 : 10,
  20. // fewer cells for iPod because it's slow and js halts after 10s, see
  21. // https://discussions.apple.com/thread/2298038
  22. CELLSPACING: 0,
  23. CHANCE: .09, // chance of random cell being alive at start
  24. TICKTIME: 200, // time in milliseconds between ticks (plus processing time)
  25. TIMER: null, // set when setTimeout called
  26. ROWS: 0,
  27. COLUMNS: 0,
  28. DEBUGGING: false,
  29. KEYCODE: {r: "r".charCodeAt(0), s: "s".charCodeAt(0), c: "c".charCodeAt(0)}
  30. };
  31. com.jcomeau.conwaylife.getParameters = function() {
  32. var querystring = window.location.search;
  33. var regex = /([^?=&]+)=([^?=&]*)/g;
  34. var match;
  35. while (match = regex.exec(querystring)) {
  36. console.log("setting parameter " + match[1] + " to " + match[2]);
  37. com.jcomeau.conwaylife[match[1]] = match[2]; // match[0] is complete match
  38. }
  39. }
  40. com.jcomeau.conwaylife.start = function() {
  41. var life = com.jcomeau.conwaylife;
  42. life.getParameters();
  43. var table = document.createElement("table");
  44. document.body.appendChild(table);
  45. var i, j, tr, td, cellsize = life.CELLSIZE + life.CELLSPACING;
  46. life.COLUMNS = Math.max(1, Math.floor(window.innerWidth / cellsize));
  47. life.ROWS = Math.max(1, Math.floor(window.innerHeight / cellsize));
  48. for (i = 0; i < life.ROWS; i++) {
  49. tr = document.createElement("tr");
  50. table.appendChild(tr);
  51. for (j = 0; j < life.COLUMNS; j++) {
  52. td = document.createElement("td");
  53. td.id = "(" + j + "," + i + ")";
  54. td.width = td.height = life.CELLSIZE;
  55. life.setCell(td, Math.random() < life.CHANCE ? "alive" : "dead");
  56. td.onclick = life.toggle;
  57. tr.appendChild(td);
  58. }
  59. }
  60. document.onkeypress = life.keyHandler;
  61. if (life.CHANCE) life.restart();
  62. };
  63. com.jcomeau.conwaylife.keyHandler = function(event) {
  64. var life = com.jcomeau.conwaylife;
  65. var charCode = event.charCode || event.keyCode;
  66. if (charCode == life.KEYCODE["r"]) life.restart();
  67. else if (charCode == life.KEYCODE["s"]) life.freeze();
  68. else if (charCode == life.KEYCODE["c"]) life.clear();
  69. };
  70. com.jcomeau.conwaylife.recalc = function(cell) {
  71. // set this cell's state; don't update its representation until phase 2
  72. var life = com.jcomeau.conwaylife;
  73. var surroundedBy = life.liveNeighbors(cell)
  74. if (surroundedBy < 2 || surroundedBy > 3) // rules 1 and 3
  75. cell.className = "dead";
  76. else if (surroundedBy == 3) // rule 4; rule 2 is covered by default
  77. cell.className = "alive";
  78. };
  79. com.jcomeau.conwaylife.liveNeighbors = function(cell) {
  80. // must use colors, not className, to determine state
  81. var life = com.jcomeau.conwaylife;
  82. var column = parseInt(cell.id.substring(1, cell.id.indexOf(",")));
  83. var row = parseInt(cell.id.substring(cell.id.indexOf(",") + 1,
  84. cell.id.length - 1));
  85. var alive = -(cell.style.backgroundColor == "green");
  86. var i, j, r, c, td;
  87. if (life.DEBUGGING && alive) console.log("checking cell at column " + cell.id);
  88. for (i = -1; i < 2; i++) {
  89. r = life.modulo(row + i, life.ROWS);
  90. for (j = -1; j < 2; j++) {
  91. c = life.modulo(column + j, life.COLUMNS);
  92. td = document.getElementById("(" + c + "," + r + ")");
  93. alive += td.style.backgroundColor == "green";
  94. }
  95. }
  96. return alive;
  97. };
  98. com.jcomeau.conwaylife.modulo = function(number, divisor) {
  99. // fix javascript modulo to work like Python's
  100. return ((number % divisor) + divisor) % divisor;
  101. };
  102. com.jcomeau.conwaylife.update = function(cell) {
  103. // based on the decisions made in phase 1, update this cell's state
  104. cell.style.backgroundColor = cell.className == "alive" ? "green" : "white";
  105. };
  106. com.jcomeau.conwaylife.refresh = function() {
  107. var life = com.jcomeau.conwaylife;
  108. var cells = document.getElementsByTagName("td");
  109. var i;
  110. for (i = 0; i < cells.length; i++) { // phase 1
  111. life.recalc(cells[i]);
  112. }
  113. for (i = 0; i < cells.length; i++) { // phase 2
  114. life.update(cells[i]);
  115. }
  116. life.restart();
  117. };
  118. com.jcomeau.conwaylife.restart = function() {
  119. var life = com.jcomeau.conwaylife;
  120. life.TIMER = setTimeout(life.refresh, life.TICKTIME);
  121. };
  122. com.jcomeau.conwaylife.freeze = function() {
  123. clearTimeout(com.jcomeau.conwaylife.TIMER);
  124. };
  125. com.jcomeau.conwaylife.toggle = function(cell) {
  126. if (!(cell instanceof HTMLElement)) cell = this; // called by onclick handler
  127. var state = cell.className;
  128. com.jcomeau.conwaylife.setCell(cell, state == "alive" ? "dead" : "alive");
  129. };
  130. com.jcomeau.conwaylife.setCell = function(cell, state) {
  131. cell.className = state;
  132. cell.style.backgroundColor = (state == "alive" ? "green" : "white");
  133. };
  134. com.jcomeau.conwaylife.clear = function() {
  135. var life = com.jcomeau.conwaylife;
  136. var cells = document.getElementsByTagName("td");
  137. var i;
  138. for (i = 0; i < cells.length; i++) {
  139. life.setCell(cells[i], "dead");
  140. }
  141. };
  142. window.onload = com.jcomeau.conwaylife.start;
  143. //]]>
  144. </script>
  145. </head>
  146. <body>
  147. </body>
  148. </html>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement