-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffbde75
commit f5804b3
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Word Ladder Puzzle</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>Word Ladder Puzzle</h1> | ||
<div class="game-area"> | ||
<input type="text" id="start-word" placeholder="Enter start word"> | ||
<input type="text" id="end-word" placeholder="Enter end word"> | ||
<button id="start-game">Start Game</button> | ||
<div id="word-chain"></div> | ||
<input type="text" id="user-word" placeholder="Enter your word"> | ||
<button id="submit-word">Submit</button> | ||
</div> | ||
<div id="message"></div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |