Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1555 Added the Word Ladder Puzzle Game #1592

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions projects/Word Ladder Puzzle Game/index.html
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>
67 changes: 67 additions & 0 deletions projects/Word Ladder Puzzle Game/script.js
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;
}
57 changes: 57 additions & 0 deletions projects/Word Ladder Puzzle Game/style.css
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;
}
Loading