Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const cl = console.log;
- //console.clear();
- class Game {
- constructor() {
- this.HEIGHT = 10;
- this.WIDTH = 10;
- this.control = document.getElementById('game');
- this.createBombs();
- this.createMatrix();
- this.control.addEventListener('click', this.onClick.bind(this));
- }
- createBombs() {
- this.bombs = new Array(this.HEIGHT).fill(true).map(() => {
- return {
- x: Math.floor(Math.random() * this.HEIGHT),
- y: Math.floor(Math.random() * this.HEIGHT),
- }
- });
- }
- onClick(e) {
- const {target: {dataset: {x, y}}} = e;
- this.checkCoords(parseInt(x), parseInt(y));
- }
- cellsNear(x, y, fn) {
- let bombs = 0;
- for (let i = x - 1; i <= x + 1; i++) {
- for (let j = y - 1; j <= y + 1; j++) {
- if (x < 0 || y < 0 || x > this.HEIGHT || y > this.WIDTH) continue;
- if (fn(i, j)) bombs++;
- }
- }
- return bombs;
- }
- checkCoords(x, y) {
- if (this.isBomb(x, y)) alert('bomb');
- const cell = this.matrix[x][y];
- cell.open();
- const isEmpty = cell.isEmpty();
- const emptyAround = this.checkAdjucent(cell, []);
- emptyAround.forEach(cell => cell.open());
- }
- getEmptyCells(x, y, fn, acc) {
- let cells = [];
- for (let i = x - 1; i <= x + 1; i++) {
- for (let j = y - 1; j <= y + 1; j++) {
- if (x < 0 || y < 0 || x > this.HEIGHT || y > this.WIDTH) continue;
- if (fn(i, j) && !acc.includes(this.matrix[i][j])) {
- const cell = this.matrix[i][j];
- acc.push(cell);
- cells.push(cell);
- }
- }
- }
- return cells;
- }
- isEmptyCell(x, y) {
- if (x < 0 || y < 0 || x >= this.WIDTH || y >= this.HEIGHT) return false;
- return this.matrix[x][y].isEmpty();
- }
- checkAdjucent({x, y}, acc) {
- const emptyArr = this.getArr(x, y, acc);
- return emptyArr.length
- ? emptyArr.reduce((a, node) => {
- const arr = this.checkAdjucent(node, acc);
- arr.forEach(i => {
- if (!acc.includes(i)) {
- a.push(i);
- }
- });
- return a;
- }, acc)
- : acc
- }
- getArr(x, y, acc) {
- return this.getEmptyCells(x, y, this.isEmptyCell.bind(this), acc);
- }
- isBomb(x, y) {
- return this.bombs.some(({x: bombX, y: bombY}) => {
- return x === bombX && y === bombY;
- })
- }
- createMatrix() {
- this.matrix = [];
- for (let i = 0; i < this.HEIGHT; i++) {
- if (!this.matrix[i]) this.matrix.push([])
- for (let j = 0; j < this.WIDTH; j++) {
- const isBomb = this.isBomb(i, j);
- const bombsNear = this.cellsNear(i, j, this.isBomb.bind(this));
- const cell = new Cell(i, j, isBomb, bombsNear);
- this.control.appendChild(cell.control);
- this.matrix[i].push(cell);
- }
- }
- }
- }
- class Cell {
- constructor(x, y, isBomb, bombsNear) {
- this.x = x;
- this.y = y;
- this.bombsNear = bombsNear;
- this.control = this.createCell(x, y, isBomb, bombsNear);
- }
- createCell(x, y, isBomb, bombsNear) {
- const cell = document.createElement('div');
- cell.classList.add('cell');
- cell.dataset.x = x;
- cell.dataset.y = y;
- cell.innerText = isBomb ? 'b' : bombsNear;
- return cell;
- }
- isEmpty() {
- return this.bombsNear === 0;
- }
- open() {
- this.control.style.background = 'blue';
- }
- }
- new Game()
Add Comment
Please, Sign In to add comment