Guest User

Untitled

a guest
Jan 21st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. const cl = console.log;
  2.  
  3. //console.clear();
  4.  
  5. class Game {
  6. constructor() {
  7. this.HEIGHT = 10;
  8. this.WIDTH = 10;
  9. this.control = document.getElementById('game');
  10. this.createBombs();
  11. this.createMatrix();
  12. this.control.addEventListener('click', this.onClick.bind(this));
  13. }
  14.  
  15. createBombs() {
  16. this.bombs = new Array(this.HEIGHT).fill(true).map(() => {
  17. return {
  18. x: Math.floor(Math.random() * this.HEIGHT),
  19. y: Math.floor(Math.random() * this.HEIGHT),
  20. }
  21. });
  22. }
  23.  
  24. onClick(e) {
  25. const {target: {dataset: {x, y}}} = e;
  26. this.checkCoords(parseInt(x), parseInt(y));
  27. }
  28.  
  29. cellsNear(x, y, fn) {
  30. let bombs = 0;
  31. for (let i = x - 1; i <= x + 1; i++) {
  32. for (let j = y - 1; j <= y + 1; j++) {
  33. if (x < 0 || y < 0 || x > this.HEIGHT || y > this.WIDTH) continue;
  34.  
  35. if (fn(i, j)) bombs++;
  36. }
  37. }
  38. return bombs;
  39. }
  40.  
  41. checkCoords(x, y) {
  42. if (this.isBomb(x, y)) alert('bomb');
  43. const cell = this.matrix[x][y];
  44. cell.open();
  45. const isEmpty = cell.isEmpty();
  46.  
  47. const emptyAround = this.checkAdjucent(cell, []);
  48. emptyAround.forEach(cell => cell.open());
  49. }
  50.  
  51. getEmptyCells(x, y, fn, acc) {
  52. let cells = [];
  53. for (let i = x - 1; i <= x + 1; i++) {
  54. for (let j = y - 1; j <= y + 1; j++) {
  55. if (x < 0 || y < 0 || x > this.HEIGHT || y > this.WIDTH) continue;
  56.  
  57. if (fn(i, j) && !acc.includes(this.matrix[i][j])) {
  58. const cell = this.matrix[i][j];
  59. acc.push(cell);
  60. cells.push(cell);
  61. }
  62. }
  63. }
  64. return cells;
  65. }
  66.  
  67. isEmptyCell(x, y) {
  68. if (x < 0 || y < 0 || x >= this.WIDTH || y >= this.HEIGHT) return false;
  69. return this.matrix[x][y].isEmpty();
  70. }
  71.  
  72. checkAdjucent({x, y}, acc) {
  73. const emptyArr = this.getArr(x, y, acc);
  74.  
  75.  
  76. return emptyArr.length
  77. ? emptyArr.reduce((a, node) => {
  78. const arr = this.checkAdjucent(node, acc);
  79. arr.forEach(i => {
  80. if (!acc.includes(i)) {
  81. a.push(i);
  82. }
  83. });
  84.  
  85. return a;
  86. }, acc)
  87. : acc
  88. }
  89.  
  90. getArr(x, y, acc) {
  91. return this.getEmptyCells(x, y, this.isEmptyCell.bind(this), acc);
  92. }
  93.  
  94.  
  95. isBomb(x, y) {
  96. return this.bombs.some(({x: bombX, y: bombY}) => {
  97. return x === bombX && y === bombY;
  98. })
  99. }
  100.  
  101. createMatrix() {
  102. this.matrix = [];
  103. for (let i = 0; i < this.HEIGHT; i++) {
  104. if (!this.matrix[i]) this.matrix.push([])
  105. for (let j = 0; j < this.WIDTH; j++) {
  106. const isBomb = this.isBomb(i, j);
  107. const bombsNear = this.cellsNear(i, j, this.isBomb.bind(this));
  108. const cell = new Cell(i, j, isBomb, bombsNear);
  109.  
  110. this.control.appendChild(cell.control);
  111. this.matrix[i].push(cell);
  112. }
  113. }
  114. }
  115. }
  116.  
  117. class Cell {
  118. constructor(x, y, isBomb, bombsNear) {
  119. this.x = x;
  120. this.y = y;
  121. this.bombsNear = bombsNear;
  122. this.control = this.createCell(x, y, isBomb, bombsNear);
  123. }
  124.  
  125. createCell(x, y, isBomb, bombsNear) {
  126. const cell = document.createElement('div');
  127. cell.classList.add('cell');
  128. cell.dataset.x = x;
  129. cell.dataset.y = y;
  130. cell.innerText = isBomb ? 'b' : bombsNear;
  131.  
  132.  
  133. return cell;
  134. }
  135.  
  136. isEmpty() {
  137. return this.bombsNear === 0;
  138. }
  139.  
  140. open() {
  141. this.control.style.background = 'blue';
  142. }
  143. }
  144.  
  145. new Game()
Add Comment
Please, Sign In to add comment