WTAssignment 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Name – Uditkumar Panda Roll No.

– 02 Class – TE Comp

HTML Code –

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Word Guessing Game</title>
<!--Google Fonts-->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
rel="stylesheet"
/>
<!-- Stylesheet -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="wrapper">
<div class="hint-ref"></div>
<div id="user-input-section"></div>
<div id="message"></div>
<div id="letter-container"></div>
</div>
<div class="controls-container">
<div id="result"></div>
<div id="word"></div>
<button id="start">Start</button>
</div>
<!-- Script -->
<script src="script.js"></script>
</body>
</html>

CSS Code –

*{
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background-color: #7786f5;
}
.wrapper {
Name – Uditkumar Panda Roll No. – 02 Class – TE Comp

position: absolute;
width: 90%;
max-width: 37em;
background-color: #ffffff;
padding: 7em 3em;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
text-align: center;
border-radius: 1em;
}
.controls-container {
background-color: #7786f5;
position: absolute;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
top: 0;
}
#start {
font-size: 1.2em;
padding: 1em 3em;
background-color: #ffffff;
border: none;
outline: none;
border-radius: 2em;
cursor: pointer;
}
#letter-container {
display: flex;
flex-wrap: wrap;
gap: 0.8em 0.4em;
justify-content: center;
margin-top: 2em;
}
#letter-container button {
background-color: #ffffff;
border: 2px solid #7786f5;
color: #7786f5;
Name – Uditkumar Panda Roll No. – 02 Class – TE Comp

outline: none;
border-radius: 0.3em;
cursor: pointer;
height: 3em;
width: 3em;
}
#letter-container .correct {
background-color: #008000;
color: #ffffff;
border: 2px solid #008000;
}
#letter-container .incorrect {
background-color: #8a8686;
color: #ffffff;
border: 2px solid #8a8686;
}
.hint-ref {
margin-bottom: 1em;
}
.hint-ref span {
font-weight: 600;
}
#chanceCount {
margin: 1em 0;
position: absolute;
top: 0.62em;
right: 2em;
}
#word {
font-weight: 600;
margin: 1em 0 2em 0;
}
#word span {
text-transform: uppercase;
font-weight: 400;
}
.hide {
display: none;
}

JavaScript Code –

const options = {
Name – Uditkumar Panda Roll No. – 02 Class – TE Comp

aroma: "Pleasing smell",


pepper: "Salt's partner",
halt: "put a stop to",
jump: "Rise suddenly ",
shuffle: "Mix cards up ",
combine: "Add; Mix",
chaos: "Total disorder",
labyrinth: "Maze",
disturb: "Interrupt; upset ",
shift: "Move; Period of word",
machine: "Device or appliance",
};

const message = document.getElementById("message");


const hintRef = document.querySelector(".hint-ref");
const controls = document.querySelector(".controls-container");
const startBtn = document.getElementById("start");
const letterContainer = document.getElementById("letter-container");
const userInpSection = document.getElementById("user-input-section");
const resultText = document.getElementById("result");
const word = document.getElementById("word");
const words = Object.keys(options);
let randomWord = "",
randomHint = "";
let winCount = 0,
lossCount = 0;

const generateRandomValue = (array) => Math.floor(Math.random() * array.length);


const blocker = () => {
let lettersButtons = document.querySelectorAll(".letters");
stopGame();
};

startBtn.addEventListener("click", () => {
controls.classList.add("hide");
init();
});

const stopGame = () => {


controls.classList.remove("hide");
};

const generateWord = () => {


Name – Uditkumar Panda Roll No. – 02 Class – TE Comp

letterContainer.classList.remove("hide");
userInpSection.innerText = "";
randomWord = words[generateRandomValue(words)];
randomHint = options[randomWord];
hintRef.innerHTML = `<div id="wordHint">
<span>Hint: </span>${randomHint}</div>`;
let displayItem = "";
randomWord.split("").forEach((value) => {
displayItem += '<span class="inputSpace">_ </span>';
});

userInpSection.innerHTML = displayItem;
userInpSection.innerHTML += `<div id='chanceCount'>Chances Left: ${lossCount}</div>`;
};

const init = () => {


winCount = 0;
lossCount = 5;
randomWord = "";
word.innerText = "";
randomHint = "";
message.innerText = "";
userInpSection.innerHTML = "";
letterContainer.classList.add("hide");
letterContainer.innerHTML = "";
generateWord();

for (let i = 65; i < 91; i++) {


let button = document.createElement("button");
button.classList.add("letters");

button.innerText = String.fromCharCode(i);

button.addEventListener("click", () => {
message.innerText = `Correct Letter`;
message.style.color = "#008000";
let charArray = randomWord.toUpperCase().split("");
let inputSpace = document.getElementsByClassName("inputSpace");

if (charArray.includes(button.innerText)) {
charArray.forEach((char, index) => {
if (char === button.innerText) {
button.classList.add("correct");
Name – Uditkumar Panda Roll No. – 02 Class – TE Comp

inputSpace[index].innerText = char;
winCount += 1;
if (winCount == charArray.length) {
resultText.innerHTML = "You Won";
startBtn.innerText = "Restart";
alert("Congratulations! You've won the game!");
//block all buttons
blocker();
}
}
});
} else {
button.classList.add("incorrect");
lossCount -= 1;
document.getElementById(
"chanceCount"
).innerText = `Chances Left: ${lossCount}`;
message.innerText = `Incorrect Letter`;
alert("Incorrect Letter");
message.style.color = "#ff0000";
if (lossCount == 0) {
word.innerHTML = `The word was: <span>${randomWord}</span>`;
resultText.innerHTML = "Game Over";
alert("Game Over!!");
blocker();
}
}

button.disabled = true;
});

letterContainer.appendChild(button);
}
};

window.onload = () => {
init();
};
Name – Uditkumar Panda Roll No. – 02 Class – TE Comp

Output –

You might also like