diff --git a/projects/Word Ladder Puzzle Game/index.html b/projects/Word Ladder Puzzle Game/index.html new file mode 100644 index 00000000..91ded55d --- /dev/null +++ b/projects/Word Ladder Puzzle Game/index.html @@ -0,0 +1,24 @@ + + + + + + Word Ladder Puzzle + + + +
+

Word Ladder Puzzle

+
+ + + +
+ + +
+
+
+ + + \ No newline at end of file diff --git a/projects/Word Ladder Puzzle Game/script.js b/projects/Word Ladder Puzzle Game/script.js new file mode 100644 index 00000000..da62b448 --- /dev/null +++ b/projects/Word Ladder Puzzle Game/script.js @@ -0,0 +1,67 @@ +// A simple word list (you should replace this with a more comprehensive dictionary) +const wordList = new Set(['cat', 'cot', 'dot', 'dog', 'log', 'lag', 'bag', 'big', 'pig', 'pin', 'pan', 'man', 'mat', 'hat']); + +let startWord, endWord, currentWord; +let wordChain = []; + +document.getElementById('start-game').addEventListener('click', startGame); +document.getElementById('submit-word').addEventListener('click', submitWord); + +function startGame() { + startWord = document.getElementById('start-word').value.toLowerCase(); + endWord = document.getElementById('end-word').value.toLowerCase(); + + if (startWord.length !== endWord.length || !wordList.has(startWord) || !wordList.has(endWord)) { + showMessage('Invalid words. Please try again.'); + return; + } + + currentWord = startWord; + wordChain = [startWord]; + updateWordChain(); + showMessage('Game started! Change one letter at a time to reach the end word.'); +} + +function submitWord() { + const userWord = document.getElementById('user-word').value.toLowerCase(); + + if (!isValidWord(userWord)) { + showMessage('Invalid word. Try again.'); + return; + } + + if (!isOneLetterDifference(currentWord, userWord)) { + showMessage('You can only change one letter at a time.'); + return; + } + + currentWord = userWord; + wordChain.push(currentWord); + updateWordChain(); + + if (currentWord === endWord) { + showMessage('Congratulations! You\'ve completed the word ladder!'); + } +} + +function isValidWord(word) { + return wordList.has(word); +} + +function isOneLetterDifference(word1, word2) { + if (word1.length !== word2.length) return false; + let differences = 0; + for (let i = 0; i < word1.length; i++) { + if (word1[i] !== word2[i]) differences++; + } + return differences === 1; +} + +function updateWordChain() { + document.getElementById('word-chain').textContent = wordChain.join(' → '); + document.getElementById('user-word').value = ''; +} + +function showMessage(message) { + document.getElementById('message').textContent = message; +} \ No newline at end of file diff --git a/projects/Word Ladder Puzzle Game/style.css b/projects/Word Ladder Puzzle Game/style.css new file mode 100644 index 00000000..54450506 --- /dev/null +++ b/projects/Word Ladder Puzzle Game/style.css @@ -0,0 +1,57 @@ +body { + font-family: Arial, sans-serif; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + margin: 0; + background-color: #f0f0f0; +} + +.container { + background-color: white; + padding: 2rem; + border-radius: 10px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); +} + +h1 { + text-align: center; + color: #333; +} + +.game-area { + display: flex; + flex-direction: column; + gap: 1rem; +} + +input, button { + padding: 0.5rem; + font-size: 1rem; +} + +button { + background-color: #4CAF50; + color: white; + border: none; + cursor: pointer; + transition: background-color 0.3s; +} + +button:hover { + background-color: #45a049; +} + +#word-chain { + min-height: 2rem; + border: 1px solid #ddd; + padding: 0.5rem; + margin-top: 1rem; +} + +#message { + margin-top: 1rem; + text-align: center; + font-weight: bold; +} \ No newline at end of file