-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (104 loc) · 3.28 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
let wordToFind = "Dictionnaire";
let arrayWordToFind = wordToFind.split("");
let word = document.getElementById("word").value;
let arrayWord = word.split("");
let wellPlaced = [];
let notInWord = [];
let missplaced = [];
let result = {};
function firstLetter() {
document.getElementById("word").value = arrayWordToFind[0];
}
firstLetter();
function numbersOfLetters() {
document.getElementById("letters").innerHTML = arrayWordToFind.length;
}
numbersOfLetters();
function tryWord(word, wordToFind) {
// Réinitialisation des tableaux
wellPlaced = [];
missplaced = [];
notInWord = [];
wordToFind = "Dictionnaire";
// arrayWordToFind = wordToFind.split("")
// afficher de la longueur
document.getElementById("letters").innerHTML = arrayWordToFind.length;
// Afficher la première lettre
document.getElementById("word").value = arrayWordToFind[0];
// Conversion en minuscules
word = word.toLowerCase();
wordToFind = wordToFind.toLowerCase();
// Appel des différentes fonctions de vérification
checkWin(word, wordToFind);
checkWellPlaced(word, wordToFind);
checkMissPlaced(word, wordToFind);
checkNotInWord(word, wordToFind);
// Mise à jour de l'affichage
updateDisplay();
// Retour du résultat
return {
wellPlaced: wellPlaced,
missplaced: missplaced,
notInWord: notInWord
};
}
function checkWin(word, wordToFind) {
if (word === wordToFind) {
document.getElementById("win").innerText = "Vous avez gagné !";
return true;
}
return false;
}
function checkWellPlaced(word, wordToFind) {
wellPlaced = []; // Réinitialisation
const wordArray = word.split('');
for (let i = 0; i < wordArray.length; i++) {
if (wordArray[i] === arrayWordToFind[i]) {
wellPlaced.push(wordArray[i]);
}
}
return wellPlaced;
}
function checkMissPlaced(word, wordToFind) {
missplaced = []; // Réinitialisation
const arrayWordToFind = wordToFind.split('');
const wordArray = word.split('');
// Créer une copie modifiable de baseArray
let remainingBase = [...arrayWordToFind];
for (let i = 0; i < wordArray.length; i++) {
// Si la lettre n'est pas bien placée
if (wordArray[i] !== arrayWordToFind[i]) {
// Chercher si la lettre existe ailleurs
const index = remainingBase.indexOf(wordArray[i]);
if (index !== -1) {
missplaced.push(wordArray[i]);
// Marquer cette lettre comme utilisée
remainingBase[index] = null;
}
}
}
return missplaced;
}
function checkNotInWord(word, wordToFind) {
notInWord = []; // Réinitialisation
for (const char of arrayWord) {
if (!arrayWordToFind.includes(char)) {
notInWord.push(char);
}
}
return notInWord;
}
function updateDisplay() {
document.getElementById("well").innerText = "Bien placé: " + wellPlaced.join(", ");
document.getElementById("miss").innerText = "Mal placé: " + missplaced.join(", ");
document.getElementById("not").innerText = "Pas dans le mot: " + notInWord.join(", ");
}
function guess() {
// Mettre à jour word et arrayWord avant d'appeler tryWord
word = document.getElementById("word").value.toLowerCase();
arrayWord = word.split("");
tryWord(word, wordToFind);
document.getElementById("word").value = "";
document.getElementById("try").innerText = word;
firstLetter()
}