Skip to content

Commit

Permalink
Added the Word Ladder Puzzle Game
Browse files Browse the repository at this point in the history
  • Loading branch information
pratheekv39 committed Oct 22, 2024
1 parent ffbde75 commit f5804b3
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
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;
}

0 comments on commit f5804b3

Please sign in to comment.