This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (99 loc) · 3.18 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
// select elements in page
const stock = document.querySelector(".stock")
const selector = document.querySelector(".selector")
const resetButton = document.querySelector("#reset")
const doneButton = document.querySelector("#done")
const content = document.querySelector(".content")
const row = document.querySelector(".row")
let life = document.querySelector(".life")
const totalRows = 10
let allColors = []
let finalCode = []
let selectedColors = []
let rowsList = []
let rowCount = 0
stock.addEventListener("click", handleColorsClick)
resetButton.addEventListener("click", handleReset)
doneButton.addEventListener("click", handleDone)
// init
duplicateRow()
getColors()
createFinalCode()
life.textContent = totalRows
function duplicateRow () {
for (let i = 1; i < totalRows; i++) {
const newRow = row.cloneNode(true)
content.append(newRow)
}
rowsList = Array.from(content.children)
}
function getColors () {
Array.from(stock.children).map(child => {
allColors.push(child.dataset.color)
})
}
function createFinalCode () {
for (let i = 0; i < 4; i++) {
const randomNumber = Math.floor(Math.random() * allColors.length)
const randomColor = allColors.splice(randomNumber, 1)[0]
finalCode.push(randomColor)
}
console.log(finalCode)
}
// handle events
function changeSelectorColor () {
selectedColors.map((color, index) => {
selector.children[index].dataset.color = color
})
}
// handle colors click in stock
function handleColorsClick (event) {
if (selectedColors.length < 4 && event.target.dataset.color) {
const element = event.target
const clickedColor = element.dataset.color
element.style.display = "none"
selectedColors.push(clickedColor)
changeSelectorColor()
}
}
// reset colors in selector
function handleReset () {
selectedColors = []
Array.from(selector.children).map(child => {
child.dataset.color = null
})
Array.from(stock.children).map(child => {
child.style.display = null
})
}
// submit colors in game
function handleDone () {
if (selectedColors.length === 4 && rowCount < totalRows) {
const selectedRow = rowsList[rowCount]
const selectedRowDots = Array.from(selectedRow.querySelector(".dots").children)
let leftWitness = selectedRow.querySelectorAll(".witness")[0]
let rightWitness = selectedRow.querySelectorAll(".witness")[1]
let leftWitnessNumber = 0
let rightWitnessNumber = 0
selectedRowDots.map((dot, index) => {
dot.dataset.color = selectedColors[index]
})
selectedColors.map((color, index) => {
if (finalCode[index] === color) {
rightWitnessNumber++
} else {
if (finalCode.includes(color)) {
leftWitnessNumber++
}
}
})
if (selectedColors.toString() === finalCode.toString()) {
alert("You won !")
}
leftWitness.textContent = leftWitnessNumber
rightWitness.textContent = rightWitnessNumber
life.textContent = totalRows - rowCount - 1
handleReset()
rowCount++
}
}