-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
145 lines (129 loc) · 4.49 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
const tiles = document.querySelector(".tile-container");
const backspaceAndEnterRow = document.querySelector("#backspaceAndEnterRow");
const keyboardFirstRow = document.querySelector("#keyboardFirstRow");
const keyboardSecondRow = document.querySelector("#keyboardSecondRow");
const keyboardThirdRow = document.querySelector("#keyboardThirdRow");
const keysFirstRow = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"];
const keysSecondRow = ["A", "S", "D", "F", "G", "H", "J", "K", "L"];
const keysThirdRow = ["Z", "X", "C", "V", "B", "N", "M"];
const rows = 6;
const columns = 5;
let currentRow = 0;
let currentColumn = 0;
let letreco = "LETRA";
let letrecoMap = {};
for (let index = 0; index < letreco.length; index++) {
letrecoMap[letreco[index]] = index;
}
const guesses = [];
for (let rowIndex = 0; rowIndex < rows; rowIndex++) {
guesses[rowIndex] = new Array(columns);
const tileRow = document.createElement("div");
tileRow.setAttribute("id", "row" + rowIndex);
tileRow.setAttribute("class", "tile-row");
for (let columnIndex = 0; columnIndex < columns; columnIndex++) {
const tileColumn = document.createElement("div");
tileColumn.setAttribute("id", "row" + rowIndex + "column" + columnIndex);
tileColumn.setAttribute(
"class",
rowIndex === 0 ? "tile-column typing" : "tile-column disabled"
);
tileRow.append(tileColumn);
guesses[rowIndex][columnIndex] = "";
}
tiles.append(tileRow);
}
const checkGuess = () => {
const guess = guesses[currentRow].join("");
if (guess.length !== columns) {
return;
}
var currentColumns = document.querySelectorAll(".typing");
for (let index = 0; index < columns; index++) {
const letter = guess[index];
if (letrecoMap[letter] === undefined) {
currentColumns[index].classList.add("wrong")
} else {
if(letrecoMap[letter] === index) {
currentColumns[index].classList.add("right")
} else {
currentColumns[index].classList.add("displaced")
}
}
}
if(guess === letreco) {
window.alert("tu é demais, simplesmente o detetivao do entreterimento!")
return
} {
if(currentRow === rows -1) {
window.alert("Errrrrrou!")
} else {
moveToNextRow()
}
}
};
const moveToNextRow = () => {
var typingColumns = document.querySelectorAll(".typing")
for (let index = 0; index < typingColumns.length; index++) {
typingColumns[index].classList.remove("typing")
typingColumns[index].classList.add("disabled")
}
currentRow++
currentColumn=0
const currentRowEl = document.querySelector("#row"+currentRow)
var currentColumns = currentRowEl.querySelectorAll(".tile-column")
for (let index = 0; index < currentColumns.length; index++) {
currentColumns[index].classList.remove("disabled")
currentColumns[index].classList.add("typing")
}
}
const handleKeyboardOnClick = (key) => {
if (currentColumn === columns) {
return;
}
const currentTile = document.querySelector(
"#row" + currentRow + "column" + currentColumn
);
currentTile.textContent = key;
guesses[currentRow][currentColumn] = key;
currentColumn++;
};
const createKeyboardRow = (keys, keyboardRow) => {
keys.forEach((key) => {
var buttonElement = document.createElement("button");
buttonElement.textContent = key;
buttonElement.setAttribute("id", key);
buttonElement.addEventListener("click", () => handleKeyboardOnClick(key));
keyboardRow.append(buttonElement);
});
};
createKeyboardRow(keysFirstRow, keyboardFirstRow);
createKeyboardRow(keysSecondRow, keyboardSecondRow);
createKeyboardRow(keysThirdRow, keyboardThirdRow);
const handleBackspace = () => {
if(currentColumn === 0){
return
}
currentColumn--
guesses[currentRow][currentColumn] = ""
const tile = document.querySelector("#row"+currentRow+"column"+currentColumn)
tile.textContent = ""
};
const backspaceButton = document.createElement("button");
backspaceButton.addEventListener("click", handleBackspace);
backspaceButton.textContent = "<";
backspaceAndEnterRow.append(backspaceButton);
const enterButton = document.createElement("button");
enterButton.addEventListener("click", checkGuess);
enterButton.textContent = "ENTER";
backspaceAndEnterRow.append(enterButton);
document.onkeydown = function (evt) {
evt = evt || window.evt
if(evt.key === "Enter"){
checkGuess();
} else if (evt.key === "Backspace") {
handleBackspace()
} else {
handleKeyboardOnClick(evt.key.toUpperCase())
}
}