-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
122 lines (90 loc) · 3.55 KB
/
script.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
const SET_ALL_ONE_BTN = document.getElementById('set-all-one-btn')
const SET_ALL_ZERO_BTN = document.getElementById('set-all-zero-btn')
const SET_RANDOM_BTN = document.getElementById('set-random-btn')
const CELLS = document.querySelectorAll('.cell')
let previousFlippedCell = null
// Setting value function
function flipCell(cell) {
const value = getValue(cell)
if (previousFlippedCell !== cell) {
if (previousKeyContainingCell && previousFlippedCell) {
// Flipping previous flipped Cell
const previousCellValue = getValue(previousFlippedCell)
if (previousCellValue === 1) setZero(previousFlippedCell)
else/**********************/ setOne(previousFlippedCell)
unflippedAsHint(previousFlippedCell)
}
if (previousKeyContainingCell) {
previousFlippedCell = cell
flippedAsHint(cell)
}
}
else {
previousFlippedCell = null
unflippedAsHint(cell)
}
// Flipping current clicked Cell
if (value === 1) setZero(cell)
else/**********/ setOne(cell)
}
function setAllOne() {
CELLS.forEach(setOne)
}
function setAllZero() {
CELLS.forEach(setZero)
}
function setRandom() {
for (i = 0; i < 10; i++) {
setTimeout(() => {
CELLS.forEach(cell => {
if (Math.random() > 0.4) setOne(cell)
else/******************/ setZero(cell)
})
}, i * 20)
}
}
// ------------------------------------------------------------------------------------
const setOne/****/ = cell => cell.setAttribute('data-value', '1')
const setZero/***/ = cell => cell.setAttribute('data-value', '0')
const getValue/**/ = cell => parseInt(cell.getAttribute('data-value'))
function flippedAsHint(cell) {
if (cell === previousKeyContainingCell) cell.setAttribute('title', 'Contain Key & also Flipped')
else/*********************************/ cell.setAttribute('title', 'Flipped')
cell.setAttribute('data-flipped-as-hint', '')
}
function unflippedAsHint(cell) {
if (cell === previousKeyContainingCell) cell.setAttribute('title', 'Contain Key')
else/*********************************/ cell.setAttribute('title', '')
cell.removeAttribute('data-flipped-as-hint')
}
// ------------------------------------------------------------------------------------
// KEY functions
let previousKeyContainingCell = null
function setKey(cell) {
if (previousKeyContainingCell)
UNSET_KEY(previousKeyContainingCell)
if (cell === previousKeyContainingCell) {
UNSET_KEY(cell)
previousKeyContainingCell = null
if (previousFlippedCell) { // Here we also have to remove flipped properties
unflippedAsHint(previousFlippedCell)
previousFlippedCell = null
}
SET_ALL_ONE_BTN.disabled = SET_ALL_ZERO_BTN.disabled = SET_RANDOM_BTN.disabled = false
} else {
SET_KEY(cell)
previousKeyContainingCell = cell
SET_ALL_ONE_BTN.disabled = SET_ALL_ZERO_BTN.disabled = SET_RANDOM_BTN.disabled = true
}
}
// ----------------------------------------------
function SET_KEY(cell) {
if (cell === previousFlippedCell) cell.setAttribute('title', 'Contain Key & also Flipped')
else/***************************/ cell.setAttribute('title', 'Contain Key')
cell.setAttribute('data-contain-key', '')
}
function UNSET_KEY(cell) {
if (cell === previousFlippedCell) cell.setAttribute('title', 'Flipped')
else/***************************/ cell.setAttribute('title', '')
cell.removeAttribute('data-contain-key')
}