-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
146 lines (114 loc) · 4.33 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
const playBtn = document.getElementById("play-btn");
const swipeDuration = 0.8;
let attempts = 10;
let correctGuesses = 0;
const guessedCharacters = [];
const swipeOutLeft = (elementId) => {
document.body.style.overflowX = "hidden"; // Temporarily hide the scrollbar when moving elements outside of the view range
const el = document.getElementById(elementId);
el.style.animationName = "swipe-out-left";
el.style.animationDuration = `${swipeDuration}s`;
return new Promise((resolve) => {
setTimeout(() => {
document.body.style.overflowX = "visible";
resolve(el);
}, swipeDuration * 1000);
});
};
const swipeInRight = (elementId) => {
document.body.style.overflowX = "hidden";
const el = document.getElementById(elementId);
el.style.animationName = "swipe-in-right";
el.style.animationDuration = `${swipeDuration}s`;
return new Promise((resolve) => {
setTimeout(() => {
document.body.style.overflowX = "visible";
resolve(el);
}, swipeDuration * 1000);
});
};
const startGame = async (word) => {
const enterWordEl = await swipeOutLeft("enter-word");
enterWordEl.remove();
enterWordEl.style.display = "none";
const mainGameDiv = document.getElementById("main-game");
const attemptsH1 = document.getElementById("attempts");
attemptsH1.innerText = `Attempts: ${attempts}`;
for (character of word) {
const letterPlaceholder = document.createElement("input");
letterPlaceholder.type = "text";
letterPlaceholder.maxLength = 1;
letterPlaceholder.className = "letter-placeholder";
letterPlaceholder.disabled = true;
mainGameDiv.appendChild(letterPlaceholder);
}
mainGameDiv.appendChild(document.createElement("br"));
const letterInput = document.createElement("input");
letterInput.type = "text";
letterInput.className = "letter-input";
letterInput.maxLength = 1;
letterInput.required = true;
const letterSubmit = document.createElement("button");
letterSubmit.style.display = "none";
const wrongLetters = document.createElement("p");
wrongLetters.innerHTML = "Wrong guesses: ";
mainGameDiv.appendChild(letterInput);
mainGameDiv.appendChild(wrongLetters);
mainGameDiv.style.display = "block";
await swipeInRight("main-game");
const letterPlaceholders =
document.getElementsByClassName("letter-placeholder");
letterInput.focus();
letterInput.oninput = (e) => {
const guessedLetter = e.target.value;
if (guessedCharacters.includes(guessedLetter)) {
alert("You've already guessed this letter!");
letterInput.value = "";
return;
}
guessedCharacters.push(guessedLetter);
let guessIsCorrect = false;
for (let i = 0; i < letterPlaceholders.length; i++) {
if (guessedLetter.toLowerCase() === word[i].toLowerCase()) {
if (++correctGuesses === word.length) {
alert(
`Congratulations, you've won, the word was '${word}'.`
);
location.reload();
}
letterPlaceholders.item(i).value = word[i];
guessIsCorrect = true;
}
}
if (guessIsCorrect === false) {
attempts--;
wrongLetters.innerHTML += `${guessedLetter}, `;
if (attempts === 0) {
alert(`Oh no! You lost.. the word was '${word}'`);
location.reload();
}
attemptsH1.innerText = `Attempts: ${attempts}`;
}
letterInput.value = "";
};
};
const loadGame = async () => {
const titleElement = await swipeOutLeft("title");
titleElement.hidden = true;
document.getElementById("enter-word").style.display = "block";
await swipeInRight("enter-word");
const wordInput = document.getElementById("word-input");
wordInput.focus();
wordInput.oninput = (e) => {
console.log(e.target.value);
if (e.target.value === " ") {
e.target.value.slice(0, -1);
}
};
document.getElementById("word-form").onsubmit = (e) => {
e.preventDefault();
const word = wordInput.value;
startGame(word);
};
};
playBtn.onclick = loadGame;