From b46f1cfea779fdaeb7df663b6f248a4088f3aa04 Mon Sep 17 00:00:00 2001 From: dilip27m Date: Sun, 8 Dec 2024 20:40:12 +0530 Subject: [PATCH] added solution for issue3 --- 3-typing-game/game/index.html | 24 ++++++++++++++ 3-typing-game/game/script.js | 59 +++++++++++++++++++++++++++++++++++ 3-typing-game/game/style.css | 24 ++++++++++++++ 3-typing-game/solution.md | 8 +++++ 4 files changed, 115 insertions(+) create mode 100644 3-typing-game/game/index.html create mode 100644 3-typing-game/game/script.js create mode 100644 3-typing-game/game/style.css create mode 100644 3-typing-game/solution.md diff --git a/3-typing-game/game/index.html b/3-typing-game/game/index.html new file mode 100644 index 0000000..ec78392 --- /dev/null +++ b/3-typing-game/game/index.html @@ -0,0 +1,24 @@ + + + + + + + Cows and bull game + + + +

COws and bulls game

+

Guess the 4-digit number. Before time completes

+
Time left: 300 seconds
+ + +
+
+

History

+ +
+ + + + \ No newline at end of file diff --git a/3-typing-game/game/script.js b/3-typing-game/game/script.js new file mode 100644 index 0000000..5b54ef1 --- /dev/null +++ b/3-typing-game/game/script.js @@ -0,0 +1,59 @@ +function fourDigitNumber() { + let digits = []; + while (digits.length < 4) { + let randomDigit = Math.floor(Math.random() * 10); + if (!digits.includes(randomDigit)) { + digits.push(randomDigit); + } + } + return digits.join(''); +} + +const code = fourDigitNumber(); + +let timeLeft = 300; + + +const timer = setInterval(() => { + timeLeft--; + document.getElementById('timer').innerText = `Time left: ${timeLeft} seconds`; + + if (timeLeft <= 0) { + clearInterval(timer); + document.getElementById('result').innerHTML = `You lost the game. The number was ${code}`; + document.getElementById('guess').disabled = true; + } +}, 1000); + + +function checkGuess() { + const guess = document.getElementById('guess').value; + if (guess.length !== 4 || isNaN(guess)) { + alert("Please enter a valid 4-digit number."); + return; + } + + let bulls = 0; + let cows = 0; + + for (let i = 0; i < 4; i++) { + if (guess[i] === code[i]) { + bulls++; + } else if (code.includes(guess[i])) { + cows++; + } + } + + const historyList = document.getElementById('historyList'); + const guessResult = document.createElement('li'); + guessResult.textContent = `Guess: ${guess} - ${bulls} Bulls, ${cows} Cows`; + historyList.appendChild(guessResult); + + if (bulls === 4) { + clearInterval(timer); + document.getElementById('result').innerHTML = ` Congratulations! You won the match,you guessed the number : ${secretNumber}`; + document.getElementById('guess').disabled = true; + } else { + document.getElementById('result').innerHTML = `Keep trying! Check your history below.`; + } +} \ No newline at end of file diff --git a/3-typing-game/game/style.css b/3-typing-game/game/style.css new file mode 100644 index 0000000..5e35d3a --- /dev/null +++ b/3-typing-game/game/style.css @@ -0,0 +1,24 @@ +body { + font-family: Arial, sans-serif; + text-align: center; + margin-top: 50px; +} +#timer { + font-size: 24px; + color: red; +} +#result { + margin-top: 20px; + font-size: 18px; +} +#history { + margin-top: 20px; +} +ul { + list-style-type: none; + padding: 0; +} +li { + margin: 5px 0; + font-size: 16px; +} \ No newline at end of file diff --git a/3-typing-game/solution.md b/3-typing-game/solution.md new file mode 100644 index 0000000..03cbd2b --- /dev/null +++ b/3-typing-game/solution.md @@ -0,0 +1,8 @@ +## cow-bull Game + +Actually i have a created basic game only using the concepts that i learnt in the typing +its just a basic ,we need to guess the 4 digit number randomly generated by the computer +and there is a 5 minutes time limit.you need to guess the number before time ends +example suppose computer generates 1276 and u guessed 1265 ,when both number and position +matchs it is bull when only number is present position is incorrect it shows cow +in this 1,2 is bull and 6 is cow .it will display the previous guessed data also. \ No newline at end of file