Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. // Brains game implementation in javascript
  2.  
  3. // new Brains(el)
  4. // initialize Brains on a preferably empty div
  5. function Brains (el) {
  6.  
  7. // --- START: Board ---
  8. var Board = function (boardEl) {
  9. var board = [[]];
  10. var width = 0;
  11. var height = 0;
  12.  
  13. var completed = new CustomEvent(
  14. "board_completed",
  15. {
  16. detail: {
  17. element: boardEl
  18. },
  19. bubbles: false,
  20. cancelable: false
  21. }
  22. );
  23.  
  24. var isComplete = function () {
  25. for (var x = 0; x < width; x++) {
  26. for (var y = 0; y < height; y++) {
  27. if (board[x][y].value === " ") {
  28. return false;
  29. }
  30. }
  31. }
  32.  
  33. return true;
  34. };
  35.  
  36. var increase = function (button) {
  37. button.value = (button.value % 4) + 1;
  38. };
  39.  
  40. // click a button and increment its neighbours
  41. var increment = function (x, y) {
  42. return function () {
  43. var button = board[x][y];
  44.  
  45. // only when button hasn't been clicked
  46. if (button.value === " ") {
  47. button.value = "1";
  48.  
  49. // button to the left
  50. if (x > 0 && board[x - 1][y].value !== " ") {
  51. increase(board[x - 1][y]);
  52. }
  53.  
  54. // button to the right
  55. if (x < (width - 1) && board[x + 1][y].value !== " ") {
  56. increase(board[x + 1][y]);
  57. }
  58.  
  59. // button above
  60. if (y > 0 && board[x][y - 1].value !== " ") {
  61. increase(board[x][y - 1]);
  62. }
  63.  
  64. // button below
  65. if (y < (height - 1) && board[x][y + 1].value !== " ") {
  66. increase(board[x][y + 1]);
  67. }
  68.  
  69. if (isComplete()) {
  70. boardEl.dispatchEvent(completed);
  71. }
  72. }
  73. };
  74. };
  75.  
  76. return {
  77.  
  78. completed: completed,
  79.  
  80. getBoard: function () {
  81. return board;
  82. },
  83.  
  84. // call with w: width, h: height
  85. create: function (w, h) {
  86. width = w;
  87. height = h;
  88.  
  89. // init array
  90. board = new Array(width);
  91. for (var i = 0; i < width; i++) {
  92. board[i] = new Array(height);
  93. }
  94.  
  95. boardEl.setAttribute('class', 'board width-' + width + '-' + height);
  96.  
  97. // initialize the buttons
  98. for (var i = 0; i < height; i++) {
  99. for (var j = 0; j < width; j++) {
  100. board[j][i] = document.createElement('input');
  101. board[j][i].setAttribute('type', 'button');
  102. board[j][i].setAttribute('data-x', j);
  103. board[j][i].setAttribute('data-y', i);
  104. board[j][i].setAttribute('value', ' ');
  105. board[j][i].onclick = increment(j, i);
  106. boardEl.appendChild(board[j][i]);
  107. }
  108. }
  109. },
  110.  
  111. // reset board to original state
  112. reset: function () {
  113. for (var x = 0; x < width; x++) {
  114. for (var y = 0; y < height; y++) {
  115. board[x][y].value = " ";
  116. }
  117. }
  118.  
  119. boardEl.setAttribute('class', 'board width-' + width + '-' + height);
  120. },
  121.  
  122. // generate a random board
  123. new: function () {
  124. while (!isComplete()) {
  125. var x = Math.floor(Math.random() * width);
  126. var y = Math.floor(Math.random() * height);
  127.  
  128. increment(x, y)();
  129. }
  130. },
  131.  
  132. // is this board same as other board?
  133. sameAs: function (other) {
  134. var otherBoard = other.getBoard();
  135.  
  136. for (var x = 0; x < width; x++) {
  137. for (var y = 0; y < height; y++) {
  138. if (board[x][y].value !== otherBoard[x][y].value) {
  139. return false;
  140. }
  141. }
  142. }
  143.  
  144. return true;
  145. }
  146. }
  147. }
  148. // --- END: Board ---
  149.  
  150. var playEl = document.createElement('div');
  151. var originalEl = document.createElement('div');
  152.  
  153. var play = [[]];
  154. var original = [[]];
  155.  
  156. var markSuccessOrFailure = function (e) {
  157. var boardEl = e.detail.element;
  158.  
  159. if (play.sameAs(original)) {
  160. boardEl.setAttribute('class', 'success ' + boardEl.getAttribute('class'));
  161. }
  162. else {
  163. boardEl.setAttribute('class', 'failure ' + boardEl.getAttribute('class'));
  164. }
  165. };
  166.  
  167. return {
  168.  
  169. // create boards of specific size
  170. create: function (width, height) {
  171.  
  172. // create play board
  173. playEl = document.createElement('div');
  174. play = new Board(playEl);
  175. play.create(width, height);
  176. el.appendChild(playEl);
  177.  
  178. // create original board
  179. originalEl = document.createElement('div');
  180. original = new Board(originalEl);
  181. original.create(width, height);
  182. el.appendChild(originalEl);
  183.  
  184. playEl.addEventListener("board_completed", markSuccessOrFailure, false);
  185.  
  186. original.new();
  187. },
  188.  
  189. // create a new game
  190. new: function () {
  191. original.reset();
  192. play.reset();
  193. original.new();
  194. },
  195.  
  196. // reset the current game
  197. reset: function () {
  198. play.reset();
  199. }
  200. };
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement