-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
182 lines (157 loc) · 5.25 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
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
179
180
181
const NB_ESSAIS_MAX = 6
const periode = 400 //ms
window.onload = function(event) {
confirmOptionsButton.innerHTML = "Jouer"
confirmOptionsButton.disabled = false
}
optionsButton.onclick = function(event) {
optionsDialog.showModal();
}
var volumeOn = true
var nbLettres = 8
optionsForm.onsubmit = function(event) {
if (optionsForm.checkValidity()) {
volumeOn = volumeCheckbox.checked
nbLettres = nbLettresInput.valueAsNumber
optionsDialog.close()
if (!nbEssais) nouvellePartie()
} else {
optionsForm.reportValidity()
}
}
var motATrouver
var listeATrouver
var lettresTrouvees
var nbEssais = 0
function nouvellePartie() {
nbEssais = 0
motATrouver = motsATrouver[nbLettres][Math.floor(motsATrouver[nbLettres].length * Math.random())]
motATrouver = motATrouver.normalize("NFD").replace(/\p{Diacritic}/gu, "")
listeATrouver = Array.from(motATrouver)
lettresTrouvees = [listeATrouver[0]]
grille.innerHTML = ""
nouvelEssai()
}
var form
var lettresATrouver
var nbLettresBienPlacees
function nouvelEssai() {
nbEssais++
form = document.createElement("form")
form.action = "#"
lettresATrouver = Array.from(listeATrouver)
nbLettresBienPlacees = 0
listeATrouver.forEach((lettre, indice) => {
var input = document.createElement("input")
input.type = "text"
input.required = true
input.minLength = 1
input.maxLength = 1
input.size = 1
input.pattern = "[a-z]"
input.placeholder = "."
input.classList.add("lettre")
input.onfocus = onfocus
input.onkeydown = onkeydown
input.oninput = oninput
input.onkeyup = onkeyup
input.tabIndex = indice + 1
if (lettresTrouvees[indice]) input.value = lettresTrouvees[indice]
form.appendChild(input)
})
grille.appendChild(form)
if (nbEssais <= NB_ESSAIS_MAX) {
form.onsubmit = onsubmit
form.children[0].disabled = true
form.children[1].focus()
} else {
listeATrouver.forEach((lettre, indice) => {
var input = form.children[indice]
input.disabled = true
input.value = lettre
})
sonPerdu.onended = function() {
if (confirm(`Perdu ! Le mot à trouver était : ${motATrouver.toUpperCase()}.\nRéessayer ?`)) nouvellePartie()
else nbEssais = 0
}
if (volumeOn) play(sonPerdu)
else sonPerdu.onended()
}
}
function onfocus() {
this.select()
}
function onkeydown(event) {
if (event.key == "Backspace" && this.value == "") {
this.previousSibling?.focus()
}
}
function oninput() {
this.value = this.value.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase()
if (this.checkValidity()) {
this.nextSibling?.focus()
} else {
this.value = ""
}
}
function onkeyup(event) {
switch(event.key) {
case "Enter": form.onsubmit(); break
case "ArrowLeft": this.previousSibling?.focus(); break
case "ArrowRight": this.nextSibling?.focus(); break
case "Home": form.children[0].focus(); break
case "End": form.children[nbLettres-1].focus(); break
}
}
function play(son) {
son.currentTime = 0
son.play()
}
sonMotTrouve.onended = function(event) {
if (confirm("Bien joué !\nUne nouvelle partie ?")) nouvellePartie()
}
function onsubmit(event) {
if (this.checkValidity()) {
if (motsAutorises.includes(Array.from(form.children).map((input) => input.value).join(""))) {
var inputsNonValides = Array.from(form.children)
listeATrouver.forEach((lettre, indice) => {
var input = this.children[indice]
if (input.value == lettre) {
if (!lettresTrouvees[indice]) lettresTrouvees[indice] = lettre
delete(lettresATrouver[indice])
delete(inputsNonValides[indice])
nbLettresBienPlacees++
setTimeout(() => {
input.classList.add("bien-placee")
if (volumeOn) play(sonLettreBienPlacee)
}, periode * indice)
}
input.disabled = true
})
inputsNonValides.forEach((input, indice) => {
var index = lettresATrouver.indexOf(input.value)
if (index >= 0) {
delete(lettresATrouver[index])
setTimeout(() => {
input.classList.add("mal-placee")
if (volumeOn) play(sonLettreMalPlacee)
}, periode * indice)
} else {
setTimeout(() => {if (volumeOn) play(sonLettreNonTrouvee)}, periode * indice)
}
})
setTimeout(() => {
if (nbLettresBienPlacees == nbLettres) {
if (volumeOn) play(sonMotTrouve)
else sonMotTrouve.onended()
} else nouvelEssai()
}, listeATrouver.length * periode)
} else {
for(input of form.children) input.disabled = true
if (volumeOn) play(sonLettreNonTrouvee)
nouvelEssai()
}
} else {
this.reportValidity()
}
}