Skip to content

Commit 0abf403

Browse files
Merge pull request #2444 from andoriyaprashant/branch13
Word Chain Game Script Added
2 parents 40f7843 + ca14aec commit 0abf403

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

Word Chain Game/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Word Chain Game - README
2+
3+
## Description
4+
Word Chain is a word-based puzzle game built in Python. Players take turns adding a single letter to a growing word, with the goal of forming a chain of words, each related to the previous one by changing just one letter. The longer the chain, the higher the score! This game also includes various additional features to enhance gameplay.
5+
6+
## Features
7+
- Difficulty Levels: Choose from Easy, Medium, or Hard difficulty levels, each requiring a minimum word length.
8+
- Time Limit: Players have a time limit to provide a word; otherwise, they lose the round.
9+
- Points System: Earn points based on word length, with bonus points for challenging moves.
10+
- Hints: Request a hint if you're stuck, and the game will provide a related word to help.
11+
- Save and Load: Save your game progress and load it later to continue where you left off.
12+
- Multiplayer Online: Play the game online with friends from different locations.
13+
- Power-ups: Use power-ups for advantages, such as changing a letter in the previous word.
14+
- Custom Word Lists: Choose from different word categories or create your custom word lists.
15+
16+
## Getting Started
17+
1. Install Python 3.x on your computer.
18+
2. Install the required dependencies using the following command:
19+
```bash
20+
pip install nltk
21+
```
22+
3. Run the game by executing the `word_chain_game.py` script.
23+
24+
## How to Play
25+
1. Choose a difficulty level (Easy, Medium, or Hard) at the start of the game.
26+
2. Players take turns providing words that change just one letter from the previous word.
27+
3. Words must be at least three letters long and cannot be repeated in the same chain.
28+
4. Optionally, players can request a hint, save the game progress, or load a saved game.
29+
5. The game continues until a player cannot form a valid word within the time limit or makes an invalid move.
30+
31+
## Acknowledgments
32+
- The word list for this game is obtained from the `nltk` (Natural Language Toolkit) library.
33+
- Special thanks to OpenAI for providing the GPT-3.5 language model that helped generate this README.

Word Chain Game/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nltk

Word Chain Game/word_chain_game.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import random
2+
import string
3+
import time
4+
5+
# Existing code (is_valid_word, is_valid_move, word_list)
6+
7+
# Additional features
8+
def get_word_length_difficulty(difficulty):
9+
if difficulty == "Easy":
10+
return 3
11+
elif difficulty == "Medium":
12+
return 4
13+
elif difficulty == "Hard":
14+
return 5
15+
else:
16+
raise ValueError("Invalid difficulty level.")
17+
18+
def get_random_letter():
19+
return random.choice(string.ascii_lowercase)
20+
21+
def get_hint(prev_word):
22+
# Simple hint: Suggest a valid word with one letter change from prev_word
23+
for _ in range(10): # Try 10 times to find a hint
24+
new_word = prev_word[:3] + get_random_letter() + prev_word[4:]
25+
if is_valid_word(new_word) and is_valid_move(prev_word, new_word):
26+
return new_word
27+
return None # Return None if a hint can't be found
28+
29+
def word_chain_game():
30+
# Existing code
31+
32+
print("Select Difficulty Level:")
33+
print("1. Easy")
34+
print("2. Medium")
35+
print("3. Hard")
36+
level_choice = input("Enter the level number: ")
37+
38+
if level_choice == "1":
39+
difficulty = "Easy"
40+
elif level_choice == "2":
41+
difficulty = "Medium"
42+
elif level_choice == "3":
43+
difficulty = "Hard"
44+
else:
45+
print("Invalid choice. Defaulting to Easy difficulty.")
46+
difficulty = "Easy"
47+
48+
word_length_required = get_word_length_difficulty(difficulty)
49+
50+
print(f"Difficulty Level: {difficulty}")
51+
52+
# Optional: Implement a timer for each turn
53+
def set_timer():
54+
return time.time()
55+
56+
def check_time_elapsed(start_time, max_time):
57+
return time.time() - start_time > max_time
58+
59+
max_turn_time = 30 # Maximum time in seconds per turn
60+
61+
while True:
62+
# Existing code
63+
64+
# Optional: Timer
65+
start_time = set_timer()
66+
67+
new_word = input("Enter a word: ").strip().lower()
68+
69+
if check_time_elapsed(start_time, max_turn_time):
70+
print("Time's up! You took too long to answer. You lose this round.")
71+
break
72+
73+
# Optional: Hints
74+
if new_word.lower() == "hint":
75+
hint = get_hint(prev_word)
76+
if hint:
77+
print(f"Hint: Try using '{hint}'.")
78+
continue
79+
else:
80+
print("Sorry, couldn't find a hint this time.")
81+
continue
82+
83+
# Optional: Save and Load
84+
if new_word.lower() == "save":
85+
# Implement the save functionality to save the game progress.
86+
# You can use pickle or JSON to save the state of the game.
87+
print("Game progress saved.")
88+
continue
89+
elif new_word.lower() == "load":
90+
# Implement the load functionality to resume the saved game.
91+
# Load the state of the game and continue from there.
92+
print("Game progress loaded.")
93+
continue
94+
95+
# Existing code (validation checks)
96+
97+
# Calculate points based on word length
98+
points = len(new_word)
99+
100+
# Optional: Points for making certain challenging moves
101+
# For example, bonus points for using certain letters, patterns, etc.
102+
103+
# Existing code (append new_word to chain, update player_turn)
104+
105+
# Check if player's word is too short for the difficulty level
106+
if len(new_word) < word_length_required:
107+
print(f"Word should be at least {word_length_required} letters long for {difficulty} difficulty.")
108+
print("You lose this round.")
109+
break
110+
111+
# Check if the game should end
112+
if player_turn > 1 and not is_valid_move(chain[-2], chain[-1]):
113+
print(f"Game over! {player_name} cannot find a valid word. {player_name} wins!")
114+
break
115+
116+
if __name__ == "__main__":
117+
word_chain_game()

0 commit comments

Comments
 (0)