-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #832 from pratheekv39/word_ladder__puzzle_game
#774 Added the Word Ladder Puzzle Game
- Loading branch information
Showing
3 changed files
with
129 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,69 @@ | ||
# Word Ladder Puzzle 🔤 | ||
|
||
This is a simple **HTML, CSS, and JavaScript-based web implementation** of the classic **Word Ladder Puzzle**. In this game, you need to transform one word into another by changing only one letter at a time, with each intermediate step being a valid word! | ||
|
||
## 🎯 Objective | ||
|
||
The goal is to transform the start word into the end word by creating a chain of valid words. Each word in the chain should differ by only one letter from the previous word. | ||
|
||
## 🚀 How to Play | ||
|
||
1. **Start the Game**: Open the `index.html` file in a web browser. | ||
2. **Enter Words**: Input a start word and an end word in the respective fields. | ||
3. **Begin the Puzzle**: Click the "Start Game" button. | ||
4. **Make Word Changes**: Enter a new word that differs by one letter from the current word. | ||
5. **Submit Your Word**: Click the "Submit" button to add your word to the chain. | ||
6. **Receive Feedback**: After each submission, you'll get one of these responses: | ||
* "Invalid word. Try again." if the word is not in the dictionary. | ||
* "You can only change one letter at a time." if more than one letter was changed. | ||
* "Congratulations! You've completed the word ladder!" if you reach the end word. | ||
7. **Keep Transforming**: Continue changing words until you reach the end word. | ||
|
||
## 🛠 System Requirements | ||
|
||
* **Web Browser**: Any modern web browser (Chrome, Firefox, Safari, Edge) | ||
|
||
## Dependencies | ||
|
||
This game uses only HTML, CSS, and vanilla JavaScript, so no additional dependencies are required. | ||
|
||
## 🔧 How to Run | ||
|
||
1. Clone the repository and navigate to the project folder: | ||
|
||
``` | ||
git clone <repository-url> | ||
cd <project-folder> | ||
``` | ||
|
||
2. Open the `index.html` file in your web browser. | ||
|
||
3. Enjoy playing the Word Ladder Puzzle! | ||
|
||
## 📚 Game Mechanics | ||
|
||
* **Word Validation**: The game checks if each word exists in its dictionary. | ||
* **One-Letter Difference**: Each word must differ by only one letter from the previous word. | ||
* **Word Chain**: The game displays the chain of words as you progress. | ||
* **User Input**: You enter each new word in the input field. | ||
* **Feedback**: The game provides immediate feedback on each word submission. | ||
|
||
## 💻 System Specifications | ||
|
||
* HTML5 | ||
* CSS3 | ||
* JavaScript (ES6+) | ||
* Any modern web browser | ||
|
||
## 📖 Additional Information | ||
|
||
The Word Ladder Puzzle is an excellent way to enhance your vocabulary and practice lateral thinking. It's also a great introduction to basic web development concepts like DOM manipulation and event handling. | ||
|
||
## 🤔 Strategy Tips | ||
|
||
1. Focus on changing one letter at a time that gets you closer to the end word. | ||
2. Sometimes, you may need to change a letter that doesn't seem to get you closer, to set up for future moves. | ||
3. If you're stuck, try to think of words that are similar to your target word and work backwards. | ||
4. Expand your vocabulary! The more words you know, the easier the game becomes. | ||
|
||
Enjoy playing the Word Ladder Puzzle! 🎉 |
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,58 @@ | ||
import random | ||
|
||
class WordLadderPuzzle: | ||
def __init__(self): | ||
self.words = set(self.load_words()) | ||
|
||
def load_words(self): | ||
# In a real implementation, this would load from a file | ||
return ['cat', 'cot', 'dot', 'dog', 'log', 'lag', 'bag', 'big', 'pig', 'pin', 'pan'] | ||
|
||
def get_neighbors(self, word): | ||
return [w for w in self.words if self.is_neighbor(word, w)] | ||
|
||
def is_neighbor(self, word1, word2): | ||
if len(word1) != len(word2): | ||
return False | ||
return sum(c1 != c2 for c1, c2 in zip(word1, word2)) == 1 | ||
|
||
def find_ladder(self, start, end): | ||
if start not in self.words or end not in self.words: | ||
return None | ||
|
||
queue = [(start, [start])] | ||
visited = set() | ||
|
||
while queue: | ||
(word, path) = queue.pop(0) | ||
if word not in visited: | ||
visited.add(word) | ||
|
||
if word == end: | ||
return path | ||
|
||
for neighbor in self.get_neighbors(word): | ||
if neighbor not in visited: | ||
queue.append((neighbor, path + [neighbor])) | ||
|
||
return None | ||
|
||
def play(self): | ||
print("Welcome to Word Ladder Puzzle!") | ||
start = input("Enter the starting word: ").lower() | ||
end = input("Enter the target word: ").lower() | ||
|
||
if len(start) != len(end): | ||
print("Words must be of the same length.") | ||
return | ||
|
||
ladder = self.find_ladder(start, end) | ||
if ladder: | ||
print(f"Found a ladder in {len(ladder) - 1} steps:") | ||
print(" -> ".join(ladder)) | ||
else: | ||
print("No valid word ladder found.") | ||
|
||
if __name__ == "__main__": | ||
game = WordLadderPuzzle() | ||
game.play() |
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