-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
270 lines (209 loc) · 6.94 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const game = {
secret: "",
guesses: [],
numDigits: 3,
maxGuesses: 10,
currentGuess: "",
gameOver: false,
mode: "easy", // Set the default mode
levels: {
easy: {
numDigits: 3,
maxGuesses: 10,
},
medium: {
numDigits: 4,
maxGuesses: 7
},
hard: {
numDigits: 6,
maxGuesses: 4
},
impossible: {
numDigits: 8,
maxGuesses: 1
},
},
};
function generateNumber() {
let digits = new Set();
while (digits.size < game.numDigits) {
digits.add(Math.floor(Math.random() * 10));
}
game.secret = Array.from(digits).join('');
// let secretNumber = document.getElementById('secretNumber');
// secretNumber.innerHTML = `Secret number: ${game.secret}`;
}
function checkGuess() {
let guessInput = document.getElementById('guessInput');
let guess = guessInput.value;
if (guess.length > game.numDigits) {
guess = guess.slice(0, game.numDigits);
}
if (!isValidGuess(guess)) {
alert(`Please enter a ${game.numDigits}-digit number with unique digits.`);
guessInput.value = '';
return;
}
if (!isNumric(guess)) {
alert(`Please enter a number.`);
guessInput.value = '';
return;
}
game.guesses.push(guess);
game.currentGuess = guess;
let guessResult;
if (game.mode == 'hard') {
guessResult = getGuessResultMode2(guess);
} else {
guessResult = getGuessResult(guess);
}
let guessList = document.getElementById('guessList');
let guessItem = document.createElement('li');
guessItem.innerHTML = guessResult;
guessList.appendChild(guessItem);
guessInput.value = '';
guessInput.focus();
if (game.secret === guess) {
game.gameOver = true;
let score = calculateScore();
alert(`Urime, ju fitoni! Rezultati juaj është ${score}.`);
restartGame();
} else if (game.guesses.length >= game.maxGuesses) {
game.gameOver = true;
disableGuessing();
alert(`Keni humbur. Numri ishte ${game.secret}.`);
restartGame();
}
howMuchLeft();
}
function howMuchLeft() {
document.getElementById('guessLeft').innerHTML = `Ju keni edhe <b>${game.maxGuesses - game.guesses.length}</b> mundesi.`;
document.getElementById('guessDigit').innerHTML = `Numir është me <b>${game.numDigits}</b> karaktere.`;
}
function getGuessResult(guess) {
let result = '';
for (let i = 0; i < game.numDigits; i++) {
let digit = guess[i];
let index = game.secret.indexOf(digit);
if (index === i) {
result += `<span class="green">${digit}</span>`;
} else if (index !== -1) {
result += `<span class="yellow">${digit}</span>`;
} else {
result += `<span class="red">${digit}</span>`;
}
}
return result;
}
function getGuessResultMode2(guess) {
let result = '';
let currectOrder = 0;
let diffrentOrder = 0;
for (let i = 0; i < game.numDigits; i++) {
let digit = guess[i];
let index = game.secret.indexOf(digit);
if (index === i) {
currectOrder++
} else if (index !== -1) {
diffrentOrder++
} else {
}
}
result += `<span> ${guess} - `;
if (currectOrder != 0) {
result += `${currectOrder} i fiksuar </span>`;
}
if (diffrentOrder != 0) {
result += `${diffrentOrder} i qelluar </span>`;
}
if (currectOrder == 0 && diffrentOrder == 0) {
result += `0 te fiksuar dhe 0 te qelluar </span>`;
}
return result;
}
function disableGuessing() {
document.getElementById('guessButton').disabled = true;
document.getElementById('guessInput').disabled = true;
}
function enableGuessing() {
let guessButton = document.getElementById('guessButton');
guessButton.disabled = false;
let guessInput = document.getElementById('guessInput');
guessInput.disabled = false;
guessInput.focus();
}
function restartGame() {
game.secret = '';
game.guesses = [];
game.currentGuess = '';
game.gameOver = false;
generateNumber();
remainingGuesses = game.maxGuesses;
let guessList = document.getElementById('guessList');
guessList.innerHTML = '';
enableGuessing();
howMuchLeft();
document.getElementById('guessInput').setAttribute('maxlength', game.numDigits);
}
function setGameLevel(mode) {
const selectedLevel = game.levels[mode] || game.levels.medium;
game.numDigits = selectedLevel.numDigits;
game.maxGuesses = selectedLevel.maxGuesses;
const levelButtons = document.querySelectorAll('[data-level]');
setActiveButton(levelButtons, document.querySelector(`[data-level="${mode}Level"]`));
restartGame();
}
function setGameMode(mode) {
game.mode = mode;
const modeButtons = document.querySelectorAll('[data-mode]');
setActiveButton(modeButtons, document.querySelector(`[data-mode="${mode}Mode"]`));
restartGame();
}
function setActiveButton(buttons, selectedButton) {
buttons.forEach(button => {
if (button === selectedButton) {
button.classList.add('active');
} else {
button.classList.remove('active');
}
});
}
function setDefaultActiveMode() {
mode = game.mode
const modeButtons = document.querySelectorAll('[data-mode]');
setActiveButton(modeButtons, document.querySelector(`[data-mode="${mode}Mode"]`));
const levelButtons = document.querySelectorAll('[data-level]');
setActiveButton(levelButtons, document.querySelector(`[data-level="${mode}Level"]`));
}
function isValidGuess(guess) {
let uniqueDigits = new Set(guess);
return guess.length === game.numDigits && uniqueDigits.size === game.numDigits;
}
function isNumric(guess) {
return /^\d+$/.test(guess);
}
function refreshPage() {
location.reload();
}
function calculateScore() {
let score = (game.maxGuesses - game.guesses.length + 1) * 100;
return score > 0 ? score : 0;
}
window.onload = function () {
generateNumber();
setDefaultActiveMode();
let guessButton = document.getElementById('guessButton');
guessButton.addEventListener('click', checkGuess);
howMuchLeft();
let guessInput = document.getElementById('guessInput');
guessInput.setAttribute('maxlength', game.numDigits);
guessInput.addEventListener('keydown', function (event) {
if (event.keyCode === 13) {
event.preventDefault();
guessButton.click();
}
});
let restartButton = document.getElementById('restartButton');
restartButton.addEventListener('click', refreshPage);
};