Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
207 lines (186 loc) · 4.94 KB
/
Copy pathscript.js
File metadata and controls
207 lines (186 loc) · 4.94 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Lithuanian words list
const words = [
'katinas',
'šuo',
'namas',
'medis',
'saulė',
'mėnulis',
'žvaigždė',
'vanduo',
'ugnis',
'žemė',
'oras',
'knyga',
'mokykla',
'draugas',
'šeima',
'miestas',
'kaimas',
'upė',
'ežeras',
'jūra',
'kalnas',
'miškas',
'gėlė',
'paukštis',
'drugelis',
'žuvis',
'arklys',
'karvė',
'kiaulė',
'višta',
'obuolys',
'kriaušė',
'vyšnia',
'braškė',
'morkva',
'bulvė',
'duona',
'pienas',
'sviestas',
'sūris',
'kiaušinis',
'mėsa',
'žaidimas',
'kompiuteris',
'telefonas',
'televizorius',
'automobilis',
'dviratis',
'lėktuvas',
'laivas',
'traukinys',
'kelias',
'tiltas',
'bažnyčia',
'ligoninė',
'parduotuvė',
'restoranas',
'biblioteka',
'stadionas',
'teatras',
'muziejus'
];
// Lithuanian alphabet (including special characters)
const lithuanianAlphabet = [
'a', 'ą', 'b', 'c', 'č', 'd', 'e', 'ę', 'ė', 'f', 'g', 'h', 'i', 'į', 'y',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 'š', 't', 'u', 'ų', 'ū', 'v', 'z', 'ž'
];
// Game state
let currentWord = '';
let guessedLetters = [];
let wrongGuesses = 0;
const maxWrongGuesses = 6;
// Body parts in order they appear
const bodyParts = ['head', 'body', 'left-arm', 'right-arm', 'left-leg', 'right-leg'];
// DOM elements
const wordDisplay = document.getElementById('word-display');
const keyboard = document.getElementById('keyboard');
const message = document.getElementById('message');
const newGameBtn = document.getElementById('new-game-btn');
const wrongCount = document.getElementById('wrong-count');
// Initialize the game
function initGame() {
currentWord = words[Math.floor(Math.random() * words.length)].toLowerCase();
guessedLetters = [];
wrongGuesses = 0;
// Reset display
message.textContent = '';
message.className = 'message';
newGameBtn.style.display = 'none';
wrongCount.textContent = '0';
// Hide all body parts
bodyParts.forEach(part => {
document.getElementById(part).classList.remove('visible');
});
// Create word display
renderWord();
// Create keyboard
renderKeyboard();
}
// Render the word with blanks
function renderWord() {
wordDisplay.innerHTML = '';
for (const letter of currentWord) {
const letterBox = document.createElement('div');
letterBox.className = 'letter-box';
letterBox.textContent = guessedLetters.includes(letter) ? letter : '';
wordDisplay.appendChild(letterBox);
}
}
// Create the keyboard
function renderKeyboard() {
keyboard.innerHTML = '';
lithuanianAlphabet.forEach(letter => {
const key = document.createElement('button');
key.className = 'key';
key.textContent = letter;
key.addEventListener('click', () => handleGuess(letter, key));
keyboard.appendChild(key);
});
}
// Handle a letter guess
function handleGuess(letter, keyElement) {
if (guessedLetters.includes(letter)) return;
guessedLetters.push(letter);
keyElement.disabled = true;
if (currentWord.includes(letter)) {
keyElement.classList.add('correct');
renderWord();
checkWin();
} else {
keyElement.classList.add('wrong');
wrongGuesses++;
wrongCount.textContent = wrongGuesses;
document.getElementById(bodyParts[wrongGuesses - 1]).classList.add('visible');
checkLose();
}
}
// Check if player won
function checkWin() {
const won = [...currentWord].every(letter => guessedLetters.includes(letter));
if (won) {
message.textContent = 'Sveikiname! Jūs laimėjote! 🎉';
message.className = 'message win';
endGame();
}
}
// Check if player lost
function checkLose() {
if (wrongGuesses >= maxWrongGuesses) {
message.textContent = `Pralaimėjote! Žodis buvo: ${currentWord.toUpperCase()}`;
message.className = 'message lose';
revealWord();
endGame();
}
}
// Reveal the full word
function revealWord() {
const letterBoxes = wordDisplay.querySelectorAll('.letter-box');
[...currentWord].forEach((letter, index) => {
letterBoxes[index].textContent = letter;
});
}
// End the game
function endGame() {
const keys = keyboard.querySelectorAll('.key');
keys.forEach(key => key.disabled = true);
newGameBtn.style.display = 'inline-block';
}
// Keyboard support
document.addEventListener('keydown', (e) => {
const letter = e.key.toLowerCase();
if (lithuanianAlphabet.includes(letter)) {
const keys = keyboard.querySelectorAll('.key');
keys.forEach(key => {
if (key.textContent === letter && !key.disabled) {
handleGuess(letter, key);
}
});
}
});
// New game button
newGameBtn.addEventListener('click', initGame);
// Start the game
initGame();