-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboard.js
79 lines (68 loc) · 2.31 KB
/
board.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Column } from './column.js'
import { GAME_PARAMS } from './gameparams.js'
export function Board (playerName, gameBoard) {
this.element = document.getElementById(playerName + '-board')
this.wrapper = document.getElementById(playerName + '-zone')
this.columns = [[], [], [], [], [], [], [], [], []]
this.column = new Column(this)
this.nextColumn = new Column(this)
this.gameBoard = gameBoard
}
Board.prototype.drawData = function () {
const levelWrap = this.wrapper.querySelector('.level span')
levelWrap.innerText = this.gameBoard.level
const pointsWrap = this.wrapper.querySelector('.points span')
pointsWrap.innerText = this.gameBoard.points
const blocksWrap = this.wrapper.querySelector('.blocks span')
blocksWrap.innerText = this.gameBoard.savedBlocks
}
Board.prototype.drawBoard = function () {
this.clearBoard()
const cells = [
this.element.querySelector(`.row${this.column.y} .col${this.column.x}`),
this.element.querySelector(`.row${this.column.y + 1} .col${this.column.x}`),
this.element.querySelector(`.row${this.column.y + 2} .col${this.column.x}`)
]
cells.forEach((cell, i) => {
cell?.classList.add(GAME_PARAMS.colors[this.column.blocks[i].type])
})
this.columns.forEach((col, i) => {
col.forEach((cell, j) => {
const block = this.element.querySelector(`.row${j} .col${i}`)
block?.classList.add(GAME_PARAMS.colors[this.columns[i][j].type])
})
})
this.setSpecialToErasable()
}
Board.prototype.clearBoard = function () {
const cells = this.element.querySelectorAll('.cell')
cells.forEach(function (cell) {
cell.classList.remove('deleted', ...Object.values(GAME_PARAMS.colors))
})
}
Board.prototype.deleteBlocks = function () {
this.columns = this.columns.map(column => {
return column.filter(block => { return !block.erasable })
})
}
Board.prototype.countErasableBlocks = function () {
let amount = 0
this.columns.forEach(function (column) {
column.forEach(function (cell) {
if (cell.erasable) {
amount++
}
})
})
return amount
}
Board.prototype.setSpecialToErasable = function () {
this.columns.forEach((col, i) => {
col.forEach((block, j) => {
if (block.erasable) {
const cell = this.element.querySelector(`.row${j} .col${i}`)
cell?.classList.add('deleted')
}
})
})
}