Six letter word game
Six letter word
const sixLetterWords = [
«abacus», «abroad», «accept», «access», «action», «advice», «advise»,
«affect», «agency», «almost», «always», «animal», «answer», «anyone»,
«appear», «around», «artist», «attack», «author», «beauty», «become»,
«before», «behind», «belief», «better», «beyond», «bottle», «bounce»,
«bright», «broken», «budget», «burden», «button», «camera», «carbon»,
«career», «castle», «casual», «center», «chance», «change», «charge»,
«choice», «church», «circle», «client», «climate», «cloudy», «coffee»,
«combat», «coming», «commit», «common», «couple», «course», «create»,
«credit», «crisis», «custom», «danger», «decade», «decide», «define»,
«degree», «demand», «design», «desire», «detail», «device», «dinner»,
«direct», «doctor», «dollar», «domain», «double», «driver», «easily»,
«effort», «either», «empire», «enable», «energy», «engine», «enough»,
«entire», «escape», «ethnic», «expert», «export», «family», «father»,
«famous», «farmer», «faster», «favour», «female», «figure», «finger»,
«finish», «flight», «flower», «follow», «forest», «forget», «formal»,
«friend», «future», «garden», «gather», «global», «golden», «growth»,
«guitar», «handle», «health», «height», «hidden», «highly», «honest»,
«impact», «import», «income», «indeed», «inform», «injury», «inside»,
«invest», «island», «itself», «jacket», «jointly», «jungle», «keeper»,
«kitchen», «laptop», «leader», «length», «letter», «likely», «liquid»,
«listen», «little», «locate», «lovely», «luxury», «mainly», «manage»,
«manner», «margin», «market», «master», «matter», «member», «memory»,
«mental», «middle», «minute», «mobile», «modern», «mostly», «mother»,
«motion», «museum», «mutual», «myself», «nation», «native», «nature»,
«nearly», «nearby», «needle», «neither», «notion», «number», «object»,
«office», «online», «option», «output», «oxygen», «parent», «partly»,
«pastor», «people», «permit», «person», «photograph», «planet», «police»,
«policy», «prefer», «pretty», «prince», «prison», «profit», «proper»,
«public», «puzzle», «rabbit», «random», «reader», «really», «reason»,
«recall», «recent», «record», «reduce», «region», «relate», «relief»,
«remove», «repair», «repeat», «report», «rescue», «resist», «result»,
«reveal», «review», «reward», «rhythm», «ribbon», «rocket», «runner»,
«sample», «school», «screen», «search», «season», «secret», «secure»,
«select», «senior», «series», «settle», «severe», «should», «signal»,
«silent», «silver», «simple», «single», «sixth», «sleeve», «smooth»,
«social», «sodium», «source», «speech», «spirit», «spring», «square»,
«status», «steady», «stereo», «stone», «strong», «studio», «submit»,
«sudden», «supply», «switch», «system», «target», «taught», «thanks»,
«theory», «thirty», «ticket», «toward», «travel», «treaty», «twelve»,
«unable», «unique», «united», «unless», «update», «useful», «vacuum»,
«valley», «vector», «victim», «vision», «volume», «wander», «wealth»,
«weekly», «weight», «window», «wonder», «worker», «writer», «yellow»,
«youth», «zodiac»
];
const secretWord = sixLetterWords[Math.floor(Math.random() * sixLetterWords.length)];
let attempts = 0;
document.addEventListener(«keydown», function(event) {
if (event.code === «Enter») {
event.preventDefault();
checkGuess();
}
});
function checkGuess() {
const guessInput = document.getElementById(‘guessInput’);
const guess = guessInput.value.toLowerCase();
const resultsDiv = document.getElementById(‘results’);
const gameDiv = document.getElementById(‘Game’);
if (guess.length !== 6) {
resultsDiv.innerHTML = ‘Enter a 6-letter word!’;
return;
}
if (!sixLetterWords.includes(guess)) {
alert(‘The word is not in the dictionary’);
return;
}
attempts++;
if (guess === secretWord) {
resultsDiv.innerHTML = ‘Congratulations! You guessed the word!’;
guessInput.disabled = true;
return;
}
let secretWordCopy = secretWord.split(»);
let guessStatus = Array(guess.length).fill(null);
let result = »;
// Шаг 1: Отмечаем все правильно угаданные буквы (на своих местах) зелёным
for (let i = 0; i < guess.length; i++) {
if (guess[i] === secretWord[i]) {
guessStatus[i] = 'green'; // Эта буква зелёная
secretWordCopy[i] = null; // Убираем букву, которая уже отмечена зелёным
}
}
// Шаг 2: Отмечаем оставшиеся буквы
for (let i = 0; i < guess.length; i++) {
if (guessStatus[i] === 'green') {
result += `
${guess[i]}`;
} else if (secretWordCopy.includes(guess[i])) {
// Если буква есть в оставшемся слове, но не на своём месте — жёлтая
result += `
${guess[i]}`;
const index = secretWordCopy.indexOf(guess[i]);
secretWordCopy[index] = null; // Убираем первую встреченную букву
} else {
// Если буквы нет в оставшихся — красная
result += `
${guess[i]}`;
}
}
const resultsHeight = resultsDiv.scrollHeight;
resultsDiv.innerHTML += `
${result}
`;
guessInput.value = »;
gameDiv.style.height = `${150 + resultsHeight}px`;
}h4 {
text-align: center;
}
body {
background-image: url(‘https://bogdansergachev23.thkit.ee/wp/wp-content/uploads/2024/10/background.jpg’);
}
.Game {
width: 40%;
margin: 0 auto;
border: 1px purple solid;
background: rgba(173, 216, 230, 0.5);
}
.guessInput {
display: block;
margin-left: auto;
margin-right: auto;
margin-top: 50px;
}
.guessButton {
display: block;
margin-left: auto;
margin-right: auto;
border-radius: 10px;
padding: 3px 4px;
font-size: 16px;
cursor: pointer;
}
.results {
max-width: fit-content;
margin-left: auto;
margin-right: auto;
font-size: 20px;
}
.result {
padding: 5px;
margin: -10px 0;
}