-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
178 lines (135 loc) · 4.96 KB
/
app.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const wordsForWordSearch = [
'Operator, BigInt, Number, Boolean, Events, StrictMode, Modules, Classes,'
]
const randomizedLetters = 'operatorbigintnumberbooleaneventsstrictmodemodulesclasses'.split('')
const getRandomLetter = () => {
const randomIndex = Math.floor(Math.random() * randomizedLetters.length)
return randomizedLetters[randomIndex]
}
class Board {
constructor(boardSizeX, boardSizeY) {
this.grid = Array(boardSizeY).fill(0).map(
(_, row) => Array(boardSizeX).fill(0).map(
(__, column) => ({
x: column,
y: row,
letter: getRandomLetter(),
isInMatch: false,
})
)
)
}
getRow(index) {
return this.grid[index]
}
toString() {
return this.grid.map(
(row) => row.map(
({ letter }) => letter
).join(', ')
).join('\n')
}
forEach(callBack) {
this.grid.forEach(
(row) => {
row.forEach(
(cell) => {
callBack(cell)
}
)
}
)
}
}
document.addEventListener('DOMContentLoaded', () => {
const board = new Board(10, 10)
const gridRoot = document.querySelector("#grid")
let state = {
selectedCells: []
}
const makeSelection = (selection)=>{
state.selectedCells.push(selection)
const selectedWord = state.selectedCells.map(
(elem)=> elem.textContent
).join('')
document.querySelector('#selection').textContent = selectedWord
}
const isNewSelection = (target) => {
if (state.selectedCells.length === 0) {
return true
}
// the last item in the stack is the most recent selection
const mostRecentSelection = state.selectedCells[state.selectedCells.length - 1]
if (target === mostRecentSelection) {
// the previous selection was repeated
return false
}
const distance = Math.abs(target.dataSet.x - mostRecentSelection.dataSet.x + target.dataSet.y - mostRecentSelection.dataSet.y)
if(distance !== 1){
return true
}
if(state.selectedCells.length === 1){
return false
}
// now we know that there is already a direction
const secondMostRecentSelection = state.selectedCells[state.selectedCells.length - 2]
// this selection is vertical if adjacent cells have the same x value
const isVertical = secondMostRecentSelection.dataSet.x === mostRecentSelection.dataSet.x
if(isVertical){
return Math.abs(target.dataSet.y - mostRecentSelection.dataSet.y) !== 1
}
// this selection is horizontal if adjacent cells have the same y value
const isHorizontal = secondMostRecentSelection.dataSet.y === mostRecentSelection.dataSet.y
if(isHorizontal){
return Math.abs(target.dataSet.x - mostRecentSelection.dataSet.x) !== 1
}
// we dont allow diagnal selections
return true
}
const handleNewSelection = (selectedElement) => {
state.selectedCells.forEach(
(cell) => {
cell.classList.remove('selected')
}
)
state.selectedCells = []
makeSelection(selectedElement)
selectedElement.classList.add('selected')
}
const extendSelectionWith = (nextElement) => {
makeSelection(nextElement)
nextElement.classList.add('selected')
}
gridRoot.addEventListener('click', (e) => {
console.log({ target: e.target.textContent, x: e.target.dataSet.x, y: e.target.dataSet.y })
if (isNewSelection(e.target)) {
console.log('handle new selecion')
// clear existing selections
handleNewSelection(e.target)
return
}
if (e.target !== state.selectedCells[state.selectedCells.length - 1]) {
extendSelectionWith(e.target)
}
})
if (document.createElement("template")) {
const template = document.querySelector('#cell');
if (!template) {
throw new Error('template not found')
}
board.forEach(
({ letter, x, y }) => {
const cell = template.content.cloneNode(true);
const cellText = cell.querySelector("#cell-text")
if (!cellText) {
throw new Error('not Found')
}
cellText.textContent = letter
cellText.dataSet = {}
cellText.dataSet.x = x
cellText.dataSet.y = y
gridRoot.appendChild(cellText)
}
)
}
})